March 12, 2026
Building Your First Model Context Protocol (MCP) Server
Large Language Models are becoming increasingly powerful development tools. But by default, an LLM is isolated — it cannot interact with your filesystem, databases, APIs, or internal tooling unless you explicitly provide that capability.
The Model Context Protocol (MCP) solves this problem by standardizing how tools and services expose functionality to AI agents.
Instead of writing one-off integrations for each model or tool, you implement an MCP server that exposes capabilities in a structured way. Any MCP-compatible client, such as AI coding assistants or automation agents, can then discover and invoke those capabilities.
In this article we will:
- Understand what MCP servers are
- Build a simple MCP server
- Expose tools to an AI agent
- Test it with a local client
#What Is the Model Context Protocol?
The Model Context Protocol (MCP) is a specification that allows tools, services, and environments to expose capabilities to AI models through a structured interface.
An MCP server can expose things like:
- Tools (functions the model can call)
- Resources (files, documentation, datasets)
- Prompts (structured reusable prompts)
- Context providers (environment information)
Think of it as an API layer specifically designed for AI agents.
AI Agent
│
▼
MCP Client
│
▼
MCP Server
│
├── Tools
├── Filesystem
├── APIs
└── Databases
This architecture enables LLM agents to safely interact with real systems.
#Example: A Simple MCP Server
We'll build a small MCP server that exposes two tools:
add_numbersget_current_time
For this example we'll use Node.js with the official MCP SDK.
#Install Dependencies
npm init -y
npm install @modelcontextprotocol/sdk#Creating the Server
Create server.js.
import { McpServer } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new McpServer({
name: "example-mcp-server",
version: "1.0.0"
});Now we define tools that the AI can call.
#Tool Example: Add Numbers
server.tool(
"add_numbers",
{
a: "number",
b: "number"
},
async ({ a, b }) => {
const result = a + b;
return {
content: [
{
type: "text",
text: `The result is ${result}`
}
]
};
}
);The schema tells the AI what parameters it can send.
#Tool Example: Get Current Time
server.tool(
"get_current_time",
{},
async () => {
return {
content: [
{
type: "text",
text: new Date().toISOString()
}
]
};
}
);Even simple tools like this can be useful for agents coordinating tasks.
#Starting the Server
Finally we connect the server to the standard I/O transport.
const transport = new StdioServerTransport();
await server.connect(transport);Now run the server:
node server.jsYour MCP server is now available to any MCP client.
#Adding Resources
MCP servers can also expose resources — files or structured data that an AI can read.
Example:
server.resource(
"architecture-docs",
"docs://architecture",
async () => {
return {
contents: [
{
uri: "docs://architecture",
text: `
System Architecture
- API Gateway
- Services Layer
- PostgreSQL Database
`
}
]
};
}
);This allows agents to load project documentation on demand.
#Security Considerations
When building MCP servers, keep in mind:
#Limit Capabilities
Only expose tools the model truly needs.
Bad example:
delete_all_filesBetter:
delete_temp_file(path)#Validate Inputs
Never trust model inputs blindly.
if (typeof a !== "number") {
throw new Error("Invalid parameter");
}#Use Sandboxing
If exposing shell or filesystem access, run the server in an isolated environment.
#Real-World MCP Server Examples
MCP servers are commonly used to expose:
#Codebase Tools
- search repository
- run tests
- open files
#DevOps Tools
- query logs
- inspect deployments
- trigger CI pipelines
#Data Access
- query databases
- fetch analytics
- retrieve documents
#Example: Repository Search Tool
A more realistic MCP tool might search a codebase.
import fs from "fs";
import path from "path";
server.tool(
"search_repo",
{
query: "string"
},
async ({ query }) => {
const files = fs.readdirSync("./src");
const matches = [];
for (const file of files) {
const content = fs.readFileSync(path.join("./src", file), "utf8");
if (content.includes(query)) {
matches.push(file);
}
}
return {
content: [
{
type: "text",
text: `Matches: ${matches.join(", ")}`
}
]
};
}
);This kind of tool is extremely useful for AI code assistants.
#Testing the Server
Many AI tools support MCP servers:
- Claude Code
- Copilot CLI
- Cursor
- Custom agents
Typically you configure them like this:
{
"mcpServers": {
"example": {
"command": "node",
"args": ["server.js"]
}
}
}After restarting the client, the tools become available to the AI agent.
#When Should You Build an MCP Server?
MCP servers are useful when you want AI agents to interact with:
- your codebase
- internal APIs
- operational tooling
- structured documentation
- domain-specific systems
They allow you to turn existing systems into AI-accessible capabilities.
#Final Thoughts
The Model Context Protocol is rapidly becoming the standard integration layer for AI agents.
Instead of building bespoke integrations for every tool and model, you can:
- Implement an MCP server
- Expose tools and resources
- Let any compatible AI client use them
As AI-driven development workflows become more common, MCP servers will likely become a core part of modern developer infrastructure.
If you're building AI automation, start with one simple MCP server. You'll quickly discover how powerful it is to give models structured access to your tools and systems.