Minimal MCP-over-HTTP adapter: - POST {MCP_HTTP_ENDPOINT}/tools/execute with {name, arguments} - Returns JSON string. Compatible with servers exposing MCP-like HTTP interface.
(name: str, params: Dict[str, Any])
| 13 | |
| 14 | |
| 15 | async def execute_mcp_tool(name: str, params: Dict[str, Any]) -> str: |
| 16 | """ |
| 17 | Minimal MCP-over-HTTP adapter: |
| 18 | - POST {MCP_HTTP_ENDPOINT}/tools/execute with {name, arguments} |
| 19 | - Returns JSON string. |
| 20 | Compatible with servers exposing MCP-like HTTP interface. |
| 21 | """ |
| 22 | ep = os.environ.get("MCP_HTTP_ENDPOINT") |
| 23 | if not ep: |
| 24 | raise RuntimeError("MCP_HTTP_ENDPOINT not configured") |
| 25 | url = f"{_normalize_endpoint(ep)}/tools/execute" |
| 26 | payload = {"name": name, "arguments": params} |
| 27 | headers = {"Content-Type": "application/json"} |
| 28 | timeout = float(os.environ.get("MCP_HTTP_TIMEOUT", "15")) |
| 29 | async with httpx.AsyncClient(timeout=timeout) as client: |
| 30 | resp = await client.post(url, json=payload, headers=headers) |
| 31 | resp.raise_for_status() |
| 32 | try: |
| 33 | data = resp.json() |
| 34 | except asyncio.CancelledError: |
| 35 | raise |
| 36 | except Exception: |
| 37 | data = {"raw": resp.text} |
| 38 | return json.dumps(data, ensure_ascii=False) |
| 39 | |
| 40 | |
| 41 | async def execute_mcp_tool_with_endpoint( |