Spaces:
Runtime error
Runtime error
Eddyhzd
commited on
Commit
·
df6c119
1
Parent(s):
c3b66c1
test
Browse files
app.py
CHANGED
|
@@ -1,27 +1,43 @@
|
|
| 1 |
import asyncio
|
|
|
|
| 2 |
from mcp.client.session import ClientSession
|
|
|
|
| 3 |
from mcp.client.stdio import stdio_client
|
| 4 |
-
from mcp.client.http import http_client
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# URL de ton MCP server (d'après ta config JSON)
|
| 8 |
-
mcp_url = "https://hackathoncra-gradio-mcp.hf.space/gradio_api/mcp/"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
session = ClientSession(read, write)
|
| 13 |
await session.initialize()
|
| 14 |
|
| 15 |
-
#
|
| 16 |
tools = await session.list_tools()
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
tool_name = tools[0].name
|
| 22 |
-
print(f"Appel de l'outil: {tool_name}")
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import asyncio
|
| 2 |
+
import gradio as gr
|
| 3 |
from mcp.client.session import ClientSession
|
| 4 |
+
from mcp.client.sse import sse_client
|
| 5 |
from mcp.client.stdio import stdio_client
|
|
|
|
| 6 |
|
| 7 |
+
MCP_SERVER_URL = "https://hackathoncra-gradio-mcp.hf.space/gradio_api/mcp/"
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
async def call_mcp(tool_name: str, input_data: str):
|
| 10 |
+
"""
|
| 11 |
+
Appelle un outil MCP depuis le serveur spécifié.
|
| 12 |
+
"""
|
| 13 |
+
async with sse_client(MCP_SERVER_URL) as (read, write):
|
| 14 |
session = ClientSession(read, write)
|
| 15 |
await session.initialize()
|
| 16 |
|
| 17 |
+
# Découvrir les outils disponibles
|
| 18 |
tools = await session.list_tools()
|
| 19 |
+
tool_names = [t.name for t in tools]
|
| 20 |
+
print("Outils MCP disponibles :", tool_names)
|
| 21 |
|
| 22 |
+
if tool_name not in tool_names:
|
| 23 |
+
return f"Outil {tool_name} non trouvé. Outils disponibles : {tool_names}"
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Appel du tool MCP
|
| 26 |
+
result = await session.call_tool(tool_name, {"input": input_data})
|
| 27 |
+
return result.content[0].text if result.content else "Pas de résultat"
|
| 28 |
|
| 29 |
+
# Fonction wrapper Gradio (synchrone)
|
| 30 |
+
def run_mcp(tool_name, text):
|
| 31 |
+
return asyncio.run(call_mcp(tool_name, text))
|
| 32 |
+
|
| 33 |
+
# Interface Gradio
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("### Client MCP branché sur Hugging Face + Gradio")
|
| 36 |
+
tool_input = gr.Textbox(label="Nom de l’outil MCP")
|
| 37 |
+
text_input = gr.Textbox(label="Texte à envoyer")
|
| 38 |
+
output = gr.Textbox(label="Résultat MCP")
|
| 39 |
+
|
| 40 |
+
run_button = gr.Button("Exécuter")
|
| 41 |
+
run_button.click(run_mcp, inputs=[tool_input, text_input], outputs=output)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|