import asyncio import json import sys from pathlib import Path import httpx sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import mcp_connection # noqa: E402 import server # noqa: E402 class Vendor: def __init__(self, tool, argument="query"): self.tool, self.argument, self.sent = tool, argument, [] def __call__(self, request): body = json.loads(request.content) self.sent.append(body) if body["method"] == "initialize": return httpx.Response(200, headers={"mcp-session-id": "s1"}, json={"result": {}}) if body["method"] == "tools/list": return httpx.Response(200, json={"result": {"tools": [{"name": self.tool, "inputSchema": { "required": [self.argument], "properties": {self.argument: {"type": "string"}}}}]}}) text = json.dumps({"results": [{"title": "Reliability", "contentUrl": "https://example.test/r", "content": "Guidance"}]}) payload = json.dumps({"result": {"content": [{"type": "text", "text": text}]}}) return httpx.Response(200, headers={"content-type": "text/event-stream"}, text=f"event: message\ndata: {payload}\n\n") def call(arguments, vendor, connection=None): async def run(): client = httpx.AsyncClient(transport=httpx.MockTransport(vendor)) original = server.get_upstream_client server.get_upstream_client = lambda kind, build: client bound = mcp_connection.bind(connection or {"google_api_key": "k"}) try: return await server.cloud_search_well_architected(arguments) finally: mcp_connection.unbind(bound) server.get_upstream_client = original await client.aclose() return asyncio.run(run()) GOOD = {"provider": "azure", "pillar": "reliability", "topic": "landing zone"} def test_search_returns_cited_guidance(): data = call(GOOD, Vendor("microsoft_docs_search"))["data"] assert data["results"][0]["url"] == "https://example.test/r" and data["evidence_ref"].startswith("sha256:") def test_query_is_built_only_from_fixed_lists(): vendor = Vendor("microsoft_docs_search") call({**GOOD, "customer": "Meridian Retail", "extra": "pos-db-01"}, vendor) sent = json.dumps(vendor.sent) assert "Azure Well-Architected Framework reliability landing zone" in sent assert "Meridian" not in sent and "pos-db-01" not in sent def test_free_text_topic_never_leaves(): vendor = Vendor("microsoft_docs_search") result = call({**GOOD, "topic": "Meridian Retail pos-db-01 outage"}, vendor) assert result["success"] is False and not vendor.sent def test_unknown_pillar_and_provider_are_refused(): vendor = Vendor("microsoft_docs_search") assert call({**GOOD, "pillar": "x"}, vendor)["success"] is False assert call({**GOOD, "provider": "x"}, vendor)["success"] is False assert not vendor.sent def test_the_vendors_argument_name_is_read_from_the_tool(): vendor = Vendor("aws___search_documentation", "search_phrase") connection = {"aws_access_key_id": "a", "aws_secret_access_key": "b"} call({**GOOD, "provider": "aws"}, vendor, connection) assert "search_phrase" in vendor.sent[-1]["params"]["arguments"] def test_a_vendor_dropping_its_tool_is_a_clean_failure(): assert call(GOOD, Vendor("something_else"))["success"] is False