Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/integration/contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@
],
"tests/integration/sdk/test_http2_wire.py::test_sync_handler_negotiates_http2_only_when_enabled": [
"other.sdk_wire.http2.sync_handler_negotiates_h2_only_when_enabled"
],
"tests/integration/mcp/test_mcp_lifecycle.py::test_health_intersects_route_restricted_key_grants_in_both_management_modes": [
"other.mcp.health.restricted_keys_intersect_grants_in_both_modes"
],
"tests/integration/mcp/test_mcp_lifecycle.py::test_warm_credential_removal_rejects_without_upstream_traffic": [
"other.mcp.credentials.warm_removal_fails_closed_without_upstream_traffic"
],
"tests/integration/observability/test_guardrail_effects.py::test_request_selected_mcp_guardrail_blocks_direct_and_virtual_calls": [
"other.mcp.guardrails.request_selection_blocks_resolved_tool_without_execution"
]
},
"browser": {
Expand Down
100 changes: 100 additions & 0 deletions tests/integration/mcp/test_mcp_lifecycle.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import uuid
from contextlib import ExitStack
from pathlib import Path
from typing import Final

import pytest
import yaml
from hypothesis import strategies as st
from hypothesis.stateful import RuleBasedStateMachine, invariant, rule, run_state_machine_as_test

from integration._support.client import Gateway
from integration._support.database import read_rows
from integration._support.generation import LIFECYCLE_SETTINGS, bounded_http_requests
from integration._support.process import owned_proxy
from integration._support.mcp import call_tool, mcp_peer, register_mcp, tool_names


Expand Down Expand Up @@ -121,3 +124,100 @@ def teardown(self) -> None:
self.resources.close()

run_state_machine_as_test(Servers, settings=LIFECYCLE_SETTINGS)


@pytest.mark.covers("other.mcp.health.restricted_keys_intersect_grants_in_both_modes")
def test_health_intersects_route_restricted_key_grants_in_both_management_modes(
gateway: Gateway, tmp_path: Path
) -> None:
for mode in ("restricted", "view_all"):
config = yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text())
config["general_settings"]["user_mcp_management_mode"] = mode
path = tmp_path / f"health-{mode}.yaml"
path.write_text(yaml.safe_dump(config))
with (
owned_proxy(gateway, tmp_path, {}, config=path) as candidate,
mcp_peer() as peer,
candidate.scenario() as scenario,
):
first = register_mcp(scenario, peer, "health" + uuid.uuid4().hex)
second = register_mcp(scenario, peer, "health" + uuid.uuid4().hex)
owned = {first, second}
control = scenario.key(object_permission={"mcp_servers": [first]})
names = tool_names(candidate, control, first)
healthy = call_tool(candidate, control, first, names["add"], {"a": 3, "b": 5})
assert healthy.status_code == 200 and healthy.json()["content"][0]["text"] == "8", healthy.text
for grants in ([first], [second], []):
key = scenario.key(
allowed_routes=["/v1/mcp/server", "/v1/mcp/server/health"],
object_permission={"mcp_servers": grants},
)
listed = candidate.request("GET", "/v1/mcp/server", key=key)
assert listed.status_code == 200, listed.text
assert {row["server_id"] for row in listed.json()}.intersection(owned) == set(grants)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
for requested in (None, [second], [first, second]):
response = candidate.client.get(
"/v1/mcp/server/health",
headers={"Authorization": f"Bearer {key}"},
params=[] if requested is None else [("server_ids", identity) for identity in requested],
)
assert response.status_code == 200, response.text
expected = set(grants) if requested is None else set(grants).intersection(requested)
assert {row["server_id"] for row in response.json()}.intersection(owned) == expected, response.text
assert all(row["status"] == "healthy" for row in response.json() if row["server_id"] in owned)


