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/目录下)
包
此单体仓库发布拆分的包:
@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();准备构建真实应用?请按照分步教程操作:
构建你的第一个服务器 — 一个基于 stdio 的天气预警服务器,从
npm init到工具调用构建你的第一个客户端 — 连接到该服务器,列出其工具并进行调用
有关教程之外可运行、端到端的示例,请参阅:
examples/README.md— 可运行、可自验证的客户端/服务器示例对(每个目录一个故事)
文档
构建服务器 — 你的第一个 MCP 服务器,逐步操作
构建客户端 — 你的第一个 MCP 客户端,逐步操作
文档网站 — 完整指南:工具、资源、提示、通过 HTTP 和 stdio 提供服务、客户端、OAuth 以及迁移
故障排除 — 常见错误及其修复
本地构建文档
要在本地处理文档网站:
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/)This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Connectors
A TypeScript MCP server for Home Assistant, enabling programmatic management of entities, automati…
A comprehensive Model Context Protocol (MCP) server that enables AI assistants to interact with yo…
MCP (Model Context Protocol) server for Appwrite
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/dilipbaviskar/mcp-server-sdk-typescript'
If you have feedback or need assistance with the MCP directory API, please join our Discord server