Have you ever thought about building your own AI agent, something like ChatGPT that can do tasks, answer questions, and even use tools like a calculator or search engine? What if I told you that you can create one using only 50 lines of code?
In this article, We’ll walk you through how to build a tiny but powerful AI agent using something called the Model Context Protocol (MCP). We’ll keep everything simple and easy to follow, even if you’re not a coding expert. By the end, you’ll understand how this works, and you might even be ready to build one yourself.
What Is a Tiny Agent?
A tiny agent is a small program that talks to a large AI model (like ChatGPT), but with a twist—it can think, plan, and use tools to complete tasks.
Think of it like this:
A chatbot gives answers.
A tiny agent can decide what steps to take, use tools, and then give you a better answer.
For example, if you ask it, “What’s the weather in Lagos?”—instead of guessing, it can use a weather tool to check and then give you the actual answer.
What is MCP (Model Context Protocol)?
Let’s break it down in simple terms.
Model Context Protocol (MCP) is a set of rules that helps AI models and tools talk to each other. It’s like a translator between the brain (the AI) and the hands (the tools).
With MCP:
- The AI model understands what tools are available.
- It knows when to use them.
- It learns from past actions to improve future answers.
It turns a regular chatbot into a smart agent that can think and act.
Why Build a Tiny Agent?
You might wonder—why bother making a tiny agent?
Here are some great reasons:
- It’s fast to build – Just 50 lines of code.
- You learn a lot – You get to understand how AI tools really work.
- You can customize it – Want it to help with your tasks? You can set that up.
- It saves resources – Lightweight code means it runs easily without needing powerful computers.
So even if you’re just starting, you can do this.
What You Need to Get Started
To build your tiny agent, here are the basic things you’ll need:
- Node.js is installed on your computer (this lets you run JavaScript code).
- A bit of knowledge in JavaScript or TypeScript (don’t worry, you can still follow if you’re learning).
- Access to an AI model provider like Hugging Face.
- An API key from that provider.
- A code editor like VS Code.
Once you’re ready, install the needed tools by running this in your terminal:
bashCopyEditnpm install @mcp/tokenizer @mcp/agent @huggingface/inference
These tools will help your tiny agent talk to the AI model and use tools.
The 3 Tools You’ll Use
Here’s what each part does:
@mcp/tokenizer
This helps prepare the message you want to send to the AI model.
@mcp/agent
This is the heart of your tiny agent. It handles decisions like when to use tools or what to say next.
@huggingface/inference
This lets you use AI models from Hugging Face easily.
Together, these give you everything you need to create a smart, tiny agent.
Understanding the Agent’s Workflow
Let’s look at how the agent works in five simple steps:
- You type something (like a question).
- The agent sends it to the AI model.
- The model decides whether to use a tool (like a search tool).
- If needed, it uses the tool and gets the result.
- Finally, the model responds with the final answer.
This loop is what makes the tiny agent smart.
What the Code Looks Like
Here’s a simple example of how the 50 lines might look in TypeScript:
tsCopyEditimport { createAgent } from '@mcp/agent';
import { HfInference } from '@huggingface/inference';
const hf = new HfInference(process.env.HF_API_KEY);
const tools = {
search: async (query: string) => {
const res = await fetch(`https://api.search.com?q=${query}`);
return await res.text();
}
};
const agent = createAgent({
model: async (messages) => {
return await hf.chatCompletion({
model: 'meta-llama/Llama-3-8B-Instruct',
messages
});
},
tools,
system: 'You are a smart AI agent. Use tools if needed.'
});
(async () => {
const userInput = 'What is the capital of Canada?';
const result = await agent.run(userInput);
console.log(result);
})();
What does this do?
- It sets up the Hugging Face AI model.
- It adds a search tool the agent can use.
- It runs the agent with one question.
- If the agent thinks it needs help, it uses the tool.
All of that in less than 50 lines!
How Do Tools Work in This Setup?
In your agent, tools are just simple functions you write.
Let’s say you want a calculator. You create a function like this:
tsCopyEditconst tools = {
calculator: (input) => eval(input)
};
When someone asks, “What’s 45 x 3?”, the agent can use this tool to calculate the answer and then reply with it.
The model decides when to use a tool based on your message.
Taking It Further With MCP Servers
If you want more power, like long conversations or multiple tools, you can run an MCP server.
This is like giving your tiny agent a bigger brain.
Run it with:
bashCopyEditnpx mcp-server start
It lets you:
- Use many tools at once
- Store memory for ongoing chats
- Connect with other apps using APIs
It’s a bigger setup, but it gives your tiny agent even more abilities.
Why Use Hugging Face?
Hugging Face is a platform full of AI models. You can:
- Use powerful models like LLaMA 3, Falcon, and more
- Run them in the cloud
- Try them for free or pay as needed
It works really well with MCP and helps you keep your code light and clean.
Real-Life Uses for Tiny Agents
So what can you actually build?
- Customer support bot for your website
- AI helper to check your calendar or emails
- A study assistant who can explain topics to you
- A search agent that finds info online and summarizes it
- Simple AI app for kids or elderly users
Because it’s so light, you can build fast and improve as you go.
Tips for Success
Here are a few tips to make your tiny agent even better:
- Write clear tool descriptions so the model knows what each tool does.
- Keep tools simple at first—focus on one function per tool.
- Test often by sending different types of questions.
- Use structured prompts (set the tone with a clear system message).
Big Possibilities in Tiny Code
You don’t need 1,000 lines of code or a team of engineers to build a smart AI assistant. With Model Context Protocol (MCP) and Hugging Face, you can build a tiny agent that thinks and acts—all in under 50 lines.
Whether you’re learning, building a startup, or just having fun, tiny agents are a powerful way to explore what AI can do.
Now it’s your turn, go ahead and try building your tiny agent today. You’ll be amazed at how much it can do with so little code.