Comprehensive guide for creating Model Context Protocol (MCP) servers. Use when building MCP servers, integrating external APIs, or creating tool interfaces for LLMs.
npx playbooks add skill langconfig/langconfig --skill mcp-builder
You are an expert MCP server developer. Follow this four-phase process when creating MCP servers:
Before writing code, thoroughly understand:
get_, create_, update_, delete_, list_, search_my-mcp-server/
├── src/
│ ├── index.ts # Entry point
│ ├── tools/ # Tool implementations
│ │ ├── index.ts
│ │ └── [feature].ts
│ ├── types/ # Type definitions
│ └── utils/ # Helpers (auth, pagination)
├── package.json
└── tsconfig.json
1. Tool Definition with Zod Schema:
import { z } from "zod";
const GetUserSchema = z.object({
userId: z.string().describe("The unique user identifier"),
includeDetails: z.boolean().optional().describe("Include extended profile")
});
server.tool(
"get_user",
"Retrieve user profile by ID",
GetUserSchema,
async ({ userId, includeDetails }) => {
// Implementation
}
);
2. Error Handling:
try {
const response = await api.request(endpoint);
return { content: [{ type: "text", text: JSON.stringify(response) }] };
} catch (error) {
if (error.status === 429) {
return { content: [{ type: "text", text: "Rate limited. Retry in 60s." }] };
}
throw new McpError(ErrorCode.InternalError, error.message);
}
3. Pagination Helper:
async function* paginate<T>(fetcher: (cursor?: string) => Promise<PageResponse<T>>) {
let cursor: string | undefined;
do {
const page = await fetcher(cursor);
yield* page.items;
cursor = page.nextCursor;
} while (cursor);
}
4. Tool Annotations:
server.tool("delete_resource", "Permanently delete a resource", schema, handler, {
annotations: {
destructiveHint: true,
idempotentHint: false,
readOnlyHint: false
}
});
my-mcp-server/
├── src/
│ └── my_mcp_server/
│ ├── __init__.py
│ ├── server.py # Main server
│ └── tools/ # Tool modules
├── pyproject.toml
└── README.md
Python Tool Definition:
from mcp.server import Server
from pydantic import BaseModel, Field
class GetUserInput(BaseModel):
user_id: str = Field(description="The unique user identifier")
@server.tool()
async def get_user(input: GetUserInput) -> str:
"""Retrieve user profile by ID."""
user = await api.get_user(input.user_id)
return json.dumps(user)
# TypeScript
npm run build
# Python
python -m py_compile src/**/*.py
npx @anthropic/mcp-inspector
Create 10 complex, realistic questions that verify LLM effectiveness:
User asks: "Help me build an MCP server for the GitHub API"
Response approach:
list_repos, get_issue, search_code, get_pr_diffUser asks: "I need to integrate Slack with my agents"
Response approach:
send_message, list_channels, search_messages, upload_fileWeekly roundup of top Claude Code skills, MCP servers, and AI coding tips.