Update agents/mcp/mcp_client.py
Browse files- agents/mcp/mcp_client.py +14 -3
agents/mcp/mcp_client.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
from mcp.client.sse import sse_client
|
| 3 |
from mcp.client.session import ClientSession, DEFAULT_CLIENT_INFO
|
| 4 |
-
import anyio
|
| 5 |
|
| 6 |
MCP_SSE_URL_CHESS_EVALUATION = "https://bstraehle-chess.hf.space/gradio_api/mcp/sse"
|
| 7 |
MCP_TOOL_CHESS_EVALUATION = "chess_evaluation"
|
|
@@ -54,4 +53,16 @@ def call_mcp_tool(mcp_url: str, tool_name: str, arguments: dict, timeout: int =
|
|
| 54 |
Raises:
|
| 55 |
Exception: If the MCP call fails
|
| 56 |
"""
|
| 57 |
-
return asyncio.run(call_mcp_tool_async(mcp_url, tool_name, arguments, timeout))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import anyio, asyncio, concurrent.futures
|
| 2 |
from mcp.client.sse import sse_client
|
| 3 |
from mcp.client.session import ClientSession, DEFAULT_CLIENT_INFO
|
|
|
|
| 4 |
|
| 5 |
MCP_SSE_URL_CHESS_EVALUATION = "https://bstraehle-chess.hf.space/gradio_api/mcp/sse"
|
| 6 |
MCP_TOOL_CHESS_EVALUATION = "chess_evaluation"
|
|
|
|
| 53 |
Raises:
|
| 54 |
Exception: If the MCP call fails
|
| 55 |
"""
|
| 56 |
+
#return asyncio.run(call_mcp_tool_async(mcp_url, tool_name, arguments, timeout))
|
| 57 |
+
try:
|
| 58 |
+
loop = asyncio.get_event_loop()
|
| 59 |
+
|
| 60 |
+
if loop.is_running():
|
| 61 |
+
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 62 |
+
future = executor.submit(asyncio.run, call_mcp_tool_async(mcp_url, tool_name, arguments, timeout))
|
| 63 |
+
|
| 64 |
+
return future.result()
|
| 65 |
+
else:
|
| 66 |
+
return loop.run_until_complete(call_mcp_tool_async(mcp_url, tool_name, arguments, timeout))
|
| 67 |
+
except RuntimeError:
|
| 68 |
+
return asyncio.run(call_mcp_tool_async(mcp_url, tool_name, arguments, timeout))
|