You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: units/en/unit1/mcp-clients.mdx
+88-91Lines changed: 88 additions & 91 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,6 +12,14 @@ In this section, you will:
12
12
* Discover how to use Hugging Face's MCP Client implementation
13
13
* See practical examples of MCP Client usage
14
14
15
+
<Tip>
16
+
17
+
In this page we're going to show examples of how to set up MCP Clients in a few different ways using the JSON notation. For now, we will use *examples* like `path/to/server.py` to represent the path to the MCP Server. In the next unit, we'll implement this with real MCP Servers.
18
+
19
+
For now, focus on understanding the MCP Client notation. We'll implement the MCP Servers in the next unit.
20
+
21
+
</Tip>
22
+
15
23
## Understanding MCP Clients
16
24
17
25
MCP Clients are crucial components that act as the bridge between AI applications (Hosts) and external capabilities provided by MCP Servers. Think of the Host as your main application (like an AI assistant or IDE) and the Client as a specialized module within that Host responsible for handling MCP communications.
@@ -52,6 +60,8 @@ Fortunately, the configuration files are very simple, easy to understand, and co
52
60
53
61
The standard configuration file for MCP is named `mcp.json`. Here's the basic structure:
54
62
63
+
This is the basic structure of the `mcp.json` can be passed to applications like Claude Desktop, Cursor, or VS Code.
64
+
55
65
```json
56
66
{
57
67
"servers": [
@@ -114,14 +124,6 @@ Environment variables can be passed to server processes using the `env` field. H
114
124
<hfoptionsid="env-variables">
115
125
<hfoptionid="python">
116
126
117
-
Let's first install the packages we need to run the examples.
In Python, we use the `os` module to access environment variables:
126
128
127
129
```python
@@ -170,7 +172,7 @@ The corresponding configuration in `mcp.json` would look like this:
170
172
"transport": {
171
173
"type": "stdio",
172
174
"command": "python",
173
-
"args": ["/path/to/github_server.py"],
175
+
"args": ["/path/to/github_server.py"],// This is an example, we'll use a real server in the next unit
174
176
"env": {
175
177
"GITHUB_TOKEN": "your_github_token"
176
178
}
@@ -196,7 +198,7 @@ In this scenario, we have a local server that is a Python script which could be
196
198
"transport": {
197
199
"type": "stdio",
198
200
"command": "python",
199
-
"args": ["/path/to/file_explorer_server.py"]
201
+
"args": ["/path/to/file_explorer_server.py"]// This is an example, we'll use a real server in the next unit
200
202
}
201
203
}
202
204
]
@@ -214,7 +216,7 @@ In this scenario, we have a remote server that is a weather API.
214
216
"name": "Weather API",
215
217
"transport": {
216
218
"type": "sse",
217
-
"url": "https://example.com/mcp-server"
219
+
"url": "https://example.com/mcp-server"// This is an example, we'll use a real server in the next unit
218
220
}
219
221
}
220
222
]
@@ -225,122 +227,117 @@ Proper configuration is essential for successfully deploying MCP integrations. B
225
227
226
228
In the next section, we'll explore the ecosystem of MCP servers available on Hugging Face Hub and how to publish your own servers there.
227
229
228
-
## Code Clients
229
-
230
-
You can also use the MCP Client within code so that the tools are available to the LLM. Let's explore some examples in `smolagents`. To run these examples you will need to add `mcp[cli]`, `smolagents[toolkit]`, and `smolagents[mcp]` to your `uv` virtual environment.
230
+
## Tiny Agents Clients
231
231
232
-
First, let's explore our weather server from the previous page. In `smolagents`, we can use the `ToolCollection` class to automatically discover and register tools from an MCP server. This is done by passing the `StdioServerParameters` or `SSEServerParameters` to the `ToolCollection.from_mcp` method. We can then print the tools to the console.
232
+
Now, let's explore how to use MCP Clients within code.
233
233
234
-
```python
235
-
from smolagents import ToolCollection
236
-
from mcp.client.stdio import StdioServerParameters
234
+
You can also use tiny agents as MCP Clients to connect directly to MCP servers from your code. Tiny agents provide a simple way to create AI agents that can use tools from MCP servers.
Tiny Agent can run MCP servers with a command line environment. To do this, we will need to install `npm` and run the server with `npx`. **We'll need these for both Python and JavaScript.**
239
237
240
-
with ToolCollection.from_mcp(
241
-
server_parameters, trust_remote_code=True
242
-
) as tools:
243
-
print("\n".join(f"{tool.name}: {tool.description}"for tool in tools.tools))
238
+
Let's install `npx` with `npm`. If you don't have `npm` installed, check out the [npm documentation](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm).
244
239
240
+
```bash
241
+
# install npx
242
+
npm install -g npx
245
243
```
246
244
247
-
<details>
248
-
<summary>
249
-
Output
250
-
</summary>
245
+
<hfoptionsid="language">
246
+
<hfoptionid="python">
251
247
252
-
```sh
253
-
get_weather: Get the current weather for a specified location.
248
+
First, install the tiny agents package:
254
249
250
+
```bash
251
+
pip install "huggingface_hub[mcp]>=0.32.0"
255
252
```
256
253
257
-
</details>
254
+
First, let's login to the Hugging Face Hub. You will need a [login token](https://huggingface.co/docs/huggingface_hub/v0.32.3/en/quick-start#authentication) to do this.
258
255
259
-
We can also connect to an MCP server that is hosted on a remote machine. In this case, we need to pass the `SSEServerParameters` to the `MCPClient` class.
The following example shows a web-browsing agent configured to use the [Qwen/Qwen2.5-72B-Instruct](https://huggingface.co/Qwen/Qwen2.5-72B-Instruct) model via Nebius inference provider, and it comes equipped with a playwright MCP server, which lets it use a web browser! The agent config is loaded specifying [its path in the `tiny-agents/tiny-agents`](https://huggingface.co/datasets/tiny-agents/tiny-agents/tree/main/celinah/web-browser) Hugging Face dataset.
328
331
329
-
with ToolCollection.from_mcp(server_parameters, trust_remote_code=True) as tool_collection:
When you run the agent, you'll see it load, listing the tools it has discovered from its connected MCP servers. Then, it's ready for your prompts!
338
337
339
-
```sh
340
-
The remedy for hangover is to drink water.
341
-
```
338
+
Prompt used in this demo:
342
339
343
-
</details>
340
+
> do a Web Search for HF inference providers on Brave Search and open the first result and then give me the list of the inference providers supported on Hugging Face
0 commit comments