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
+97-82Lines changed: 97 additions & 82 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": [
@@ -80,7 +90,7 @@ For local servers using stdio transport, the configuration includes the command
80
90
"transport": {
81
91
"type": "stdio",
82
92
"command": "python",
83
-
"args": ["/path/to/file_explorer_server.py"]
93
+
"args": ["/path/to/file_explorer_server.py"]// This is an example, we'll use a real server in the next unit
84
94
}
85
95
}
86
96
]
@@ -162,7 +172,7 @@ The corresponding configuration in `mcp.json` would look like this:
162
172
"transport": {
163
173
"type": "stdio",
164
174
"command": "python",
165
-
"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
166
176
"env": {
167
177
"GITHUB_TOKEN": "your_github_token"
168
178
}
@@ -188,7 +198,7 @@ In this scenario, we have a local server that is a Python script which could be
188
198
"transport": {
189
199
"type": "stdio",
190
200
"command": "python",
191
-
"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
192
202
}
193
203
}
194
204
]
@@ -206,7 +216,7 @@ In this scenario, we have a remote server that is a weather API.
206
216
"name": "Weather API",
207
217
"transport": {
208
218
"type": "sse",
209
-
"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
210
220
}
211
221
}
212
222
]
@@ -217,120 +227,125 @@ Proper configuration is essential for successfully deploying MCP integrations. B
217
227
218
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.
219
229
220
-
## Code Clients
230
+
## Tiny Agents Clients
221
231
222
-
You can also use the MCP Client in within code so that the tools are available to the LLM. Let's explore some examples in `smolagents`.
232
+
Now, let's explore how to use MCP Clients within code.
223
233
224
-
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.
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.
225
235
226
-
```python
227
-
from smolagents import ToolCollection
228
-
from mcp.client.stdio import StdioServerParameters
236
+
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.**
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).
231
239
232
-
with ToolCollection.from_mcp(
233
-
server_parameters, trust_remote_code=True
234
-
) as tools:
235
-
print("\n".join(f"{tool.name}: {tool.description}"for tool in tools.tools))
240
+
### Setup
236
241
242
+
First, we will need to install `npx` if you don't have it installed. You can do this with the following command:
243
+
244
+
```bash
245
+
# install npx
246
+
npm install -g npx
237
247
```
238
248
239
-
<details>
240
-
<summary>
241
-
Output
242
-
</summary>
249
+
Then, we will need to install the huggingface_hub package with the MCP support. This will allow us to run MCP servers and clients.
250
+
251
+
```bash
252
+
pip install "huggingface_hub[mcp]>=0.32.0"
253
+
```
243
254
244
-
```sh
245
-
get_weather: Get the current weather for a specified location.
255
+
Then, we will need to log in to the Hugging Face Hub to access the MCP servers. You can do this with the `huggingface-cli` command line tool. You will need a [login token](https://huggingface.co/docs/huggingface_hub/v0.32.3/en/quick-start#authentication) to do this.
246
256
257
+
```bash
258
+
huggingface-cli login
247
259
```
248
260
249
-
</details>
261
+
<hfoptionsid="language">
262
+
<hfoptionid="python">
250
263
251
-
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.
264
+
### Connecting to MCP Servers
252
265
253
-
```python
254
-
from smolagents.mcp_client import MCPClient
266
+
Now, let's create an agent configuration file `agent.json`.
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.
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!
345
+
346
+
Prompt used in this demo:
332
347
333
-
</details>
348
+
> 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
The MCP is a standardized protocol designed to connect AI models with external tools, data sources, and environments. It addresses the limitations of existing AI systems by enabling interoperability and access to real-time information.
6
+
7
+
## Key Concepts
8
+
9
+
### Client-Server Architecture
10
+
MCP follows a client-server model where clients manage communication between users and servers. This architecture promotes modularity, allowing for easy addition of new servers without requiring changes to existing hosts.
11
+
12
+
### Components
13
+
#### Host
14
+
The user-facing AI application that serves as the interface for end-users.
15
+
16
+
##### Client
17
+
A component within the host application responsible for managing communication with a specific MCP server. Clients maintain 1:1 connections with servers and handle protocol-level details.
18
+
19
+
#### Server
20
+
An external program or service that provides access to tools, data sources, or services via the MCP protocol. Servers act as lightweight wrappers around existing functionalities.
21
+
22
+
### Capabilities
23
+
#### Tools
24
+
Executable functions that can perform actions (e.g., sending messages, querying APIs). Tools are typically model-controlled and require user approval due to their ability to perform actions with side effects.
25
+
26
+
#### Resources
27
+
Read-only data sources for context retrieval without significant computation. Resources are application-controlled and designed for data retrieval similar to GET endpoints in REST APIs.
28
+
29
+
#### Prompts
30
+
Pre-defined templates or workflows that guide interactions between users, AI models, and available capabilities. Prompts are user-controlled and set the context for interactions.
31
+
32
+
#### Sampling
33
+
Server-initiated requests for LLM processing, enabling server-driven agentic behaviors and potentially recursive or multi-step interactions. Sampling operations typically require user approval.
34
+
35
+
### Communication Protocol
36
+
The MCP protocol uses JSON-RPC 2.0 as the message format for communication between clients and servers. Two primary transport mechanisms are supported: stdio (for local communication) and HTTP+SSE (for remote communication). Messages include requests, responses, and notifications.
37
+
38
+
### Discovery Process
39
+
MCP allows clients to dynamically discover available tools, resources, and prompts through list methods (e.g., `tools/list`). This dynamic discovery mechanism enables clients to adapt to the specific capabilities each server offers without requiring hardcoded knowledge of server functionality.
40
+
41
+
### MCP SDKs
42
+
Official SDKs are available in various programming languages for implementing MCP clients and servers. These SDKs handle protocol-level communication, capability registration, and error handling, simplifying the development process.
43
+
44
+
### Gradio Integration
45
+
Gradio allows easy creation of web interfaces that expose capabilities to the MCP protocol, making it accessible for both humans and AI models. This integration provides a human-friendly interface alongside AI-accessible tools with minimal code.
0 commit comments