Spaces:
Running
Running
File size: 942 Bytes
f26de06 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# services/mcp_client.py
import requests
from typing import Any, Dict, List
from config.settings import Settings
class MCPClient:
def __init__(self, base_url: str | None = None):
settings = Settings()
self.base_url = (base_url or settings.mcp_server_url).rstrip("/")
def list_tools(self) -> List[Dict[str, Any]]:
resp = requests.get(f"{self.base_url}/tools", timeout=5)
resp.raise_for_status()
data = resp.json()
return data.get("tools", [])
def call_tool(self, name: str, arguments: Dict[str, Any] | None = None) -> Any:
payload = {"name": name, "arguments": arguments or {}}
resp = requests.post(f"{self.base_url}/tools/call", json=payload, timeout=30)
resp.raise_for_status()
data = resp.json()
if not data.get("success", False):
raise RuntimeError(data.get("error", "Unknown tool error"))
return data.get("result")
|