Spaces:
Runtime error
Runtime error
Eddyhzd
commited on
Commit
·
674e815
1
Parent(s):
f344af7
test
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ from openai import OpenAI
|
|
| 3 |
import os
|
| 4 |
from mcp import ClientSession, StdioServerParameters
|
| 5 |
from mcp.client.stdio import stdio_client
|
|
|
|
|
|
|
| 6 |
|
| 7 |
cle_api = os.environ.get("CLE_API_MISTRAL")
|
| 8 |
|
|
@@ -30,33 +32,55 @@ def chatbot(message, history):
|
|
| 30 |
return history, history
|
| 31 |
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
stdio_transport = self.exit_stack.enter_async_context(stdio_client(server_params))
|
| 42 |
-
self.stdio, self.write = stdio_transport
|
| 43 |
-
|
| 44 |
-
self.session = self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))
|
| 45 |
-
self.session.initialize()
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
"name": tool.name,
|
| 50 |
-
"description": tool.description,
|
| 51 |
-
"input_schema": tool.inputSchema
|
| 52 |
-
} for tool in response.tools]
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
chatbot_ui = gr.Chatbot(label="ChatBot")
|
| 62 |
msg = gr.Textbox(placeholder="Écrivez un message...")
|
|
|
|
| 3 |
import os
|
| 4 |
from mcp import ClientSession, StdioServerParameters
|
| 5 |
from mcp.client.stdio import stdio_client
|
| 6 |
+
import asyncio
|
| 7 |
+
from contextlib import AsyncExitStack
|
| 8 |
|
| 9 |
cle_api = os.environ.get("CLE_API_MISTRAL")
|
| 10 |
|
|
|
|
| 32 |
return history, history
|
| 33 |
|
| 34 |
|
| 35 |
+
loop = asyncio.new_event_loop()
|
| 36 |
+
asyncio.set_event_loop(loop)
|
| 37 |
+
|
| 38 |
+
class MCPClientWrapper:
|
| 39 |
+
def __init__(self):
|
| 40 |
+
self.session = None
|
| 41 |
+
self.exit_stack = None
|
| 42 |
+
self.tools = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
def connect(self, server_path: str) -> str:
|
| 45 |
+
return loop.run_until_complete(self._connect(server_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
async def _connect(self, server_path: str) -> str:
|
| 48 |
+
if self.exit_stack:
|
| 49 |
+
await self.exit_stack.aclose()
|
| 50 |
+
|
| 51 |
+
self.exit_stack = AsyncExitStack()
|
| 52 |
+
|
| 53 |
+
is_python = server_path.endswith('.py')
|
| 54 |
+
command = "python" if is_python else "node"
|
| 55 |
+
|
| 56 |
+
server_params = StdioServerParameters(
|
| 57 |
+
command=command,
|
| 58 |
+
args=[server_path],
|
| 59 |
+
env={"PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1"}
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
stdio_transport = await self.exit_stack.enter_async_context(stdio_client(server_params))
|
| 63 |
+
self.stdio, self.write = stdio_transport
|
| 64 |
+
|
| 65 |
+
self.session = await self.exit_stack.enter_async_context(ClientSession(self.stdio, self.write))
|
| 66 |
+
await self.session.initialize()
|
| 67 |
+
|
| 68 |
+
response = await self.session.list_tools()
|
| 69 |
+
self.tools = [{
|
| 70 |
+
"name": tool.name,
|
| 71 |
+
"description": tool.description,
|
| 72 |
+
"input_schema": tool.inputSchema
|
| 73 |
+
} for tool in response.tools]
|
| 74 |
+
|
| 75 |
+
tool_names = [tool["name"] for tool in self.tools]
|
| 76 |
+
return f"Connected to MCP server. Available tools: {', '.join(tool_names)}"
|
| 77 |
+
|
| 78 |
+
client = MCPClientWrapper()
|
| 79 |
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
|
| 82 |
+
client.connect("mcp_server.py")
|
| 83 |
+
gr.Markdown(f"Connected to MCP server. Available tools: {', '.join([tool['name'] for tool in client.tools])}")
|
| 84 |
|
| 85 |
chatbot_ui = gr.Chatbot(label="ChatBot")
|
| 86 |
msg = gr.Textbox(placeholder="Écrivez un message...")
|