typescript-sdk

modelcontextprotocol活跃维护

Model Context Protocol 官方 TypeScript SDK,用来快速搭建 MCP 服务端和客户端,让大模型应用以统一协议接入外部工具与数据源。

The official TypeScript SDK for Model Context Protocol servers and clients

v2.4.0发布于 2026-10-23 18:32 UTC13,739star +9 / 7 天2,377fork同步于 2026-11-10 05:27 UTC

怎么上手

由 README 提炼,执行前请确认
npm install @modelcontextprotocol/server
npm install @modelcontextprotocol/client
硬件要求按 README 说明

README 未说明,仅要求 Node.js、Bun 或 Deno 运行时

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

MCP 官方 TypeScript SDK,用统一协议把工具和上下文接给 LLM

Model Context Protocol 官方 TypeScript SDK,用来快速搭建 MCP 服务端和客户端,让大模型应用以统一协议接入外部工具与数据源。

核心能力

  • 提供 server 与 client 两套库,实现 MCP 规范
  • 支持 stdio 与 Streamable HTTP 两种传输方式
  • 内置 OAuth 与认证辅助工具
  • 工具和提示词 schema 兼容 Zod v4、Valibot、ArkType
  • 提供 Express、Fastify、Hono、Node HTTP 中间件适配包

容易踩的坑

