{"id":5763,"date":"2025-07-02T22:36:56","date_gmt":"2025-07-02T21:36:56","guid":{"rendered":"https:\/\/villpress.com\/?p=5763"},"modified":"2025-07-02T22:37:34","modified_gmt":"2025-07-02T21:37:34","slug":"tiny-agents-a-mcp-powered-agent-in-50-lines-of-code","status":"publish","type":"post","link":"https:\/\/villpress.com\/zh\/tiny-agents-a-mcp-powered-agent-in-50-lines-of-code\/","title":{"rendered":"Tiny Agents: A MCP-Powered Agent in 50 Lines of Code"},"content":{"rendered":"<p class=\"wp-block-paragraph\">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 <strong>only 50 lines of code<\/strong>?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, We\u2019ll walk you through how to build a <strong><a target=\"_blank\" rel=\"nofollow\" href=\"https:\/\/villpress.com\/goto\/https:\/\/huggingface.co\/blog\/tiny-agents\">tiny but powerful AI agent<\/a><\/strong> using something called the <strong>Model Context Protocol (MCP)<\/strong>. We\u2019ll keep everything simple and easy to follow, even if you&#8217;re not a coding expert. By the end, you\u2019ll understand how this works, and you might even be ready to build one yourself.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What Is a Tiny Agent?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>tiny agent<\/strong> is a small program that talks to a large AI model (like ChatGPT), but with a twist\u2014it can <strong>think, plan, and use tools<\/strong> to complete tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of it like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">A chatbot gives answers.<br>A <strong>tiny agent<\/strong> can <strong>decide what steps to take<\/strong>, <strong>use tools<\/strong>, and <strong>then give you a better answer<\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">For example, if you ask it, <em>&#8220;What&#8217;s the weather in Lagos?&#8221;<\/em>\u2014instead of guessing, it can use a weather tool to check and then give you the actual answer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What is MCP (Model Context Protocol)?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s break it down in simple terms.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Model Context Protocol (MCP)<\/strong> is a set of rules that helps <strong>AI models and tools talk to each other<\/strong>. It\u2019s like a translator between the brain (the AI) and the hands (the tools).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With MCP:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The AI model understands what tools are available.<\/li>\n\n\n\n<li>It knows <strong>when<\/strong> to use them.<\/li>\n\n\n\n<li>It <strong>learns from past actions<\/strong> to improve future answers.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It turns a regular chatbot into a <strong>smart agent<\/strong> that can think and act.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Build a Tiny Agent?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You might wonder\u2014why bother making a tiny agent?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here are some great reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>It\u2019s fast to build<\/strong> \u2013 Just 50 lines of code.<\/li>\n\n\n\n<li><strong>You learn a lot<\/strong> \u2013 You get to understand how AI tools really work.<\/li>\n\n\n\n<li><strong>You can customize it<\/strong> \u2013 Want it to help with your tasks? You can set that up.<\/li>\n\n\n\n<li><strong>It saves resources<\/strong> \u2013 Lightweight code means it runs easily without needing powerful computers.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">So even if you\u2019re just starting, <strong>you can do this<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What You Need to Get Started<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To build your tiny agent, here are the basic things you\u2019ll need:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Node.js is installed<\/strong> on your computer (this lets you run JavaScript code).<\/li>\n\n\n\n<li>A bit of knowledge in <strong>JavaScript or TypeScript<\/strong> (don\u2019t worry, you can still follow if you&#8217;re learning).<\/li>\n\n\n\n<li>Access to an AI model provider like <strong>Hugging Face<\/strong>.<\/li>\n\n\n\n<li>An <strong>API key<\/strong> from that provider.<\/li>\n\n\n\n<li>A code editor like <strong>VS Code<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Once you\u2019re ready, install the needed tools by running this in your terminal:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopyEdit<code>npm install @mcp\/tokenizer @mcp\/agent @huggingface\/inference\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These tools will help your tiny agent talk to the AI model and use tools.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The 3 Tools You\u2019ll Use<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s what each part does:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>@mcp\/tokenizer<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This helps prepare the message you want to send to the AI model.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>@mcp\/agent<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the heart of your tiny agent. It handles decisions like when to use tools or what to say next.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>@huggingface\/inference<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This lets you use AI models from Hugging Face easily.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Together, these give you everything you need to create a smart, tiny agent.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding the Agent\u2019s Workflow<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s look at <strong>how the agent works<\/strong> in five simple steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>You type something<\/strong> (like a question).<\/li>\n\n\n\n<li>The agent <strong>sends it to the AI model<\/strong>.<\/li>\n\n\n\n<li>The model <strong>decides<\/strong> whether to use a tool (like a search tool).<\/li>\n\n\n\n<li>If needed, it <strong>uses the tool<\/strong> and gets the result.<\/li>\n\n\n\n<li>Finally, the model <strong>responds with the final answer<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This loop is what makes the tiny agent smart.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What the Code Looks Like<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s a simple example of how the 50 lines might look in TypeScript:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tsCopyEdit<code>import { createAgent } from '@mcp\/agent';\nimport { HfInference } from '@huggingface\/inference';\n\nconst hf = new HfInference(process.env.HF_API_KEY);\n\nconst tools = {\n  search: async (query: string) =&gt; {\n    const res = await fetch(`https:\/\/api.search.com?q=${query}`);\n    return await res.text();\n  }\n};\n\nconst agent = createAgent({\n  model: async (messages) =&gt; {\n    return await hf.chatCompletion({\n      model: 'meta-llama\/Llama-3-8B-Instruct',\n      messages\n    });\n  },\n  tools,\n  system: 'You are a smart AI agent. Use tools if needed.'\n});\n\n(async () =&gt; {\n  const userInput = 'What is the capital of Canada?';\n  const result = await agent.run(userInput);\n  console.log(result);\n})();\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">What does this do?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It sets up the Hugging Face AI model.<\/li>\n\n\n\n<li>It adds a <strong>search tool<\/strong> the agent can use.<\/li>\n\n\n\n<li>It runs the agent with one question.<\/li>\n\n\n\n<li>If the agent thinks it needs help, it uses the tool.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All of that in <strong>less than 50 lines<\/strong>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How Do Tools Work in This Setup?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In your agent, tools are just <strong>simple functions<\/strong> you write.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s say you want a calculator. You create a function like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">tsCopyEdit<code>const tools = {\n  calculator: (input) =&gt; eval(input)\n};\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When someone asks, <em>\u201cWhat\u2019s 45 x 3?\u201d<\/em>, the agent can use this tool to calculate the answer and then reply with it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>model decides<\/strong> when to use a tool based on your message.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Taking It Further With MCP Servers<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you want more power, like long conversations or multiple tools, you can run an <strong>MCP server<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is like giving your tiny agent a bigger brain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run it with:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopyEdit<code>npx mcp-server start\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It lets you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use many tools at once<\/li>\n\n\n\n<li>Store memory for ongoing chats<\/li>\n\n\n\n<li>Connect with other apps using APIs<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It\u2019s a bigger setup, but it gives your tiny agent even more abilities.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Use Hugging Face?<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hugging Face is a platform full of AI models. You can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use powerful models like <strong>LLaMA 3<\/strong>, <strong>Falcon<\/strong>, and more<\/li>\n\n\n\n<li>Run them in the cloud<\/li>\n\n\n\n<li>Try them for free or pay as needed<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">It works really well with MCP and helps you keep your code light and clean.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Real-Life Uses for Tiny Agents<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So what can you actually build?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Customer support bot<\/strong> for your website<\/li>\n\n\n\n<li><strong>AI helper<\/strong> to check your calendar or emails<\/li>\n\n\n\n<li><strong>A study assistant<\/strong> who can explain topics to you<\/li>\n\n\n\n<li><strong>A search agent<\/strong> that finds info online and summarizes it<\/li>\n\n\n\n<li><strong>Simple AI app<\/strong> for kids or elderly users<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because it\u2019s so light, you can build fast and improve as you go.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Tips for Success<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here are a few tips to make your tiny agent even better:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Write clear tool descriptions<\/strong> so the model knows what each tool does.<\/li>\n\n\n\n<li><strong>Keep tools simple<\/strong> at first\u2014focus on one function per tool.<\/li>\n\n\n\n<li><strong>Test often<\/strong> by sending different types of questions.<\/li>\n\n\n\n<li>Use <strong>structured prompts<\/strong> (set the tone with a clear system message).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Big Possibilities in Tiny Code<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You don\u2019t need 1,000 lines of code or a team of engineers to build a smart AI assistant. With <strong>Model Context Protocol (MCP)<\/strong> and <strong>Hugging Face<\/strong>, you can build a <strong>tiny agent that thinks and acts<\/strong>\u2014all in under 50 lines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whether you&#8217;re learning, building a startup, or just having fun, tiny agents are a powerful way to explore what AI can do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Now it\u2019s your turn<\/strong>, go ahead and try building your tiny agent today. You\u2019ll be amazed at how much it can do with so little code.<\/p>","protected":false},"excerpt":{"rendered":"<p>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\u2019ll walk you through how to build [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5764,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[64],"tags":[420,419],"ppma_author":[331],"class_list":["post-5763","post","type-post","status-publish","format-standard","has-post-thumbnail","category-ai","tag-ai-agent","tag-tiny-agents"],"authors":[{"term_id":331,"user_id":1,"is_guest":0,"slug":"pastakutmanwen","display_name":"Staff Writer","avatar_url":{"url":"https:\/\/villpress.com\/wp-content\/uploads\/2025\/05\/Logo.png","url2x":"https:\/\/villpress.com\/wp-content\/uploads\/2025\/05\/Logo.png"},"author_category":"1","first_name":"Staff","last_name":"Writer","user_url":"http:\/\/villpress.com","job_title":"Staffs At Villpress","description":"The Villpress Staff Writers are an in-house team of experienced editors and industry experts dedicated to producing clear, insightful content. As part of Villpress, they cover the latest trends and innovations across business, technology, artificial intelligence, advertising, and more, delivering stories that inform, engage, and add real value to readers."}],"_links":{"self":[{"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/posts\/5763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/comments?post=5763"}],"version-history":[{"count":1,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/posts\/5763\/revisions"}],"predecessor-version":[{"id":5765,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/posts\/5763\/revisions\/5765"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/media\/5764"}],"wp:attachment":[{"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/media?parent=5763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/categories?post=5763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/tags?post=5763"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/villpress.com\/zh\/wp-json\/wp\/v2\/ppma_author?post=5763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}