
Share
Discover how to create a sophisticated AI agent using only 50 lines of TypeScript and the Model Context Protocol (MCP), transforming complex tasks into simple scripts.
Over the past few weeks, I've been diving into the Model Context Protocol (MCP) to understand what all the buzz is about. My takeaway? It's surprisingly simple but incredibly powerful. MCP is a standard API that exposes sets of tools to be used by language models (LLMs). By extending an inference client-like Hugging Face’s @huggingface/inference in JavaScript or huggingface_hub in Python-to act as an MCP client, you can seamlessly integrate these tools into your LLM workflows.
But here's the kicker: once you have an MCP client set up, building an agent is literally just a while loop on top of it. In this article, I’ll walk you through how I implemented a simple agent in TypeScript (JS) and why adopting MCP can simplify agentic AI development.
MCP is a protocol that standardizes the way tools are exposed to LLMs. Think of it as a bridge between your language model and external tools like file systems, web browsers, or custom scripts. By adhering to this protocol, you can create modular, reusable tools that any MCP-compatible client can use.
To get started, you need an inference client that supports MCP. Hugging Face provides official SDKs for both JavaScript and Python:
@huggingface/inferencehuggingface_hubFor this example, we'll use the JavaScript SDK. If you have Node.js installed (with pnpm or npm), you can run the following command to install and execute my package in a temporary folder:
npx @huggingface/mcp-client
Or, if you prefer pnpm:
pnpx @huggingface/mcp-client
When you run the command, your simple agent will connect to two distinct MCP servers running locally. These servers provide different sets of tools:
Here’s a high-level overview of how the agent is implemented:
Here’s a simplified version of the code:

import { MCPClient } from '@huggingface/mcp-client';
const client = new MCPClient();
// Connect to MCP servers
client.connect('http://localhost:8081'); // File System Server
client.connect('http://localhost:8082'); // Playwright MCP Server
async [function](/articles/llm-function-calls-hit-a-wall-code-orchestration-offers-a-scalable-solution) loadTools() {
const tools = await client.loadTools();
console.log('Available tools:', tools);
}
loadTools();
// Main loop for user interaction
(async () => {
while (true) {
const input = prompt('Enter your command: ');
if (!input) break;
const response = await client.execute(input);
console.log('Response:', response);
}
})();
Let’s see how this works in practice with a couple of examples:
To write a haiku about the Hugging Face community and save it to a file named hf.txt on your desktop, you can input:
write a haiku about the Hugging Face community and write it to a file named "hf.txt" on my Desktop
The agent will use the file system tool to create and write to the specified file.
For tasks that involve web browsing, you can input something like:
search for the latest news about AI and summarize it in one paragraph
The agent will use the Playwright MCP server to navigate the web, fetch the required information, and generate a summary.
Adopting MCP simpl
Tags
Original Sources
About the author
Kai built ML infrastructure at a Bay Area startup before developing an obsession with transformer architectures and inference optimisation that eventually pulled him out of product work entirely. A stint at a compute research lab sharpened his instinct for what actually matters in a model release versus what is marketing. He writes from the inside — from the perspective of someone who has debugged the systems he is describing at three in the morning. He is allergic to hype and instinctively drawn to the unglamorous plumbing questions that everyone else skips over.
More from The Engineer →This Week's Edition
28 April 2025
88 articles
Related Articles
Related Articles
More Stories