Everything you need to build MCP servers that expose tools, resources, and prompts to AI assistants. Covers both the TypeScript (@modelcontextprotocol/sdk) and Python (mcp) SDKs with production-ready patterns. Bookmark this and start building.
| Name | Syntax | Description |
|---|---|---|
| Create Server | new McpServer({ name, version }) | Initialize an MCP server instance with a name and version string. |
| Define Tool | server.tool(name, schema, handler) | Register a tool with a Zod schema for parameters and an async handler function. |
| Define Resource | server.resource(name, uri, handler) | Expose a readable resource at a URI pattern for clients to fetch. |
| Define Prompt | server.prompt(name, schema, handler) | Register a reusable prompt template with optional parameters. |
| Stdio Transport | new StdioServerTransport() | Connect server via stdin/stdout for local process communication. |
| SSE Transport | new SSEServerTransport('/messages', res) | Connect server via Server-Sent Events for HTTP-based communication. |
| Streamable HTTP | new StreamableHTTPServerTransport({ sessionIdGenerator }) | Modern HTTP transport with session management and bidirectional streaming. |
| Resource Template | server.resource(name, new ResourceTemplate(pattern, { list }), handler) | Define dynamic resources with URI template patterns like 'users://{id}'. |
| Python Decorator | @mcp.tool() | Python decorator syntax to register a function as an MCP tool. |
| Run Server | mcp.run(transport='stdio') | Start the Python MCP server with the specified transport. |
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'Initialize a TypeScript MCP server with stdio transport. Install with: npm install @modelcontextprotocol/sdk zod
Tips
from mcp.server.fastmcp import FastMCPInitialize a Python MCP server using the FastMCP high-level API. Install with: pip install mcp
Tips
package.json + tsconfig.jsonMinimal package.json and tsconfig.json for a TypeScript MCP server project.
Tips
server.tool(name, schema, handler)Define a tool with typed parameters using Zod schemas. The handler receives validated parameters and returns content.
Tips
return { content, isError: true }Return isError: true to signal tool execution failure to the AI without crashing the server.
Tips
@mcp.tool()Python tools use type annotations for schema generation. The Context parameter provides logging and progress reporting.
Tips
server.resource(name, uri, handler)Expose a static resource at a fixed URI. Resources are read-only data the AI can fetch on demand.
Tips
new ResourceTemplate(pattern, { list })Resource templates expose parameterized resources. The list callback enables discovery of available resources.
Tips
@mcp.resource(uri)Python resources use decorator syntax with URI patterns. Template variables are passed as function parameters.
Tips
server.prompt(name, schema, handler)Prompts are reusable message templates the AI can invoke. They can read files and build complex multi-turn messages.
Tips
messages: [{ role, content }]Multi-turn prompts seed conversations with example exchanges to guide the AI's response style and depth.
Tips
@mcp.prompt()Python prompts return either a string (single user message) or a list of message dicts for multi-turn templates.
Tips
new StdioServerTransport()Stdio transport communicates over stdin/stdout. Best for local tools launched as child processes by the AI client.
Tips
new SSEServerTransport('/messages', res)SSE transport exposes the MCP server over HTTP. Useful for remote servers and web-based clients.
Tips
new StreamableHTTPServerTransport({ sessionIdGenerator })Streamable HTTP is the modern transport supporting sessions, bidirectional streaming, and stateless operation.
Tips
npx @modelcontextprotocol/inspectorThe MCP Inspector is a visual testing tool that connects to your server and lets you call tools, read resources, and invoke prompts interactively.
Tips
InMemoryTransport.createLinkedPair()Use InMemoryTransport for fast unit tests without spawning processes. Create linked client-server pairs for direct communication.
Tips
claude_desktop_config.jsonRegister your MCP server with Claude Desktop for end-to-end testing. Restart Claude Desktop after editing.
Tips
A complete MCP server that wraps the GitHub REST API. Demonstrates the common pattern of wrapping an authenticated API with typed tools: one for reading (list-issues) and one for writing (create-issue). Uses environment variables for secrets and includes error handling.
A Python MCP server that provides database access through a resource (schema inspection) and a tool (read-only queries). The schema resource lets the AI understand table structures, while the query tool enforces SELECT-only access. Uses asyncpg for PostgreSQL with connection pooling.
A file system MCP server with path traversal protection. Exposes files as browsable resources with a template pattern, plus tools for searching and reading files. The safePath function prevents escaping the allowed root directory. This is the foundation pattern for any server that needs to give AI access to local files.
console.log in a stdio server corrupts the JSON-RPC stream because stdout is used for protocol messages
Use console.error for all logging in stdio servers. The MCP protocol uses stdout exclusively for JSON-RPC messages. Any non-protocol data on stdout will cause parsing errors and disconnect the client.
Tool handlers that throw exceptions crash the server instead of reporting the error to the AI
Always wrap tool handler logic in try/catch and return { content: [{ type: 'text', text: error.message }], isError: true } instead of throwing. This keeps the server running and lets the AI handle the error gracefully.
Forgetting to use .js extensions in TypeScript imports causes 'module not found' errors at runtime with NodeNext resolution
Always use .js extensions in import paths even in .ts files: import { foo } from './utils.js'. TypeScript with NodeNext module resolution requires this because the compiled JS files need .js extensions.
Resource list callbacks that return thousands of items overwhelm the AI's context window and cause slow responses
Limit list callback results to 50-100 items. Implement pagination or filtering in the list callback. For large datasets, provide a search tool instead of listing everything as resources.
Python FastMCP tools with synchronous functions block the event loop, causing timeouts on concurrent requests
Use async def for all tool and resource handlers that perform I/O (network requests, file reads, database queries). FastMCP runs on asyncio, so blocking calls freeze the entire server until they complete.