Spaces:
Runtime error
Runtime error
Eddyhzd
commited on
Commit
·
c548a89
1
Parent(s):
7fdb083
test
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ from openai import OpenAI
|
|
| 3 |
import os
|
| 4 |
import asyncio
|
| 5 |
from contextlib import AsyncExitStack
|
| 6 |
-
from mcp import ClientSession
|
| 7 |
-
from mcp.client.
|
| 8 |
|
| 9 |
cle_api = os.environ.get("CLE_API_MISTRAL")
|
| 10 |
|
|
@@ -29,31 +29,30 @@ class MCPClientWrapper:
|
|
| 29 |
|
| 30 |
self.exit_stack = AsyncExitStack()
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
self.http, self.write = http_transport
|
| 37 |
-
|
| 38 |
-
self.session = await self.exit_stack.enter_async_context(ClientSession(self.http, self.write))
|
| 39 |
await self.session.initialize()
|
| 40 |
|
| 41 |
-
|
| 42 |
-
self.tools = [
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
return f"Connecté au MCP {server_url}. Outils disponibles : {', '.join(tool_names)}"
|
| 50 |
|
| 51 |
-
# Connexion au MCP HuggingFace
|
| 52 |
clientMCP = MCPClientWrapper()
|
| 53 |
print(clientMCP.connect("https://huggingface.co/spaces/HackathonCRA/mcp"))
|
| 54 |
print(clientMCP.tools)
|
| 55 |
|
| 56 |
-
|
| 57 |
# Chatbot
|
| 58 |
def chatbot(message, history):
|
| 59 |
# Préparer l’historique
|
|
|
|
| 3 |
import os
|
| 4 |
import asyncio
|
| 5 |
from contextlib import AsyncExitStack
|
| 6 |
+
from mcp import ClientSession
|
| 7 |
+
from mcp.client.streamable_http import streamablehttp_client
|
| 8 |
|
| 9 |
cle_api = os.environ.get("CLE_API_MISTRAL")
|
| 10 |
|
|
|
|
| 29 |
|
| 30 |
self.exit_stack = AsyncExitStack()
|
| 31 |
|
| 32 |
+
# Utiliser le transport HTTP streamable ou SSE selon ce qui est disponible
|
| 33 |
+
streams = await self.exit_stack.enter_async_context(streamablehttp_client(url=server_url))
|
| 34 |
+
# streams va typiquement donner une paire (transport_read, transport_write) ou similaire
|
| 35 |
+
self.http_read, self.http_write = streams
|
| 36 |
|
| 37 |
+
self.session = await self.exit_stack.enter_async_context(ClientSession(self.http_read, self.http_write))
|
|
|
|
|
|
|
|
|
|
| 38 |
await self.session.initialize()
|
| 39 |
|
| 40 |
+
tools_response = await self.session.list_tools()
|
| 41 |
+
self.tools = [
|
| 42 |
+
{
|
| 43 |
+
"name": t.name,
|
| 44 |
+
"description": t.description,
|
| 45 |
+
"input_schema": t.inputSchema
|
| 46 |
+
}
|
| 47 |
+
for t in tools_response.tools
|
| 48 |
+
]
|
| 49 |
+
tool_names = [t["name"] for t in self.tools]
|
| 50 |
return f"Connecté au MCP {server_url}. Outils disponibles : {', '.join(tool_names)}"
|
| 51 |
|
|
|
|
| 52 |
clientMCP = MCPClientWrapper()
|
| 53 |
print(clientMCP.connect("https://huggingface.co/spaces/HackathonCRA/mcp"))
|
| 54 |
print(clientMCP.tools)
|
| 55 |
|
|
|
|
| 56 |
# Chatbot
|
| 57 |
def chatbot(message, history):
|
| 58 |
# Préparer l’historique
|