Files
cp1-agents/mcp-servers/_common/test_knowledge.py
Jonathan Boniface f659564208 Platform scripts, MCP servers, deployment and docs for the KAB runs
- Scripts to run workflows from a YAML context or run inputs (kab_run_workflow.py, with
  --input name=@file), store per-project discovery connections, bootstrap agent MCP keys,
  refresh the KAB token, publish reports and build estate snapshots.
- Hyperscaler discovery servers extended in place; cloud-knowledge folded into them.
- deploy/crucible-kab-dev.yaml: the Discovery package agent and the Assessment agents.
- Platform issue write-ups and current-wiring notes under docs/platform-issues/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 22:13:25 +00:00

105 lines
4.1 KiB
Python

"""`<provider>_search_well_architected`, for every discovery server that offers it. Run with SERVER_DIR set, as
test_contract.py is; a server without the tool skips. Moved from the retired cloud-knowledge server on 2026-11-04."""
import asyncio
import json
import os
import sys
from pathlib import Path
import httpx
import pytest
SERVER_DIR = Path(__file__).resolve().parents[1] / os.environ["SERVER_DIR"]
sys.path.insert(0, str(SERVER_DIR))
import mcp_connection # noqa: E402
import server # noqa: E402
PROVIDER = SERVER_DIR.name.split("-")[0]
TOOL = f"{PROVIDER}_search_well_architected"
VENDOR_TOOL = {"azure": "microsoft_docs_search", "gcp": "search_documents", "aws": "aws___search_documentation"}
CONNECTIONS = {"azure": {"tenant_id": "t", "client_id": "c", "client_secret": "s", "scope_root": "subscriptions/sub-1"},
"gcp": {"scope_root": "projects/p1", "access_token": "tok"}}
if not hasattr(server, TOOL):
pytest.skip(f"{SERVER_DIR.name} has no {TOOL}", allow_module_level=True)
class Vendor:
def __init__(self, tool=VENDOR_TOOL[PROVIDER], argument="query"):
self.tool, self.argument, self.sent, self.headers = tool, argument, [], []
def __call__(self, request):
body = json.loads(request.content)
self.sent.append(body)
self.headers.append(dict(request.headers))
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):
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(CONNECTIONS[PROVIDER])
try:
return await getattr(server, TOOL)(arguments)
finally:
mcp_connection.unbind(bound)
server.get_upstream_client = original
await client.aclose()
return asyncio.run(run())
GOOD = {"pillar": "reliability", "topic": "landing zone"}
def test_search_returns_cited_guidance():
data = call(GOOD, Vendor())["data"]
assert data["provider"] == PROVIDER
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()
call({**GOOD, "customer": "Meridian Retail", "extra": "pos-db-01"}, vendor)
sent = json.dumps(vendor.sent)
assert f"{server.KNOWLEDGE_SERVERS[PROVIDER]['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()
result = call({**GOOD, "topic": "Meridian Retail pos-db-01 outage"}, vendor)
assert result["success"] is False and not vendor.sent
def test_unknown_pillar_is_refused():
vendor = Vendor()
assert call({**GOOD, "pillar": "x"}, vendor)["success"] is False and not vendor.sent
def test_the_vendors_argument_name_is_read_from_the_tool():
vendor = Vendor(argument="search_phrase")
call(GOOD, vendor)
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
def test_gcp_search_bills_the_connections_project():
if PROVIDER != "gcp":
pytest.skip("GCP only")
vendor = Vendor()
call(GOOD, vendor)
assert all(h.get("authorization") == "Bearer tok" and h.get("x-goog-user-project") == "p1" for h in vendor.headers)