feat(unit2/smolagents): add MCP server tool collection example
#140
by ChamMach - opened
- unit2/smolagents/tools.ipynb +59 -0
unit2/smolagents/tools.ipynb
CHANGED
|
@@ -1217,6 +1217,65 @@
|
|
| 1217 |
"agent.run(\"Search for luxury entertainment ideas for a superhero-themed event, such as live performances and interactive experiences.\")"
|
| 1218 |
]
|
| 1219 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1220 |
{
|
| 1221 |
"cell_type": "markdown",
|
| 1222 |
"source": [
|
|
|
|
| 1217 |
"agent.run(\"Search for luxury entertainment ideas for a superhero-themed event, such as live performances and interactive experiences.\")"
|
| 1218 |
]
|
| 1219 |
},
|
| 1220 |
+
{
|
| 1221 |
+
"cell_type": "markdown",
|
| 1222 |
+
"metadata": {
|
| 1223 |
+
"id": "OhbVrpoiVgRV"
|
| 1224 |
+
},
|
| 1225 |
+
"source": [
|
| 1226 |
+
"## Importing a Tool Collection from Any MCP Server\n",
|
| 1227 |
+
"\n",
|
| 1228 |
+
"`smolagents` also allows importing tools from the hundreds of MCP servers available on [glama.ai](https://glama.ai/mcp/servers) or [smithery.ai](https://smithery.ai). If you want to dive deeper about MCP, you can check our [free MCP Course](https://huggingface.co/learn/mcp-course/).\n",
|
| 1229 |
+
"\n",
|
| 1230 |
+
"We first need to install the `mcp` integration for `smolagents`."
|
| 1231 |
+
]
|
| 1232 |
+
},
|
| 1233 |
+
{
|
| 1234 |
+
"cell_type": "code",
|
| 1235 |
+
"execution_count": null,
|
| 1236 |
+
"metadata": {
|
| 1237 |
+
"id": "5IfLBcbfnoGM"
|
| 1238 |
+
},
|
| 1239 |
+
"outputs": [],
|
| 1240 |
+
"source": [
|
| 1241 |
+
"!pip install \"smolagents[mcp]\""
|
| 1242 |
+
]
|
| 1243 |
+
},
|
| 1244 |
+
{
|
| 1245 |
+
"cell_type": "markdown",
|
| 1246 |
+
"metadata": {
|
| 1247 |
+
"id": "bJmTPSIAoCLr"
|
| 1248 |
+
},
|
| 1249 |
+
"source": [
|
| 1250 |
+
"The MCP servers tools can be loaded in a `ToolCollection` object as follows. Alfred can, for instance, hook his agent straight into a PubMed-based MCP server to make sure Gotham's elite have a remedy ready for the morning after the party:"
|
| 1251 |
+
]
|
| 1252 |
+
},
|
| 1253 |
+
{
|
| 1254 |
+
"cell_type": "code",
|
| 1255 |
+
"execution_count": null,
|
| 1256 |
+
"metadata": {
|
| 1257 |
+
"id": "Z3aWZkSBvrjn"
|
| 1258 |
+
},
|
| 1259 |
+
"outputs": [],
|
| 1260 |
+
"source": [
|
| 1261 |
+
"import os\n",
|
| 1262 |
+
"from smolagents import ToolCollection, CodeAgent\n",
|
| 1263 |
+
"from mcp import StdioServerParameters\n",
|
| 1264 |
+
"from smolagents import InferenceClientModel\n",
|
| 1265 |
+
"\n",
|
| 1266 |
+
"model = InferenceClientModel(\"Qwen/Qwen2.5-Coder-32B-Instruct\")\n",
|
| 1267 |
+
"\n",
|
| 1268 |
+
"server_parameters = StdioServerParameters(\n",
|
| 1269 |
+
" command=\"uvx\",\n",
|
| 1270 |
+
" args=[\"--quiet\", \"pubmedmcp@0.1.3\"],\n",
|
| 1271 |
+
" env={\"UV_PYTHON\": \"3.12\", **os.environ},\n",
|
| 1272 |
+
")\n",
|
| 1273 |
+
"\n",
|
| 1274 |
+
"with ToolCollection.from_mcp(server_parameters, trust_remote_code=True) as tool_collection:\n",
|
| 1275 |
+
" agent = CodeAgent(tools=[*tool_collection.tools], model=model, add_base_tools=True)\n",
|
| 1276 |
+
" agent.run(\"Please find a remedy for hangover.\")"
|
| 1277 |
+
]
|
| 1278 |
+
},
|
| 1279 |
{
|
| 1280 |
"cell_type": "markdown",
|
| 1281 |
"source": [
|