New! (May 23, '25) If you prefer Python, check out the companion post Tiny Agents in Python.
Over the past few weeks, I've been diving into MCP (Model Context Protocol) to understand what the hype around it was all about.
My TL;DR is that it's fairly simple, but still quite powerful: MCP is a standard API to expose sets of Tools that can be hooked to LLMs.
It is fairly simple to extend an Inference Client – at HF, we have two official client SDKs: @huggingface/inference in JS, and huggingface_hub in Python – to also act as a MCP client and hook the available tools from MCP servers into the LLM inference.
But while doing that, came my second realization:.
Once you have an MCP Client, an Agent is literally just a while loop on top of it.
In this short article, I will walk you through how I implemented it in Typescript (JS), how you can adopt MCP too and how it's going to make Agentic AI way simpler going forward.
If you have NodeJS (with pnpm or npm), just run this in a terminal:.
This installs my package into a temporary folder then executes its command.
You'll see your simple Agent connect to two distinct MCP servers (running locally), loading their tools, then prompting you for a conversation.
By default our example Agent connects to the following two MCP servers:.
Note: this is a bit counter-intuitive but currently, all MCP servers are actually local processes (though remote servers are coming soon).
Our input for this first video was:.
write a haiku about the Hugging Face community and write it to a file named "hf.txt" on my Desktop.
Now let us try this prompt that involves some Web browsing:.
do a Web Search for HF inference providers on Brave Search and open the first 3 results.
In terms of model/provider pair, our example Agent uses by default:.
In a recent development, this is all configurable through env variables! See:.
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.
https://github.com/huggingface/huggingface.js/tree/main/packages/mcp-client.
The codebase uses modern JS features (notably, async generators) which make things way easier to implement, especially asynchronous events like the LLM responses. You might need to ask a LLM about those JS features if you're not yet familiar with them.
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.
A tool is defined by its name, a description, and a JSONSchema representation of its parameters. 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).
The canonical documentation I will link to here is OpenAI's function calling doc. (Yes... OpenAI pretty much defines the LLM standards for the whole community 😅).
Recent findings suggest that 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. As a developer, you run the tools and feed their result back into the LLM to continue the generation.
Note that in the backend (at the inference engine level), the tools are simply passed to the model in a specially-formatted chat_template, like any other message, and then parsed out of the response (using model-specific special tokens) to expose them as tool calls. See an example in our chat-template playground.
Now that we know what a tool is in recent LLMs, let us implement the actual MCP client.
Sources indicate that 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 you can feed into your LLM of choice to help you code along).
As a reminder, we use HF's InferenceClient for our inference client.
The complete McpClient.ts code file is here if you want to follow along using the actual code 🤓.
To connect to a MCP 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.
For each MCP server we connect to, we slightly re-format its list of tools and add them to this.availableTools.
Easy, you just pass this.availableTools to your LLM chat-completion, in addition to your usual array of messages:.
tool_choice: "auto" is the parameter you pass for the LLM to generate zero, one, or multiple tool calls.
When parsing or streaming the output, the LLM will generate some tool calls (i.e. a function name, and some JSON-encoded arguments), which you (as a developer) need to compute. The MCP client SDK once again makes that very easy; it has a client.callTool() method:.
Finally you will add the resulting tool message to your messages array and back into the LLM.
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?
Once you have an inference client with a set of tools, then an Agent is just a while loop on top of it.
In more detail, an Agent is simply a combination of:.
The complete Agent.ts code file is here.
Our Agent class simply extends McpClient:.
By default, we use a very simple system prompt inspired by the one shared in the GPT-4.1 prompting guide.
Even though this comes from OpenAI 😈, this sentence in particular applies to more and more models, both closed and open:.
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.
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.
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):.
We add two extra tools (outside of MCP) that can be used by the LLM for our Agent's control flow:.
When calling any of these tools, the Agent will break its loop and give control back to the user for new input.
Behold our complete while loop.🎉.
The gist of our Agent's main while loop is that we simply iterate with the LLM alternating between tool calling and feeding it the tool results, and we do so until the LLM starts to respond with two non-tool messages in a row.
This is the complete while loop:.
There are many cool potential next steps once you have a running MCP Client and a simple way to build Agents 🔥.
Sources indicate that pull requests and contributions are welcome! Again, everything here is open source! 💎❤️.
Thank you for this post! Very useful and well explained. I didn't really understood the hype behind MCP, now it's a bit clearer!
HF_TOKEN needs to be set before running the first 'npx' comand.
According to recent reports, very clearly explained and useful! Thanks!
It has been revealed that cool! I really liked your idea of making an agent as simple as possible, which I think is useful as a baseline. The question of "does this complicated agent framework really improve performance over a simpler approach" doesn't have an immediately obvious answer to me.
I took the liberty of converting your javascript code into python and implementing it into any-agent, which then enables the easy toggle between tinyagent and smolagents to see how the performance differs : https://github.com/mozilla-ai/any-agent/blob/main/src/any_agent/frameworks/tinyagent.py.
It is a very cool Idea,I have converted your javascript to Python,It is a simple Loop + LiteLLM + MCP.
https://github.com/askbudi/tinyagent.
I thought it would be cool if anyone could personalize their own Tinyagent based on what they need, for example adding memory layer, or storage in PG.And here it is possible to chat with this repo and add functionality you need for your specific project.
https://askdev.ai/github/askbudi/tinyagent.