@pytest.mark.covers("other.mcp.credentials.warm_removal_fails_closed_without_upstream_traffic")
def test_warm_credential_removal_rejects_without_upstream_traffic(gateway: Gateway) -> None:
with mcp_peer() as peer, gateway.scenario() as scenario:
identity = register_mcp(
scenario,
peer,
"credentials" + uuid.uuid4().hex,
auth_type="bearer_token",
static_headers={"Authorization": "Bearer synthetic-upstream-credential"},
)
key = scenario.key(object_permission={"mcp_servers": [identity]})
names = tool_names(gateway, key, identity)
warm = call_tool(gateway, key, identity, names["add"], {"a": 3, "b": 5})
assert warm.status_code == 200 and warm.json()["content"][0]["text"] == "8", warm.text
calls = tuple(item for item in peer.drain() if item["body"].get("method") == "tools/call")
assert len(calls) == 1
assert calls[0]["headers"][b"authorization"] == b"Bearer synthetic-upstream-credential"
removed = gateway.request("PUT", "/v1/mcp/server", {"server_id": identity, "static_headers": {}})
assert removed.status_code == 202, removed.text
stored = gateway.request("GET", f"/v1/mcp/server/{identity}")
assert stored.status_code == 200, stored.text
assert stored.json()["auth_type"] == "bearer_token"
assert not stored.json().get("static_headers"), stored.text
peer.drain()
for operation in ("list", "call"):
rejected = (
gateway.client.get(
"/mcp-rest/tools/list", params={"server_id": identity}, headers={"x-litellm-api-key": key}
)
if operation == "list"
else call_tool(gateway, key, identity, names["add"], {"a": 3, "b": 5})
)
assert rejected.status_code == 500, rejected.text
assert peer.drain() == (), "missing static credential escaped to upstream"
changed = gateway.request(
"PUT",
"/v1/mcp/server",
{
"server_id": identity,
"auth_type": "oauth2_token_exchange",
"token_exchange_endpoint": peer.url + "/token",
"credentials": {"client_id": "synthetic-client"},
},
)
assert changed.status_code == 202, changed.text
peer.drain()
rejected_subject = call_tool(gateway, key, identity, names["add"], {"a": 3, "b": 5})
assert rejected_subject.status_code == 401, rejected_subject.text
assert peer.drain() == (), "virtual key cannot supply an OBO subject token"
control_id = register_mcp(scenario, peer, "control" + uuid.uuid4().hex, auth_type="none")
control_key = scenario.key(object_permission={"mcp_servers": [control_id]})
control_names = tool_names(gateway, control_key, control_id)
control = call_tool(gateway, control_key, control_id, control_names["multiply"], {"a": 3, "b": 5})
assert control.status_code == 200 and control.json()["content"][0]["text"] == "15", control.text
71 changes: 71 additions & 0 deletions tests/integration/observability/test_guardrail_effects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from integration._support.client import Gateway, eventually
from integration._support.database import read_rows
from integration._support.mcp import mcp_peer, register_mcp, tool_names
from integration._support.process import owned_proxy
from integration._support.wire import Reply, Request, wire_server

Expand Down Expand Up @@ -143,3 +144,73 @@ def guardrail(request: Request) -> Reply:
)
assert len(observed.get("/__observations").json()["requests"]) == 1
assert len(policy.drain()) == 2


@pytest.mark.covers("other.mcp.guardrails.request_selection_blocks_resolved_tool_without_execution")
def test_request_selected_mcp_guardrail_blocks_direct_and_virtual_calls(gateway: Gateway, tmp_path: Path) -> None:
guardrail = "mcp-policy-" + uuid.uuid4().hex
config = yaml.safe_load(Path("tests/integration/proxy_config.yaml").read_text())
config["guardrails"] = [
{
"guardrail_name": guardrail,
"litellm_params": {
"guardrail": "custom_code",
"mode": "pre_mcp_call",
"default_on": False,
"custom_code": (
"def apply_guardrail(inputs, request_data, input_type):\n"
' if inputs.get("tools", [{}])[0].get("function", {}).get("name") == "add":\n'
' return block("integration resolved add denied")\n'
" return allow()\n"
),
},
}
]
path = tmp_path / "mcp-guardrail.yaml"
path.write_text(yaml.safe_dump(config))
with (
owned_proxy(gateway, tmp_path, {}, config=path) as candidate,
mcp_peer() as peer,
candidate.scenario() as scenario,
):
identity = register_mcp(scenario, peer, "guardrail" + uuid.uuid4().hex)
permission = {"mcp_servers": [identity], "mcp_tool_search_enabled": True}
key = scenario.key(object_permission=permission)
key_selected = scenario.key(object_permission=permission, guardrails=[guardrail])
team = scenario.team(guardrails=[guardrail], object_permission={"mcp_servers": [identity]})
team_selected = scenario.key(team_id=team, object_permission=permission)
catalog_key = scenario.key(object_permission={"mcp_servers": [identity]})
names = tool_names(candidate, catalog_key, identity)
assert set(names) == {"add", "multiply", "fail"}
for virtual in (False, True):
for caller, selected, tool, expected in (
(key, [], "add", 8),
(key, [guardrail], "add", None),
(key_selected, [], "add", None),
(team_selected, [], "add", None),
(key, [guardrail], "multiply", 15),
):
arguments = {"a": 3, "b": 5}
peer.drain()
response = candidate.client.post(
"/mcp-rest/tools/call",
headers={"x-litellm-api-key": caller},
json={
"server_id": identity,
"name": "mcp_tool_call" if virtual else names[tool],
"arguments": {"tool_name": names[tool], "arguments": arguments} if virtual else arguments,
"guardrails": selected,
},
)
calls = tuple(item for item in peer.drain() if item["body"].get("method") == "tools/call")
if expected is None:
assert response.status_code == 400, response.text
assert "integration resolved add denied" in response.text, response.text
assert calls == (), "pre-call denial must prevent upstream execution"
else:
assert response.status_code == 200, response.text
assert response.json()["isError"] is False
assert response.json()["content"][0]["text"] == str(expected), response.text
assert len(calls) == 1
assert calls[0]["body"]["params"]["name"] == tool
assert calls[0]["body"]["params"]["arguments"] == arguments
Loading
Loading