created local gradio clients. but should ideally be hosted on HF spaces
Browse files- mcp_client.py +37 -0
- mcp_client_template.py +15 -0
mcp_client.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from mcp import StdioServerParameters
|
4 |
+
from smolagents import InferenceClientModel, CodeAgent, ToolCollection, MCPClient
|
5 |
+
|
6 |
+
|
7 |
+
if __name__ == "__main__":
|
8 |
+
|
9 |
+
try:
|
10 |
+
mcp_client = MCPClient(
|
11 |
+
{"url": "https://abidlabs-mcp-tools2.hf.space/gradio_api/mcp/sse", "transport": "sse"}) # TODO: Change this to my HF space
|
12 |
+
tools = mcp_client.get_tools()
|
13 |
+
|
14 |
+
print("\n".join(f"{t.name}: {t.description}" for t in tools))
|
15 |
+
|
16 |
+
# Define model
|
17 |
+
model = InferenceClientModel(
|
18 |
+
model_id="Qwen/Qwen2.5-32B-Instruct",
|
19 |
+
token=os.getenv("HF_TOKEN")
|
20 |
+
)
|
21 |
+
|
22 |
+
agent = CodeAgent(tools=[*tools], model=model)
|
23 |
+
|
24 |
+
run_inference = False # TEMP
|
25 |
+
if run_inference:
|
26 |
+
|
27 |
+
chat_interface = gr.ChatInterface(
|
28 |
+
fn=lambda message, history: str(agent.run(message)),
|
29 |
+
type="messages",
|
30 |
+
examples=["Prime factorization of 68"],
|
31 |
+
title="Agent with MCP Tools",
|
32 |
+
description="This is a simple agent that uses MCP tools to answer questions."
|
33 |
+
)
|
34 |
+
|
35 |
+
chat_interface.launch()
|
36 |
+
finally:
|
37 |
+
mcp_client.close()
|
mcp_client_template.py
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents.mcp_client import MCPClient
|
2 |
+
|
3 |
+
|
4 |
+
mcp_client = MCPClient(
|
5 |
+
{"url": "https://abidlabs-mcp-tools2.hf.space/gradio_api/mcp/sse", "transport": "sse"}
|
6 |
+
)
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
if __name__ == "__main__":
|
12 |
+
|
13 |
+
# List all available tools from the MCP server
|
14 |
+
with mcp_client as tools:
|
15 |
+
print("\n".join(f"{t.name}: {t.description}" for t in tools))
|