Build Your Own MCP Server — Step-by-Step Guide
Complete guide to building a custom MCP server from scratch. Covers the protocol, TypeScript and Python SDKs, tool definition, resource management, testing, and deployment patterns.
What it is
This is a comprehensive guide to building a custom Model Context Protocol (MCP) server from scratch. It covers the MCP protocol fundamentals, TypeScript and Python SDK usage, tool definition patterns, resource management, testing strategies, and deployment options. MCP servers extend AI agents like Claude Code with custom capabilities -- database access, API integrations, file processing, and more.
The guide targets developers who want to give their AI coding agents access to specific tools and data sources not covered by existing MCP servers. Building a custom server lets you expose your organization's internal APIs, databases, or services to AI agents securely.
How it saves time or tokens
A custom MCP server lets AI agents interact with your systems directly instead of relying on you to copy-paste data between tools. The agent calls your server's tools, gets results, and incorporates them into its workflow. This eliminates the manual intermediary role and reduces the tokens spent on describing data that the agent could query directly.
How to use
- Scaffold a new MCP server (TypeScript recommended):
npx @anthropic/create-mcp-server my-mcp-server
cd my-mcp-server && npm run dev
- Or use Python:
pip install mcp
- Define tools and add to your
.mcp.jsonto test with Claude Code.
Example
A minimal MCP server with a custom tool in TypeScript:
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-server', version: '1.0.0' });
server.tool(
'lookup_user',
'Find a user by email address',
{ email: z.string().email() },
async ({ email }) => {
const user = await db.findUserByEmail(email);
return { content: [{ type: 'text', text: JSON.stringify(user) }] };
}
);
const transport = new StdioServerTransport();
await server.connect(transport);
This server exposes a lookup_user tool that Claude Code can call during conversations.
Related on TokRepo
- MCP Chrome Integration — Example MCP server for browser automation
- MCP PostgreSQL Integration — Example MCP server for database access
Common pitfalls
- MCP servers communicate via stdio. Logging to stdout breaks the protocol. Use stderr for debug output.
- Tool descriptions are sent to the LLM as context. Vague descriptions lead to incorrect tool usage. Write clear, specific descriptions for each tool.
- Input validation is critical. The LLM may send unexpected parameter values. Use Zod schemas (TypeScript) or Pydantic models (Python) to validate all inputs.
Frequently Asked Questions
The official SDKs support TypeScript and Python. TypeScript is recommended for its type safety and the mature @modelcontextprotocol/sdk package. Python uses the mcp package. Both support the full MCP specification.
Add your server to the .mcp.json file and restart Claude Code. The agent can then call your tools during conversations. Use stderr logging to debug tool execution without breaking the stdio protocol.
Yes. Your MCP server can connect to any database your programming language supports. Define tools that accept query parameters, execute database operations, and return results to the AI agent.
MCP servers run as local processes alongside the AI agent. For Claude Code, they are configured in .mcp.json and launched automatically. For shared servers, you can deploy them as HTTP endpoints using the SSE transport.
Tools are actions the agent can invoke (like API calls or database queries). Resources are data the agent can read (like file contents or configuration). Tools are active operations; resources are passive data sources.
Citations (3)
- MCP Documentation— Model Context Protocol specification and SDKs
- MCP TypeScript SDK— TypeScript SDK for building MCP servers
- Anthropic Docs— Claude Code MCP server integration
Related on TokRepo
Source & Thanks
Based on the MCP Specification by Anthropic.
MCP SDK — Official SDKs for TypeScript and Python