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
# Tiny Agents: an MCP-powered agent in 50 lines of code
2
2
3
-
Now that we've built MCP server's in Gradio let's explore MCP clients even further. This section on based on the experimental project [Tiny Agents](https://huggingface.co/blog/tiny-agents). Which is a super simple way of deploying MCP clients and using Hugging Face Inference Providers.
3
+
Now that we've built MCP servers in Gradio let's explore MCP clients even further. This section builds on the experimental project [Tiny Agents](https://huggingface.co/blog/tiny-agents), which demonstrates a super simple way of deploying MCP clients that can connect to services like our Gradio sentiment analysis server.
4
4
5
-
6
-
It is fairly simple to extend an Inference Client – at HF, we have two official client SDKs: [`@huggingface/inference`](https://github.com/huggingface/huggingface.js) in JS, and [`huggingface_hub`](https://github.com/huggingface/huggingface_hub/) in Python – to also act as a MCP client and hook the available tools from MCP servers into the LLM inference.
7
-
8
-
<Tip>
9
-
10
-
Once you have an MCP Client, an Agent is literally just a while loop on top of it.
11
-
12
-
</Tip>
13
-
14
-
In short exercise, we will walk you through how to implement a Typescript (JS) MCP client, how you can adopt MCP too and how it's going to make Agentic AI way simpler going forward.
5
+
In this short exercise, we will walk you through how to implement a TypeScript (JS) MCP client that can communicate with any MCP server, including the Gradio-based sentiment analysis server we built in the previous section. You'll see how MCP standardizes the way agents interact with tools, making Agentic AI development significantly simpler.
We will also show you how to connect your tiny agent to Gradiobased MCP server from the previous section.
10
+
We will show you how to connect your tiny agent to Gradio-based MCP servers, allowing it to leverage both your custom sentiment analysis tool and other pre-built tools.
20
11
21
12
## How to run the complete demo
22
13
@@ -32,9 +23,9 @@ or if using `pnpm`:
32
23
pnpx @huggingface/mcp-client
33
24
```
34
25
35
-
This installs my package into a temporary folder then executes its command.
26
+
This installs the package into a temporary folder then executes its command.
36
27
37
-
You'll see your simple Agent connect to two distinct MCP servers (running locally), loading their tools, then prompting you for a conversation.
28
+
You'll see your simple Agent connect to multiple MCP servers (running locally), loading their tools (similar to how it would load your Gradio sentiment analysis tool), then prompting you for a conversation.
@@ -45,8 +36,10 @@ By default our example Agent connects to the following two MCP servers:
45
36
- the "canonical" [file system server](https://github.com/modelcontextprotocol/servers/tree/main/src/filesystem), which gets access to your Desktop,
46
37
- and the [Playwright MCP](https://github.com/microsoft/playwright-mcp) server, which knows how to use a sandboxed Chromium browser for you.
47
38
39
+
You can easily add your Gradio sentiment analysis server to this list, as we'll demonstrate later in this section.
40
+
48
41
> [!NOTE]
49
-
> Note: this is a bit counter-intuitive but currently, all MCP servers are actually local processes (though remote servers are coming soon).
42
+
> Note: this is a bit counter-intuitive but currently, all MCP servers in tiny agents are actually local processes (though remote servers are coming soon). This doesn't includes our Gradio server running on localhost:7860.
50
43
51
44
Our input for this first video was:
52
45
@@ -60,40 +53,56 @@ Now let us try this prompt that involves some Web browsing:
The Tiny Agent code lives in the `mcp-client` sub-package of the `huggingface.js` mono-repo, which is the GitHub mono-repo in which all our JS libraries reside.
We connect to our Gradio based MCP server via the [`mcp-remote`](https://www.npmjs.com/package/mcp-remote) package.
85
97
86
-
> [!TIP]
87
-
> The codebase uses modern JS features (notably, async generators) which make things way easier to implement, especially asynchronous events like the LLM responses.
88
-
> You might need to ask a LLM about those JS features if you're not yet familiar with them.
98
+
</Tip>
89
99
90
100
91
101
## The foundation for this: tool calling native support in LLMs.
92
102
93
-
What is going to make this whole blogpost very easy is that the recent crop of LLMs (both closed and open) have been trained for function calling, aka. tool use.
103
+
What makes connecting Gradio MCP servers to our Tiny Agent possible is that recent LLMs (both closed and open) have been trained for function calling, aka. tool use. This same capability powers our integration with the sentiment analysis tool we built with Gradio.
94
104
95
-
A tool is defined by its name, a description, and a JSONSchema representation of its parameters.
96
-
In some sense, it is an opaque representation of any function's interface, as seen from the outside (meaning, the LLM does not care how the function is actually implemented).
105
+
A tool is defined by its name, a description, and a JSONSchema representation of its parameters - exactly how we defined our sentiment analysis function in the Gradio server. Let's look at a simple example:
97
106
98
107
```ts
99
108
const weatherTool = {
@@ -114,6 +123,8 @@ const weatherTool = {
114
123
};
115
124
```
116
125
126
+
Our Gradio sentiment analysis tool would have a similar structure, with `text` as the input parameter instead of `location`.
127
+
117
128
The canonical documentation I will link to here is [OpenAI's function calling doc](https://platform.openai.com/docs/guides/function-calling?api-mode=chat). (Yes... OpenAI pretty much defines the LLM standards for the whole community 😅).
118
129
119
130
Inference engines let you pass a list of tools when calling the LLM, and the LLM is free to call zero, one or more of those tools.
@@ -124,7 +135,7 @@ As a developer, you run the tools and feed their result back into the LLM to con
124
135
125
136
## Implementing an MCP client on top of InferenceClient
126
137
127
-
Now that we know what a tool is in recent LLMs, let us implement the actual MCP client.
138
+
Now that we know what a tool is in recent LLMs, let's implement the actual MCP client that will communicate with our Gradio server and other MCP servers.
128
139
129
140
The official doc at https://modelcontextprotocol.io/quickstart/client is fairly well-written. You only have to replace any mention of the Anthropic client SDK by any other OpenAI-compatible client SDK. (There is also a [llms.txt](https://modelcontextprotocol.io/llms-full.txt) you can feed into your LLM of choice to help you code along).
130
141
@@ -135,7 +146,7 @@ As a reminder, we use HF's `InferenceClient` for our inference client.
135
146
136
147
Our `McpClient` class has:
137
148
- an Inference Client (works with any Inference Provider, and `huggingface/inference` supports both remote and local endpoints)
138
-
- a set of MCP client sessions, one for each connected MCP server (yes, we want to support multiple servers)
149
+
- a set of MCP client sessions, one for each connected MCP server (this allows us to connect to multiple servers, including our Gradio server)
139
150
- and a list of available tools that is going to be filled from the connected servers and just slightly re-formatted.
140
151
141
152
```ts
@@ -156,7 +167,7 @@ export class McpClient {
156
167
}
157
168
```
158
169
159
-
To connect to a MCP server, the official `@modelcontextprotocol/sdk/client` TypeScript SDK provides a `Client` class with a `listTools()` method:
170
+
To connect to a MCP server (like our Gradio sentiment analysis server), the official `@modelcontextprotocol/sdk/client` TypeScript SDK provides a `Client` class with a `listTools()` method:
`StdioServerParameters` is an interface from the MCP SDK that will let you easily spawn a local process: as we mentioned earlier, currently, all MCP servers are actually local processes.
206
+
`StdioServerParameters` is an interface from the MCP SDK that will let you easily spawn a local process: as we mentioned earlier, currently, all MCP servers are actually local processes, including our Gradio server (though we access it via HTTP).
196
207
197
-
For each MCP server we connect to, we slightly re-format its list of tools and add them to `this.availableTools`.
208
+
For each MCP server we connect to (including our Gradio sentiment analysis server), we slightly re-format its list of tools and add them to `this.availableTools`.
198
209
199
210
### How to use the tools
200
211
201
-
Easy, you just pass `this.availableTools` to your LLM chat-completion, in addition to your usual array of messages:
212
+
Using our sentiment analysis tool (or any other MCP tool) is straightforward. You just pass `this.availableTools` to your LLM chat-completion, in addition to your usual array of messages:
202
213
203
214
```ts
204
215
const stream =this.client.chatCompletionStream({
@@ -235,18 +246,20 @@ if (client) {
235
246
}
236
247
```
237
248
249
+
If the LLM chooses to use our sentiment analysis tool, this code will automatically route the call to our Gradio server, execute the analysis, and return the result back to the LLM.
250
+
238
251
Finally you will add the resulting tool message to your `messages` array and back into the LLM.
239
252
240
253
## Our 50-lines-of-code Agent 🤯
241
254
242
-
Now that we have an MCP client capable of connecting to arbitrary MCP servers to get lists of tools and capable of injecting them and parsing them from the LLM inference, well... what is an Agent?
255
+
Now that we have an MCP client capable of connecting to arbitrary MCP servers (including our Gradio sentiment analysis server) to get lists of tools and capable of injecting them and parsing them from the LLM inference, well... what is an Agent?
243
256
244
257
> Once you have an inference client with a set of tools, then an Agent is just a while loop on top of it.
245
258
246
259
In more detail, an Agent is simply a combination of:
247
260
- a system prompt
248
261
- an LLM Inference client
249
-
- an MCP client to hook a set of Tools into it from a bunch of MCP servers
262
+
- an MCP client to hook a set of Tools into it from a bunch of MCP servers (including our Gradio server)
250
263
- some basic control flow (see below for the while loop)
251
264
252
265
> [!TIP]
@@ -290,7 +303,7 @@ Even though this comes from OpenAI 😈, this sentence in particular applies to
290
303
291
304
> We encourage developers to exclusively use the tools field to pass tools, rather than manually injecting tool descriptions into your prompt and writing a separate parser for tool calls, as some have reported doing in the past.
292
305
293
-
Which is to say, we don't need to provide painstakingly formatted lists of tool use examples in the prompt. The `tools: this.availableTools` param is enough.
306
+
Which is to say, we don't need to provide painstakingly formatted lists of tool use examples in the prompt. The `tools: this.availableTools` param is enough, and the LLM will know how to use both the filesystem tools and our Gradio sentiment analysis tool.
294
307
295
308
Loading the tools on the Agent is literally just connecting to the MCP servers we want (in parallel because it's so easy to do in JS):
296
309
@@ -377,19 +390,68 @@ while (true) {
377
390
}
378
391
```
379
392
380
-
## Next steps
393
+
## Connecting Tiny Agents with Gradio MCP Servers
394
+
395
+
Now that we understand both Tiny Agents and Gradio MCP servers, let's see how they work together! The beauty of MCP is that it provides a standardized way for agents to interact with any MCP-compatible server, including our Gradio-based sentiment analysis server.
396
+
397
+
### Using the Gradio Server with Tiny Agents
398
+
399
+
To connect our Tiny Agent to the Gradio sentiment analysis server we built earlier, we just need to add it to our list of servers. Here's how we can modify our agent configuration:
"http://localhost:7860/gradio_api/mcp/sse"// Your Gradio MCP server
413
+
]
414
+
}
415
+
],
416
+
});
417
+
```
418
+
419
+
Now our agent can use the sentiment analysis tool alongside other tools! For example, it could:
420
+
1. Read text from a file using the filesystem server
421
+
2. Analyze its sentiment using our Gradio server
422
+
3. Write the results back to a file
423
+
424
+
### Example Interaction
425
+
426
+
Here's what a conversation with our agent might look like:
427
+
428
+
```
429
+
User: Read the file "feedback.txt" from my Desktop and analyze its sentiment
430
+
431
+
Agent: I'll help you analyze the sentiment of the feedback file. Let me break this down into steps:
432
+
433
+
1. First, I'll read the file using the filesystem tool
434
+
2. Then, I'll analyze its sentiment using the sentiment analysis tool
435
+
3. Finally, I'll write the results to a new file
436
+
437
+
[Agent proceeds to use the tools and provide the analysis]
438
+
```
439
+
440
+
### Deployment Considerations
441
+
442
+
When deploying your Gradio MCP server to Hugging Face Spaces, you'll need to update the server URL in your agent configuration to point to your deployed space:
381
443
382
-
There are many cool potential next steps once you have a running MCP Client and a simple way to build Agents 🔥
-[mistralai/Mistral-Small-3.1-24B-Instruct-2503](https://huggingface.co/mistralai/Mistral-Small-3.1-24B-Instruct-2503) is optimized for function calling
386
-
- Gemma 3 27B, the [Gemma 3 QAT](https://huggingface.co/collections/google/gemma-3-qat-67ee61ccacbf2be4195c265b) models are a popular choice for function calling though it would require us to implement tool parsing as it's not using native `tools` (a PR would be welcome!)
387
-
- Experiment with all the **[Inference Providers](https://huggingface.co/docs/inference-providers/index)**:
0 commit comments