Step-by-step tutorial for building MCP (Model Context Protocol) servers for Claude Code. TypeScript implementation with examples for database, API, and file sys
The Model Context Protocol (MCP) is what makes Claude Code extensible. It’s the standard that lets you give Claude access to your databases, APIs, file systems, and any custom tool you can imagine.
Building an MCP server is surprisingly straightforward. Let’s build one from scratch.
What is MCP?
MCP is a protocol that standardizes how AI models communicate with external tools. Think of it as USB for AI — a universal connector that lets any AI model use any tool.
An MCP server:
- Defines available tools (what can Claude do?)
- Handles tool calls (Claude says “query the database,” the server executes it)
- Returns results (sends data back to Claude)
Setting Up Your First MCP Server
Step 1: Project Setup
mkdir my-mcp-server
cd my-mcp-server
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/node tsx
npx tsc --init
Step 2: Basic Server Structure
// src/index.ts
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { z } from 'zod';
const server = new McpServer({
name: 'my-custom-tools',
version: '1.0.0',
});
// Define a simple tool
server.tool(
'greet',
'Greet a user by name',
{
name: z.string().describe('The name to greet'),
},
async ({ name }) => ({
content: [
{
type: 'text',
text: `Hello, ${name}! Welcome to my MCP server.`,
},
],
})
);
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
}
main().catch(console.error);
Step 3: Register with Claude Code
// In your project's .claude/settings.json or CLAUDE.md:
// Add the MCP server configuration
{
"mcpServers": {
"my-tools": {
"command": "npx",
"args": ["tsx", "/path/to/my-mcp-server/src/index.ts"]
}
}
}
Comments · 0
No comments yet. Be the first to share your thoughts.