| 39 | |
| 40 | |
| 41 | async def execute_mcp_tool_with_endpoint( |
| 42 | endpoint: str, name: str, params: Dict[str, Any] |
| 43 | ) -> str: |
| 44 | url = f"{_normalize_endpoint(endpoint)}/tools/execute" |
| 45 | payload = {"name": name, "arguments": params} |
| 46 | headers = {"Content-Type": "application/json"} |
| 47 | timeout = float(os.environ.get("MCP_HTTP_TIMEOUT", "15")) |
| 48 | async with httpx.AsyncClient(timeout=timeout) as client: |
| 49 | resp = await client.post(url, json=payload, headers=headers) |
| 50 | resp.raise_for_status() |
| 51 | try: |
| 52 | data = resp.json() |
| 53 | except asyncio.CancelledError: |
| 54 | raise |
| 55 | except Exception: |
| 56 | data = {"raw": resp.text} |
| 57 | return json.dumps(data, ensure_ascii=False) |