How to Build a Custom MCP Server in TypeScript & Python: A Complete 2026 Developer Tutorial

Table of Contents (5 sections)
Implementation Overview & Architecture
A field-tested, multi-app roadmap designed for rapid execution with zero wasted iterations and maximum automation efficiency.
While hundreds of pre-built servers exist, the real superpower of the Model Context Protocol (MCP) is the ability to write a custom server that exposes your company’s internal APIs, private databases, microservices, or custom bash automation scripts directly to Claude Desktop and Cursor.
In this comprehensive developer tutorial, the AIToolsHaven Engineering Team will guide you through writing, testing, and deploying custom MCP servers from scratch using both the official TypeScript SDK (@modelcontextprotocol/sdk) and the high-level Python FastMCP framework.
code_blocks Developer Engineering Guide
Need reference implementations? Check out our Top 10 Best MCP Servers Guide and our architectural overview on What is MCP?.
Core Concepts: Resources vs Tools vs Prompts
| MCP Primitive | Analogy | Execution Behavior | Typical Example |
|---|---|---|---|
| Resources | GET Endpoint / File Attachment | Read-only context loaded into LLM memory | postgres://schema, log://server.log |
| Tools | POST Endpoint / Function Call | Executable actions with side-effects & Zod schemas | send_email(), restart_container() |
| Prompts | Slash Command / Template | Pre-packaged prompts guiding model behavior | /review-pr, /debug-stack-trace |
Tutorial Part 1: Building a Custom MCP Server in TypeScript
Let’s build a custom Internal Company Metrics Server that queries active user count and health status.
1. Initialize Project & Install Dependencies
mkdir custom-mcp-server && cd custom-mcp-servernpm init -ynpm install @modelcontextprotocol/sdk zodnpm install -D typescript @types/node tsx
2. Write the Server Code (src/index.ts)
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
const server = new Server(
{ name: "metrics-mcp-server", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
// 1. Define available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "get_active_users",
description: "Returns the current active users count for a given region",
inputSchema: {
type: "object",
properties: {
region: { type: "string", description: "e.g. us-east, eu-west" }
},
required: ["region"]
}
}
]
};
});
// 2. Handle tool invocation
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "get_active_users") {
const region = String(request.params.arguments?.region ?? "us-east");
const count = Math.floor(Math.random() * 5000) + 1200; // Replace with live API/DB call
return {
content: [
{
type: "text",
text: "Active users in " + region + ": " + count
}
]
};
}
throw new Error("Tool not found");
});
// 3. Connect via Standard Input/Output
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Metrics MCP Server running on stdio");
}
main();
Tutorial Part 2: Building with Python (FastMCP)
If you prefer Python, Anthropic’s FastMCP decorator syntax allows you to build an entire server in under 15 lines of code:
pip install "mcp[cli]"
from mcp.server.fastmcp import FastMCP
# Initialize FastMCP Server
mcp = FastMCP("DevOps Server")
@mcp.tool()
def restart_service(service_name: str, environment: str = "staging") -> str:
"""Restarts a backend microservice in the designated environment."""
# Place your live Docker / Kubernetes / AWS call here
return f"Successfully restarted {service_name} in {environment} cluster."
if __name__ == "__main__":
mcp.run()
Debugging Live with the MCP Inspector
Before connecting your custom server to Claude or Cursor, test your tools using the official MCP Inspector graphical debugger:
# Test TypeScript server:npx @modelcontextprotocol/inspector npx tsx src/index.ts# Test Python server:mcp dev server.py
The Inspector launches a local browser UI at http://localhost:5173 where you can inspect JSON-RPC payloads and test arguments with instant visual feedback.
Discover 150+ Code Assistants, Agents & MCP Servers
Compare features, community ratings, and setup guides in our directory.
Linking to Claude Desktop & Cursor AI
Add your new custom server to claude_desktop_config.json:
{
"mcpServers": {
"my-custom-metrics": {
"command": "node",
"args": ["/absolute/path/to/custom-mcp-server/dist/index.js"]
}
}
}
Explore complete automation playbooks in our Curated AI Workflows or browse all categories in the AIToolsHaven Directory.
Ready to build with AI Coding Assistants?
Compare 58+ verified tools in the AI Coding Assistants Hub, or filter for 100% free allowances with zero credit card traps.
Answers to Common Questions
This guide analyzes the most dependable solutions in the coding space based on benchmark testing, feature depth, and real-world performance. Browse the comparison table and detailed reviews above to identify the ideal platform for your workflow.
Yes, many of the featured platforms provide free starter tiers, trial periods, or community plans so you can test capabilities before committing to a paid subscription.
Our team evaluates platforms using a structured E-E-A-T benchmark methodology: we test ease of onboarding, prompt response accuracy, integrations, pricing transparency, and real-world team productivity impact.
Start by identifying your non-negotiables—such as budget, API availability, learning curve, and existing tech stack compatibility. Test the free tiers of your top two candidates using your everyday production tasks.
Featured Tools Mentioned in this Guide
Directly compare pricing, verified ratings, and capabilities in our directory.