Build an MCP Server in TypeScript or Python
Choose your runtime to view a functional Model Context Protocol server exposing custom tools.
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
// 1. Initialize MCP Server
const server = new Server(
{ name: "my-custom-server", version: "1.0.0" },
{ capabilities: { tools: {} } }
);
// 2. Define exposed tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: "calculate_tax",
description: "Calculate sales tax for a transaction",
inputSchema: {
type: "object",
properties: {
amount: { type: "number", description: "Transaction amount in USD" },
state: { type: "string", description: "Two-letter US state code" },
},
required: ["amount", "state"],
},
},
],
};
});
// 3. Handle tool invocation
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "calculate_tax") {
const { amount, state } = request.params.arguments as { amount: number; state: string };
const rate = state === "CA" ? 0.0725 : 0.05;
const tax = amount * rate;
return {
content: [{ type: "text", text: `Tax for ${state} on $${amount} is $${tax.toFixed(2)}` }],
};
}
throw new Error("Tool not found");
});
// 4. Connect via stdio
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP Server running on stdio");Standard Input/Output (stdio) vs Server-Sent Events (SSE)
Model Context Protocol supports two standardized transports for connecting clients to servers.
Sub-Process Pipes
The host application (Claude Desktop or Cursor) spawns the MCP server as a child process using npx, uvx, or docker run.
- Zero network ports or firewall configurations needed
- Instant process termination when client closes
- Highest security: credentials stay in process environment
HTTP Server-Sent Events
The MCP server runs as a standalone HTTP microservice. The client establishes an SSE stream to receive server messages and sends client requests via HTTP POST.
- Host centralized enterprise tools in your VPC or AWS
- Multiple AI clients can share a single running server
- Standard Bearer token and OAuth authentication support
Production Server Checklist
Ensure your custom MCP server functions reliably with Claude Desktop, Cursor, and Cline.
Standard stdout is strictly reserved for JSON-RPC messages. Always use console.error() or stderr for debugging logs.
Write clear parameter descriptions in your input schema so LLMs know exactly what data format and required values to pass.
When a database query or API call fails, return the error message in the tool response text so the agent can self-correct instead of crashing.
Package with an executable bin so users can instantly launch your server via npx your-server or uvx your-server.
Looking for existing servers to integrate today?
Discover 20,780+ production-ready servers for PostgreSQL, GitHub, Docker, Slack, and cloud APIs.