mcp_client.py aktualisiert
This commit is contained in:
+15
-19
@@ -1,25 +1,21 @@
|
|||||||
import asyncio
|
import requests
|
||||||
from mcp import ClientSession, StreamableHTTPTransport
|
import os
|
||||||
|
|
||||||
|
MCP_URL = os.getenv("MCP_URL")
|
||||||
|
|
||||||
|
|
||||||
class MCPClient:
|
class MCPClient:
|
||||||
|
|
||||||
def __init__(self, url: str):
|
def list_tools(self):
|
||||||
|
r = requests.get(f"{MCP_URL}/tools")
|
||||||
self.url = url
|
return r.json()
|
||||||
|
|
||||||
async def _call(self, tool, args):
|
|
||||||
|
|
||||||
transport = StreamableHTTPTransport(self.url)
|
|
||||||
|
|
||||||
async with ClientSession(transport) as session:
|
|
||||||
|
|
||||||
await session.initialize()
|
|
||||||
|
|
||||||
result = await session.call_tool(tool, args)
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def call_tool(self, tool, args):
|
def call_tool(self, tool, args):
|
||||||
|
r = requests.post(
|
||||||
return asyncio.run(self._call(tool, args))
|
f"{MCP_URL}/call",
|
||||||
|
json={
|
||||||
|
"tool": tool,
|
||||||
|
"arguments": args
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return r.json()
|
||||||
Reference in New Issue
Block a user