📄 README.md

← 返回目录

MCP TypeScript SDK ![NPM Version](https://www.npmjs.com/package/@modelcontextprotocol/sdk) ![MIT licensed](https://github.com/modelcontextprotocol/typescript-sdk/blob/main/LICENSE)

Table of Contents

- Overview

Overview

The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This TypeScript SDK implements the full MCP specification, making it easy to:

- Create MCP servers that expose resources, prompts and tools

This SDK has a required peer dependency on zod for schema validation. The SDK internally imports from zod/v4, but maintains backwards compatibility with projects using Zod v3.25 or later. You can use either API in your code by importing from zod/v3 or zod/v4:

Quick Start

To see the SDK in action end-to-end, start from the runnable examples in src/examples:

1. Install dependencies (from the SDK repo root):

    npm install
    

2. Run the example Streamable HTTP server:

    npx tsx src/examples/server/simpleStreamableHttp.ts
    

3. Run the interactive client in another terminal:

    npx tsx src/examples/client/simpleStreamableHttp.ts
    

This pair of examples demonstrates tools, resources, prompts, sampling, elicitation, tasks and logging. For a guided walkthrough and variations (stateless servers, JSON-only responses, SSE compatibility, OAuth, etc.), see docs/server.md and docs/client.md.

Core Concepts

Servers and transports

An MCP server is typically created with McpServer and connected to a transport such as Streamable HTTP or stdio. The SDK supports:

- Streamable HTTP for remote servers (recommended).

| --------------------------------------------------- | ------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ | | Streamable HTTP server (stateful) | Feature-rich server with tools, resources, prompts, logging, tasks, sampling, and optional OAuth. | simpleStreamableHttp.ts | server.md, capabilities.md | | Streamable HTTP server (stateless) | No session tracking; good for simple API-style servers. | simpleStatelessStreamableHttp.ts | server.md | | JSON response mode (no SSE) | Streamable HTTP with JSON responses only and limited notifications. | jsonResponseStreamableHttp.ts | server.md | | Server notifications over Streamable HTTP | Demonstrates server-initiated notifications using SSE with Streamable HTTP. | standaloneSseWithGetStreamableHttp.ts | server.md | | Deprecated HTTP+SSE server | Legacy HTTP+SSE transport for backwards-compatibility testing. | simpleSseServer.ts | server.md | | Backwards-compatible server (Streamable HTTP + SSE) | Single server that supports both Streamable HTTP and legacy SSE clients. | sseAndStreamableHttpCompatibleServer.ts | server.md | | Form elicitation server | Uses form elicitation to collect non-sensitive user input. | elicitationFormExample.ts | capabilities.md | | URL elicitation server | Demonstrates URL-mode elicitation in an OAuth-protected server. | elicitationUrlExample.ts | capabilities.md | | Sampling and tasks server | Combines tools, logging, sampling, and experimental task-based execution. | toolWithSampleServer.ts | capabilities.md | | OAuth demo authorization server | In-memory OAuth provider used with the example servers. | demoInMemoryOAuthProvider.ts | server.md |

Client examples

| Scenario | Description | Example file(s) | Related docs | | --------------------------------------------------- | ---------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------ | | Interactive Streamable HTTP client | CLI client that exercises tools, resources, prompts, elicitation, and tasks. | simpleStreamableHttp.ts | client.md | | Backwards-compatible client (Streamable HTTP → SSE) | Tries Streamable HTTP first, then falls back to SSE on 4xx responses. | streamableHttpWithSseFallbackClient.ts | client.md, server.md | | SSE polling client | Polls a legacy SSE server and demonstrates notification handling. | ssePollingClient.ts | client.md | | Parallel tool calls client | Shows how to run multiple tool calls in parallel. | parallelToolCallsClient.ts | client.md | | Multiple clients in parallel | Demonstrates connecting multiple clients concurrently to the same server. | multipleClientsParallel.ts | client.md | | OAuth clients | Examples of client_credentials (basic and private_key_jwt) and reusable providers. | simpleOAuthClient.ts, simpleOAuthClientProvider.ts, simpleClientCredentials.ts | client.md | | URL elicitation client | Works with the URL elicitation server to drive secure browser flows. | elicitationUrlExample.ts | capabilities.md |

Shared utilities:

- In-memory event store for resumability: inMemoryEventStore.ts (see server.md).

For more details on how to run these examples (including recommended commands and deployment diagrams), see src/examples/README.md.

Documentation

- Local SDK docs: - docs/server.md – building and running MCP servers, transports, tools/resources/prompts, CORS, DNS rebinding, and multi-node deployment. - docs/client.md – using the high-level client, transports, backwards compatibility, and OAuth helpers. - docs/capabilities.md – sampling, elicitation (form and URL), and experimental task-based execution. - docs/protocol.md – protocol features: ping, progress, cancellation, pagination, capability negotiation, and JSON Schema. - docs/faq.md – environment and troubleshooting FAQs (including Node.js Web Crypto support).

- V1 API reference - V2 API reference - Model Context Protocol documentation - MCP Specification - Example Servers

Contributing

Issues and pull requests are welcome on GitHub at .

License

This project is licensed under the MIT License—see the LICENSE file for details.