Eddyhzd commited on
Commit
2d000cd
·
1 Parent(s): e477df9
Files changed (1) hide show
  1. app.py +8 -38
app.py CHANGED
@@ -1,43 +1,13 @@
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(call_mcp, inputs=[tool_input, text_input], outputs=output)
42
-
43
- demo.launch()
 
1
  import asyncio
2
+ from mcp.client import MCPClient
 
 
 
3
 
4
  MCP_SERVER_URL = "https://hackathoncra-gradio-mcp.hf.space/gradio_api/mcp/"
5
 
6
+ async def list_tools():
7
+ client = await MCPClient.connect(MCP_SERVER_URL)
8
+ tools = await client.list_tools()
9
+ print("Tools disponibles sur le serveur MCP :")
10
+ for tool in tools:
11
+ print(f"- {tool['name']} : {tool.get('description', '')}")
 
12
 
13
+ asyncio.run(list_tools())