Jarisko
commited on
Commit
·
2b875c2
1
Parent(s):
2a169e3
added some smolagents implementations
Browse files- smolagents_client.py +33 -0
- smolagents_mcp.ipynb +86 -0
- test.ipynb +12 -29
smolagents_client.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from mcp.client.stdio import StdioServerParameters
|
| 4 |
+
from smolagents import ToolCollection, CodeAgent
|
| 5 |
+
from smolagents import CodeAgent, InferenceClientModel
|
| 6 |
+
from smolagents.mcp_client import MCPClient
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
mcp_client = MCPClient(
|
| 10 |
+
# {"url": "https://abidlabs-mcp-tools.hf.space/gradio_api/mcp/sse"}
|
| 11 |
+
{"url": "http://127.0.0.1:7860/gradio_api/mcp/sse"}
|
| 12 |
+
)
|
| 13 |
+
tools = mcp_client.get_tools()
|
| 14 |
+
|
| 15 |
+
model = InferenceClientModel()
|
| 16 |
+
agent = CodeAgent(tools=[*tools], model=model)
|
| 17 |
+
|
| 18 |
+
def call_agent(message, history):
|
| 19 |
+
return str(agent.run(message))
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
demo = gr.ChatInterface(
|
| 23 |
+
fn=lambda message, history: str(agent.run(message)),
|
| 24 |
+
type="messages",
|
| 25 |
+
examples=["Prime factorization of 68"],
|
| 26 |
+
title="Agent with MCP Tools",
|
| 27 |
+
description="This is a simple agent that uses MCP tools to answer questions.",
|
| 28 |
+
messages=[],
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
demo.launch()
|
| 32 |
+
finally:
|
| 33 |
+
mcp_client.close()
|
smolagents_mcp.ipynb
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "ea8bce22",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Smolagents MCP"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "markdown",
|
| 13 |
+
"id": "27866a1d",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": [
|
| 16 |
+
"## stdio"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": null,
|
| 22 |
+
"id": "9d1f546d",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [],
|
| 25 |
+
"source": [
|
| 26 |
+
"from smolagents import ToolCollection, CodeAgent\n",
|
| 27 |
+
"from mcp.client.stdio import StdioServerParameters\n",
|
| 28 |
+
"from smolagents import CodeAgent, InferenceClientModel\n",
|
| 29 |
+
"\n",
|
| 30 |
+
"model = InferenceClientModel()\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"server_parameters = StdioServerParameters(command=\"uv\", args=[\"run\", \"server.py\"])\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"with ToolCollection.from_mcp(\n",
|
| 35 |
+
" server_parameters, trust_remote_code=True\n",
|
| 36 |
+
") as tool_collection:\n",
|
| 37 |
+
" agent = CodeAgent(tools=[*tool_collection.tools], model=model)\n",
|
| 38 |
+
" agent.run(\"What's the weather in Tokyo?\")"
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"cell_type": "markdown",
|
| 43 |
+
"id": "bc48b7f1",
|
| 44 |
+
"metadata": {},
|
| 45 |
+
"source": [
|
| 46 |
+
"## SSE"
|
| 47 |
+
]
|
| 48 |
+
},
|
| 49 |
+
{
|
| 50 |
+
"cell_type": "code",
|
| 51 |
+
"execution_count": null,
|
| 52 |
+
"id": "84aa4b6b",
|
| 53 |
+
"metadata": {},
|
| 54 |
+
"outputs": [],
|
| 55 |
+
"source": [
|
| 56 |
+
"from smolagents.mcp_client import MCPClient\n",
|
| 57 |
+
"\n",
|
| 58 |
+
"with MCPClient(\n",
|
| 59 |
+
" {\"url\": \"https://jarisko-mcp-server-deployment-sample.hf.space/gradio_api/mcp/sse\"}\n",
|
| 60 |
+
") as tools:\n",
|
| 61 |
+
" print(\"\\n\".join(f\"{t.name}: {t.description}\" for t in tools))"
|
| 62 |
+
]
|
| 63 |
+
}
|
| 64 |
+
],
|
| 65 |
+
"metadata": {
|
| 66 |
+
"kernelspec": {
|
| 67 |
+
"display_name": ".venv",
|
| 68 |
+
"language": "python",
|
| 69 |
+
"name": "python3"
|
| 70 |
+
},
|
| 71 |
+
"language_info": {
|
| 72 |
+
"codemirror_mode": {
|
| 73 |
+
"name": "ipython",
|
| 74 |
+
"version": 3
|
| 75 |
+
},
|
| 76 |
+
"file_extension": ".py",
|
| 77 |
+
"mimetype": "text/x-python",
|
| 78 |
+
"name": "python",
|
| 79 |
+
"nbconvert_exporter": "python",
|
| 80 |
+
"pygments_lexer": "ipython3",
|
| 81 |
+
"version": "3.11.6"
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
"nbformat": 4,
|
| 85 |
+
"nbformat_minor": 5
|
| 86 |
+
}
|
test.ipynb
CHANGED
|
@@ -1,22 +1,19 @@
|
|
| 1 |
{
|
| 2 |
"cells": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
"execution_count": null,
|
| 6 |
-
"id": "
|
| 7 |
"metadata": {},
|
| 8 |
-
"outputs": [
|
| 9 |
-
{
|
| 10 |
-
"name": "stderr",
|
| 11 |
-
"output_type": "stream",
|
| 12 |
-
"text": [
|
| 13 |
-
"c:\\Users\\jaros\\Documents\\Python\\mcp_server_deployment_sample\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 14 |
-
" from .autonotebook import tqdm as notebook_tqdm\n",
|
| 15 |
-
"Error in post_writer: Client error '404 Not Found' for url 'http://127.0.0.1:7860/gradio_api/mcp/gradio_api/mcp/messages/?session_id=da1cf9f12be04584ba0d3f8cbf880871'\n",
|
| 16 |
-
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404\n"
|
| 17 |
-
]
|
| 18 |
-
}
|
| 19 |
-
],
|
| 20 |
"source": [
|
| 21 |
"import gradio as gr\n",
|
| 22 |
"\n",
|
|
@@ -26,7 +23,7 @@
|
|
| 26 |
"from smolagents.mcp_client import MCPClient\n",
|
| 27 |
"\n",
|
| 28 |
"mcp_client = MCPClient(\n",
|
| 29 |
-
" {\"url\": \"
|
| 30 |
")\n",
|
| 31 |
"tools = mcp_client.get_tools()\n",
|
| 32 |
"print(tools)"
|
|
@@ -34,22 +31,8 @@
|
|
| 34 |
}
|
| 35 |
],
|
| 36 |
"metadata": {
|
| 37 |
-
"kernelspec": {
|
| 38 |
-
"display_name": ".venv",
|
| 39 |
-
"language": "python",
|
| 40 |
-
"name": "python3"
|
| 41 |
-
},
|
| 42 |
"language_info": {
|
| 43 |
-
"
|
| 44 |
-
"name": "ipython",
|
| 45 |
-
"version": 3
|
| 46 |
-
},
|
| 47 |
-
"file_extension": ".py",
|
| 48 |
-
"mimetype": "text/x-python",
|
| 49 |
-
"name": "python",
|
| 50 |
-
"nbconvert_exporter": "python",
|
| 51 |
-
"pygments_lexer": "ipython3",
|
| 52 |
-
"version": "3.11.6"
|
| 53 |
}
|
| 54 |
},
|
| 55 |
"nbformat": 4,
|
|
|
|
| 1 |
{
|
| 2 |
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "318579ae",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Test tools"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
{
|
| 12 |
"cell_type": "code",
|
| 13 |
"execution_count": null,
|
| 14 |
+
"id": "0de3b792",
|
| 15 |
"metadata": {},
|
| 16 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"source": [
|
| 18 |
"import gradio as gr\n",
|
| 19 |
"\n",
|
|
|
|
| 23 |
"from smolagents.mcp_client import MCPClient\n",
|
| 24 |
"\n",
|
| 25 |
"mcp_client = MCPClient(\n",
|
| 26 |
+
" {\"url\": \"https://jarisko-mcp-server-deployment-sample.hf.space/gradio_api/mcp/sse\"}\n",
|
| 27 |
")\n",
|
| 28 |
"tools = mcp_client.get_tools()\n",
|
| 29 |
"print(tools)"
|
|
|
|
| 31 |
}
|
| 32 |
],
|
| 33 |
"metadata": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
"language_info": {
|
| 35 |
+
"name": "python"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
},
|
| 38 |
"nbformat": 4,
|