Skip to main content
Glama
dilipbaviskar

MCP TypeScript SDK

MCP TypeScript SDK

概述

模型上下文协议 (MCP) 允许应用程序以标准化方式为 LLM 提供上下文,将提供上下文与实际的 LLM 交互的关注点分离开来。

此仓库包含 MCP 规范的 TypeScript SDK 实现。它可在 Node.js、Bun 和 Deno 上运行,并包含:

  • MCP 服务器库(工具/资源/提示、Streamable HTTP、stdio、认证辅助工具)

  • MCP 客户端库(传输层、高级辅助工具、OAuth 辅助工具)

  • 针对特定运行时/框架的可选中间件包(Express、Fastify、Hono、Node.js HTTP)

  • 可运行的示例(位于 examples/ 目录下)

Related MCP server: MCP - Model Context Protocol TypeScript SDK

包

此单体仓库发布拆分的包:

  • @modelcontextprotocol/server:构建 MCP 服务器

  • @modelcontextprotocol/client:构建 MCP 客户端

工具和提示模式使用 Standard Schema — 可引入 Zod v4、Valibot、ArkType 或任何兼容库。

这些包是刻意设计的薄适配层:它们不应引入新的 MCP 功能或业务逻辑。详见 packages/middleware/README.md。

  • @modelcontextprotocol/node:针对 IncomingMessage / ServerResponse 的 Node.js Streamable HTTP 传输封装

  • @modelcontextprotocol/express:Express 辅助工具(应用默认值 + Host 头验证)

  • @modelcontextprotocol/fastify:Fastify 辅助工具(应用默认值 + Host 头验证)

  • @modelcontextprotocol/hono:Hono 辅助工具(应用默认值 + JSON 主体解析钩子 + Host 头验证)

安装

服务器

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

客户端

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

可选的中间件包

SDK 还发布了可选的“中间件”包,帮助您将 MCP 接入特定的运行时或 Web 框架(例如 Express、Fastify、Hono 或 Node.js http)。

这些包是刻意设计的薄适配层,不应引入额外的 MCP 功能或业务逻辑。详见 packages/middleware/README.md。

# 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

快速上手

以下是一个 MCP 服务器的示例。这个最小化示例通过 stdio 暴露了一个单一的 greet 工具:

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();

准备构建真实应用?请按照分步教程操作:

有关教程之外可运行、端到端的示例,请参阅:

  • examples/README.md — 可运行、可自验证的客户端/服务器示例对(每个目录一个故事)

文档

本地构建文档

要在本地处理文档网站:

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/)

Maintenance

ActivityMaintained
ResponsivenessSyncing

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A TypeScript wrapper library for the Model Context Protocol SDK that provides a simplified interface for creating MCP servers with tools, resources, and prompts without needing to work directly with the protocol.
    19 npm
    AGPL 3.0
  • A
    license
    A
    quality
    D
    maintenance
    A starter project for rapidly developing Model Context Protocol servers using TypeScript and the official SDK. It includes pre-implemented examples of tools and resources to help developers jumpstart their custom MCP server development.
    6
    ISC
  • A
    license
    B
    quality
    D
    maintenance
    A TypeScript MCP server boilerplate providing example tools and resources for rapid development of custom Model Context Protocol servers.
    8
    ISC