|
| 1 | +--- |
| 2 | +name: mcp-protocol |
| 3 | +version: 2025.12.1 |
| 4 | +domain: protocol |
| 5 | +expires: 2026-06-01 |
| 6 | +activates_on: [mcp, model context protocol, tool, resource, prompt, server, transport, stdio, sse, streamable] |
| 7 | +sources: |
| 8 | + - https://modelcontextprotocol.io/docs |
| 9 | + - https://github.com/modelcontextprotocol/typescript-sdk |
| 10 | +context7: modelcontextprotocol/typescript-sdk |
| 11 | +--- |
| 12 | + |
| 13 | +# Model Context Protocol (MCP) |
| 14 | + |
| 15 | +## SDK (@modelcontextprotocol/sdk) |
| 16 | +```ts |
| 17 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 18 | +import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; |
| 19 | + |
| 20 | +const server = new McpServer({ name: 'my-server', version: '1.0.0' }); |
| 21 | +``` |
| 22 | + |
| 23 | +## Tools |
| 24 | +```ts |
| 25 | +server.tool('tool_name', 'description', { |
| 26 | + param: z.string().describe('param description'), // Zod schema |
| 27 | +}, async ({ param }) => { |
| 28 | + return { content: [{ type: 'text', text: result }] }; |
| 29 | +}); |
| 30 | +``` |
| 31 | +- Input schema: Zod objects auto-converted to JSON Schema |
| 32 | +- Return: `{ content: [{ type: 'text' | 'image' | 'resource', ... }] }` |
| 33 | +- Error: `{ isError: true, content: [{ type: 'text', text: errorMsg }] }` |
| 34 | + |
| 35 | +## Resources |
| 36 | +```ts |
| 37 | +server.resource('resource://uri', 'description', async (uri) => { |
| 38 | + return { contents: [{ uri, mimeType: 'text/plain', text: data }] }; |
| 39 | +}); |
| 40 | +``` |
| 41 | +- Static URI or template: `resource://users/{id}` |
| 42 | +- `list_changed` notification when resources update |
| 43 | + |
| 44 | +## Transports |
| 45 | +- **Stdio**: `StdioServerTransport` — for CLI-launched servers (Claude Code default) |
| 46 | +- **Streamable HTTP**: `StreamableHTTPServerTransport` — for networked servers |
| 47 | +- **SSE** (deprecated): use Streamable HTTP instead for new servers |
| 48 | + |
| 49 | +## Prompts |
| 50 | +```ts |
| 51 | +server.prompt('prompt_name', 'description', { arg: z.string() }, ({ arg }) => { |
| 52 | + return { messages: [{ role: 'user', content: { type: 'text', text: `...${arg}...` } }] }; |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +## Client Side |
| 57 | +```ts |
| 58 | +import { Client } from '@modelcontextprotocol/sdk/client/index.js'; |
| 59 | +const client = new Client({ name: 'my-client', version: '1.0.0' }); |
| 60 | +await client.connect(transport); |
| 61 | +const result = await client.callTool({ name: 'tool_name', arguments: { param: 'value' } }); |
| 62 | +``` |
| 63 | + |
| 64 | +## Gotchas |
| 65 | +- Tool names: snake_case convention (not camelCase) |
| 66 | +- Stdio transport: server MUST NOT write to stdout except MCP messages (use stderr for logs) |
| 67 | +- Zod schemas: `.describe()` on each field — LLMs use descriptions for tool calling |
| 68 | +- Error handling: return error content, don't throw (throws crash the server) |
| 69 | +- Transport cleanup: `await server.close()` on shutdown |
0 commit comments