"""Secret-safe remote MCP protocol acceptance used before WorkBuddy sign-off.""" import asyncio import os import sys import httpx2 from mcp import ClientSession from mcp.client.streamable_http import streamable_http_client from mcp.types import CallToolResult async def _accept() -> None: """Discover exactly one approved tool and execute a real remote search.""" url = os.environ.get("EKP_MCP_URL", "") token = os.environ.get("EKP_MCP_TOKEN", "") query = os.environ.get("EKP_MCP_ACCEPTANCE_QUERY", "") if not url.startswith("https://") or len(token) < 32 or not query.strip(): raise ValueError( "EKP_MCP_URL (HTTPS), EKP_MCP_TOKEN and EKP_MCP_ACCEPTANCE_QUERY are required" ) # The credential is read in process and is never included in CLI arguments, # output, exceptions, or the checked-in WorkBuddy example configuration. headers = {"Authorization": f"Bearer {token}"} async with httpx2.AsyncClient(headers=headers, timeout=30.0) as client: async with streamable_http_client(url, http_client=client) as (read, write): async with ClientSession(read, write) as session: await session.initialize() tools = await session.list_tools() tool_names = [tool.name for tool in tools.tools] if tool_names != ["search_knowledge"]: raise RuntimeError("remote MCP exposed an unexpected tool set") result = await session.call_tool( "search_knowledge", {"query": query, "top_k": 1}, ) if not isinstance(result, CallToolResult) or result.is_error: raise RuntimeError("remote MCP search returned a tool error") structured = result.structured_content if not isinstance(structured, dict) or not structured.get("results"): raise RuntimeError("remote MCP search returned no structured source") print("remote MCP protocol acceptance passed: one tool, one grounded result") def main() -> int: """Return a stable non-zero code without reflecting sensitive exceptions.""" try: asyncio.run(_accept()) except Exception: print("remote MCP protocol acceptance failed", file=sys.stderr) return 1 return 0 if __name__ == "__main__": raise SystemExit(main())