MCP & Connectors
How do you build a custom MCP server for Claude?
QUICK ANSWER
To build a custom MCP server for Claude, use the official TypeScript or Python SDK, define the schemas for the tools, resources, and prompt formats, and write standard stdio or SSE transport handlers to process requests.
TypeScript Implementation Example
Here is how you build a minimal custom MCP server in Node.js that exposes a custom calculator tool:
- Initialize a project:
npm init -y && npm install @modelcontextprotocol/sdk. - Write the server code (e.g.
server.ts):
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { ListToolsRequestSchema, CallToolRequestSchema } from "@modelcontextprotocol/sdk/types.js";
const server = new Server({
name: "custom-math-server",
version: "1.0.0"
}, {
capabilities: { tools: {} }
});
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [{
name: "calculate_cube",
description: "Calculates the cube of a number.",
inputSchema: {
type: "object",
properties: {
number: { type: "number" }
},
required: ["number"]
}
}]
}));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === "calculate_cube") {
const number = request.params.arguments?.number as number;
return {
content: [{ type: "text", text: String(number * number * number) }]
};
}
throw new Error("Tool not found");
});
const transport = new StdioServerTransport();
await server.connect(transport);
Verified against: MCP SDK v0.7.0