PromptsApr 6, 2026·2 min read

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.

TL;DR
Step-by-step guide to building custom MCP servers with TypeScript or Python SDKs for extending AI agent capabilities.
§01

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.

§02

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.

§03

How to use

  1. Scaffold a new MCP server (TypeScript recommended):
npx @anthropic/create-mcp-server my-mcp-server
cd my-mcp-server && npm run dev
  1. Or use Python:
pip install mcp
  1. Define tools and add to your .mcp.json to test with Claude Code.
§04

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.

§05

Related on TokRepo

§06

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

What programming languages can I use to build an MCP server?+

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.

How do I test my MCP server?+

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.

Can MCP servers access databases?+

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.

How do I deploy an MCP server?+

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.

What is the difference between tools and resources in MCP?+

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)
🙏

Source & Thanks

Based on the MCP Specification by Anthropic.

MCP SDK — Official SDKs for TypeScript and Python

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.