Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
63 changes: 62 additions & 1 deletion tests/e2e/mcp/mcp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from pydantic import BaseModel, ConfigDict, Field, RootModel

from e2e_http import Headers, NoBody, Result, Success, unwrap
from e2e_http import Headers, NoBody, Result, Success, UnknownApiError, unwrap
from models import KeyGenerateBody, ObjectPermission
from proxy_client import ProxyClient

Expand Down Expand Up @@ -270,6 +270,59 @@ def await_tool(self, key: str, server_id: str, needle: str) -> str:
)
time.sleep(self.proxy.poll_interval)

def await_call_tool(
self,
key: str,
*,
server_id: str,
name: str,
arguments: McpToolArguments,
) -> McpCallToolResponse:
"""Poll tools/call until the result is not a multi-worker 'tool not found' miss.

Retries only when the data plane answers 500 with a tool/server not-found
body (cold worker). Any other success or error is returned immediately.
"""
deadline = time.monotonic() + self.proxy.poll_timeout
last: Result[McpCallToolResponse] | None = None
while True:
last = self.call_tool(key, server_id=server_id, name=name, arguments=arguments)
if not _is_mcp_not_synced(last):
return unwrap(last)
if time.monotonic() >= deadline:
raise AssertionError(
f"tools/call for {name!r} on server {server_id} still missing on the "
f"data plane after {self.proxy.poll_timeout}s (multi-worker registry lag); "
f"last result: {last}"
)
time.sleep(self.proxy.poll_interval)

def await_call_tool_denied(
self,
key: str,
*,
server_id: str,
name: str,
arguments: McpToolArguments,
) -> UnknownApiError:
"""Poll tools/call until a cold-worker miss clears and the call is 403 access_denied."""
deadline = time.monotonic() + self.proxy.poll_timeout
last: Result[McpCallToolResponse] | None = None
while True:
last = self.call_tool(key, server_id=server_id, name=name, arguments=arguments)
if isinstance(last, UnknownApiError) and last.status_code == 403:
return last
if not _is_mcp_not_synced(last):
raise AssertionError(
f"ungranted key's tools/call was not 403 access_denied: {last}"
)
if time.monotonic() >= deadline:
raise AssertionError(
f"ungranted key never got 403 for {name!r} within {self.proxy.poll_timeout}s; "
f"last result: {last}"
)
time.sleep(self.proxy.poll_interval)

def register_mcp_content_filter(self, *, name: str, blocked_keyword: str) -> str:
"""Register a default-on content-filter guardrail that runs on the MCP
tool-call hook (pre_mcp_call) and blocks a single keyword. The keyword is
Expand Down Expand Up @@ -317,5 +370,13 @@ def call_tool(
)


def _is_mcp_not_synced(result: Result[McpCallToolResponse]) -> bool:
"""True when the serving worker does not know the MCP server/tool yet."""
if not isinstance(result, UnknownApiError) or result.status_code != 500:
return False
body = result.body.lower()
return "not found" in body and ("tool" in body or "server" in body)
Comment thread
mubashir1osmani marked this conversation as resolved.
Resolved


def build_client(proxy: ProxyClient) -> McpClient:
return McpClient(proxy=proxy)
1 change: 1 addition & 0 deletions tests/e2e/mcp/test_mcp_access_group_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_access_group_scopes_tool_selection(
) -> None:
group = f"e2e-mcp-grp-{unique_marker()}"
server_id = register_datadog_mcp(client, resources, mcp_access_groups=[group])
client.await_registered(server_id)

granted = client.generate_key(
user_id=f"e2e-mcp-ag-granted-{unique_marker()}",
Expand Down
28 changes: 13 additions & 15 deletions tests/e2e/mcp/test_mcp_datadog_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def test_search_logs_finds_seeded_completion(
_assert_datadog_logger_active(client.proxy)

server_id = register_datadog_mcp(client, resources)
client.await_registered(server_id)
marker = f"{MARKER_PREFIX}{unique_marker()}"

key = client.generate_key(
Expand All @@ -78,22 +79,19 @@ def test_search_logs_finds_seeded_completion(
)

tool_name = client.await_tool(key, server_id, SEARCH_LOGS_TOOL)

call = unwrap(
client.call_tool(
key,
server_id=server_id,
name=tool_name,
arguments={
"query": marker,
"from": DD_SEARCH_FROM,
"to": "now",
"max_tokens": 5000,
"telemetry": {
"intent": "e2e assert seeded litellm completion log is searchable via MCP"
},
call = client.await_call_tool(
key,
server_id=server_id,
name=tool_name,
arguments={
"query": marker,
"from": DD_SEARCH_FROM,
"to": "now",
"max_tokens": 5000,
"telemetry": {
"intent": "e2e assert seeded litellm completion log is searchable via MCP"
},
)
},
)
assert call.is_error is not True, f"search_datadog_logs errored: {call}"
body = call.all_text
Expand Down
15 changes: 7 additions & 8 deletions tests/e2e/mcp/test_mcp_key_access_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from datadog_mcp import SEARCH_LOGS_TOOL, register_datadog_mcp
from e2e_config import DD_SEARCH_FROM, unique_marker
from e2e_http import UnknownApiError, unwrap
from e2e_http import unwrap
from lifecycle import ResourceManager
from mcp_client import McpClient

Expand Down Expand Up @@ -72,13 +72,12 @@ def test_call_tool_denied_without_permission(
"max_tokens": 1000,
"telemetry": {"intent": "e2e control call proving granted key can invoke Datadog MCP"},
}
permitted_call = unwrap(
client.call_tool(permitted_key, server_id=server_id, name=tool_name, arguments=search_args)
permitted_call = client.await_call_tool(
permitted_key, server_id=server_id, name=tool_name, arguments=search_args
)
assert permitted_call.is_error is not True, f"granted key's tool call errored: {permitted_call}"

match client.call_tool(denied_key, server_id=server_id, name=tool_name, arguments=search_args):
case UnknownApiError(status_code=403, body=body):
assert "access_denied" in body, f"403 was not an MCP access denial: {body}"
case other:
pytest.fail(f"ungranted key's tool call was not refused with 403 access_denied: {other}")
denied = client.await_call_tool_denied(
denied_key, server_id=server_id, name=tool_name, arguments=search_args
)
assert "access_denied" in denied.body, f"403 was not an MCP access denial: {denied.body}"
Loading