v2 拆成 @modelcontextprotocol/server 和 @modelcontextprotocol/client,旧包名 @modelcontextprotocol/sdk 属于 v1.x 分支;使用 Express、Fastify、Hono 集成时需额外安装对应框架和中间件包;schema 需自备 Zod v4 等兼容 Standard Schema 的库

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v2.4.0
  1. v2.4.02.4.0最新正式版2026-10-23 · 2026-10-23 18:32 UTC
    PackageVersion
    @modelcontextprotocol/client2.4.0
    @modelcontextprotocol/server2.4.0
    @modelcontextprotocol/core2.4.0
    @modelcontextprotocol/server-legacy2.4.0
    @modelcontextprotocol/codemod2.4.0
    @modelcontextprotocol/node2.1.2
    @modelcontextprotocol/express, hono, fastifyunchanged

    Upgrade notes

    • x-mcp-header on a number parameter is rejected, as the 2026-07-28 spec requires: the client does not list such a tool, and the server answers a call to it with -32020. Use z.int() or remove the annotation. (#2762, #2960)
    • outputSchema from z.object(...).meta({ id }). On a 2025-11-25 connection the result is no longer wrapped in { result }: read structuredContent, not structuredContent.result. (#3081)
    • The bundled Ajv validator uses fast-uri 3.1.8. It rejects a $id or $ref with an invalid URI scheme, such as my_tool:output. (#3083)

    New

    • Skills over MCP (SEP-2640). A server publishes a folder of skills with loadSkills(folder) and skills.attach(server); a client lists, reads and verifies them with SkillsClient. It works on 2025-11-25 and 2026-07-28. attach is experimental and may change in a minor release. (#3047, #3080)
    • Opt-in scope on token refresh. Implement scopeForRefresh(tokens) on your OAuthClientProvider for an authorization server that rejects a refresh without scope (Microsoft Entra ID, AADSTS90009). The default request does not change. (#3085)
    • ServerEvent takes optional params, so a gateway can forward change notifications with their _meta. (#3057)
    • The v1 to v2 upgrade guide is also an installable agent skill. (#2968)

    Fixes

    • update({ title }) on a resource or resource template shows up in list responses. (#3005)
    • A Server connected to a per-request transport at a modern revision binds that revision instead of answering -32022. (#3057)
    • createMcpHandler sends Allow: POST on every 405. (#3002)
    • @modelcontextprotocol/node accepts @hono/node-server 1.x or 2.x; a new install resolves 2.x. (#3084)
  2. @modelcontextprotocol/[email protected]2026-10-23 · 2026-10-23 18:19 UTC
    查看更新说明

    Patch Changes

  3. @modelcontextprotocol/[email protected]2026-10-23 · 2026-10-23 18:19 UTC
    查看更新说明

    Minor Changes

    • #3057 c05225a Thanks @steipete! - ServerEvent takes optional params, which the subscriptions/listen router delivers with its own subscription ID, so a gateway can forward change notifications with their _meta and extension params.

    • #3047 46d9c99 Thanks @KKonstantinov, @tobi-oye, @mattzcarey! - Add the Skills extension (SEP-2640), reachable through new ext/skills subpaths; package root exports are unchanged.

      • @modelcontextprotocol/server/ext/skills: SkillsExtension (attach(server) serves skills/list, skills/get, resources/directory/read and a skill://{+path} resource template), SkillsExtension.load(files) returning a SkillCatalog with entries and reload(), the SkillFiles storage adapter with memorySkillFiles, SkillSource for generated catalogs, skillEntry, and SkillCatalogError. Invalid skills are skipped with a warning (onWarning), or fail under strict. A file whose bytes are valid UTF-8 is served as text, whatever its MIME type; any other file as a base64 blob.
      • @modelcontextprotocol/server/ext/skills/node: loadSkills(folder) and nodeSkillFiles(folder).
      • @modelcontextprotocol/client/ext/skills: SkillsClient with list() (throws on an invalid entry; skipInvalid: true keeps the valid ones and returns the rest in invalid), get(), read(skill, path?) (size, digest and SKILL.md frontmatter verified, SkillVerificationError on a mismatch; a copy from the response cache that fails a check is fetched once more with cacheMode: 'refresh' first) and readDirectory().
      • @modelcontextprotocol/core/ext/skills: schemas, types and constants, including SKILL_LIMITS. The result schemas follow the stable spec, so ttlMs and cacheScope are required.

      SkillsExtension.attach is @experimental: how an extension is wired to a server may change in a minor release. The rest of the Skills API is stable.

      Works on 2025-11-25 and 2026-07-28. On 2026-07-28 only, the server sends the caching fields (ttlMs, cacheScope), and SkillsClient requires them and returns them from list(), get() and readDirectory().

    Patch Changes

    • #3083 ef38c67 Thanks @claude! - The bundled Ajv validator is now built with fast-uri 3.1.8 (was 3.1.0). fast-uri 3.1.6 and later reject a $id or $ref whose text before the first colon is not a valid URI scheme (my_tool:output), or whose URN namespace is not valid (urn:my_tool:output): a schema that uses one no longer compiles, and a tool that carries it fails with an invalid-schema error. An underscore without a colon (my_tool_output) and a $ref into #/$defs/... or #/definitions/... are not affected.

    • #2960 43c45f4 Thanks @claude! - A server whose tool puts x-mcp-header on a number-typed parameter keeps checking that tool's Mcp-Param-* headers on tools/call, as 2.3.0 does: a header that is missing for a value in the body, or that disagrees with the body, is answered 400 with JSON-RPC error -32020. Clients on a 2026-07-28 connection other than stdio no longer list such a tool

  4. @modelcontextprotocol/[email protected]2026-10-23 · 2026-10-23 18:19 UTC
    查看更新说明

    Patch Changes

  5. 1.32.22026-10-23 · 2026-10-23 18:06 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/modelcontextprotocol/typescript-sdk/compare/1.32.1...1.32.2

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    MCP TypeScript SDK

    [!IMPORTANT] This is the main branch: v2 of the SDK (@modelcontextprotocol/server, @modelcontextprotocol/client), the stable release line, implementing the 2026-07-28 MCP spec. Start with the v2 documentation; coming from v1, see the migration guide.

    v1.x (@modelcontextprotocol/sdk) lives on the v1.x branch and continues to receive bug fixes and security updates for at least six months after the v2 release (2026-07-27). Its documentation is at ts.sdk.modelcontextprotocol.io.

    Found a problem in v2? Open an issue.

    [!NOTE] Contributors without write access can have one open pull request at a time. Draft pull requests don't count toward the limit. For a feature or a larger change, please open an issue first; see CONTRIBUTING.md.

    NPM Version - Server NPM Version - Client Apache-2.0 licensed

    Overview

    The Model Context Protocol (MCP) allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction.

    This repository contains the TypeScript SDK implementation of the MCP specification. It runs on Node.js, Bun, and Deno, and ships:

    • MCP server libraries (tools/resources/prompts, Streamable HTTP, stdio, auth helpers)
    • MCP client libraries (transports, high-level helpers, OAuth helpers)
    • Optional middleware packages for specific runtimes/frameworks (Express, Fastify, Hono, Node.js HTTP)
    • Runnable examples (under examples/)

    Packages

    This monorepo publishes split packages:

    • @modelcontextprotocol/server: build MCP servers
    • @modelcontextprotocol/client: build MCP clients

    Tool and prompt schemas use Standard Schema — bring Zod v4, Valibot, ArkType, or any compatible library.

    Middleware packages (optional)

    The SDK also publishes small "middleware" packages under packages/middleware/ that help you wire MCP into a specific runtime or web framework.

    They are intentionally thin adapters: they should not introduce new MCP functionality or business logic. See packages/middleware/README.md for details.

    • @modelcontextprotocol/node: Node.js Streamable HTTP transport wrapper for IncomingMessage / ServerResponse
    • @modelcontextprotocol/express: Express helpers (app defaults + Host header validation)
    • @modelcontextprotocol/fastify: Fastify helpers (app defaults + Host header validation)
    • @modelcontextprotocol/hono: Hono helpers (app defaults + JSON body parsing hook + Host header validation)

    Installation

    Server

    npm install @modelcontextprotocol/server
    # or
    bun add @modelcontextprotocol/server
    # or
    deno add npm:@modelcontextprotocol/server
    

    Client

    npm install @modelcontextprotocol/client
    # or
    bun add @modelcontextprotocol/client
    # or
    deno add npm:@modelcontextprotocol/client
    

    Optional middleware packages

    The SDK also publishes optional “middleware” packages that help you wire MCP into a specific runtime or web framework (for example Express, Fastify, Hono, or Node.js http).

    These packages are intentionally thin adapters and should not introduce additional MCP features or business logic. See packages/middleware/README.md for details.

    # Node.js HTTP (IncomingMessage/ServerResponse) Streamable HTTP transport:
    npm install @modelcontextprotocol/node
    
    # Express integration:
    npm install @modelcontextprotocol/express express
    
    # Fastify integration:
    npm install @modelcontextprotocol/fastify fastify
    
    # Hono integration:
    npm install @modelcontextprotocol/hono hono
    

    Getting Started

    Here is what an MCP server looks like. This minimal example exposes a single greet tool over stdio:

    import { McpServer } from '@modelcontextprotocol/server';
    import { StdioServerTransport } from '@modelcontextprotocol/server/stdio';
    import * as z from 'zod/v4';
    
    const server = new McpServer({ name: 'greeting-server', version: '1.0.0' });
    
    server.registerTool(
        'greet',
        {
            description: 'Greet someone by name',
            inputSchema: z.object({ name: z.string() })
        },
        async ({ name }) => ({
            content: [{ type: 'text', text: `Hello, ${name}!` }]
        })
    );
    
    async function main() {
        const transport = new StdioServerTransport();
        await server.connect(transport);
    }
    
    main();
    

    Ready to build something real? Follow the step-by-step tutorials:

    For runnable, end-to-end examples beyond the tutorials, see:

    • examples/README.md — runnable, self-verifying client/server example pairs (one story per directory)

    Documentation

    Building docs locally

    To work on the documentation site locally:

    pnpm docs:api      # Generate the API reference markdown (output: docs/api/)
    pnpm docs:dev      # Start the VitePress dev server for the V2 site
    pnpm docs:build    # Build the V2 site (output: docs/.vitepress/dist/)
    pnpm docs:multi    # Build the combined V1 + V2 site (output: tmp/docs-combined/)
    

    The docs:multi script builds the V2 site from the current checkout, checks out the v1.x branch via a git worktree to build the V1 site, and produces a combined site with V1 docs at the root and V2 docs under /v2/.

    v1 (legacy) documentation and fixes

    If you are using the v1 generation of the SDK, the v1 API documentation is available at https://ts.sdk.modelcontextprotocol.io/. The v1 source code and any v1-specific fixes live on the long-lived v1.x branch. V2 API docs are at /v2/.

    Contributing

    Issues and pull requests are welcome on GitHub at https://github.com/modelcontextprotocol/typescript-sdk.

    License

    This project is licensed under the Apache License 2.0 for new contributions, with existing code under MIT. See the LICENSE file for details.