关于 Model Context Protocol (MCP) Implementation
Model Context Protocol (MCP) Implementation 是用于学习和构建 MCP 服务端与客户端的模块化框架。它提供协议处理、stdio 和 HTTP+SSE 传输,以及可运行的计算器示例用于测试。
主要功能
- 01JSON-RPC 2.0 与协议初始化
- 02能力协商和工具注册
- 03JSON Schema 验证与错误处理
- 04STDIO 和 HTTP+SSE 传输
- 05示例服务端、客户端与计算器工具
- 06测试客户端和 MCP Inspector 调试
适用场景
- 01通过可运行实现学习 MCP
- 02使用自定义工具构建 MCP 服务端
- 03测试两种传输方式下的客户端与服务端通信
MCP 相关
实现 MCP 服务端和客户端组件,包括工具发现与调用、标准化结果及错误处理。支持 STDIO 和 HTTP+SSE 传输。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
Model Context Protocol (MCP) 实现
本项目实现了用于构建 AI 工具的 Model Context Protocol (MCP)。它提供了一个模块化框架,可用于创建兼容 MCP 的服务端和客户端。
🔍 什么是 Model Context Protocol (MCP)?
Model Context Protocol (MCP) 是一种开放协议,可让 AI 助手与外部工具和数据源交互。
主要功能
- 列出可用工具及其能力
- 使用参数调用工具
- 以一致的方式处理错误
- 以标准化格式处理工具结果
📚 如需详细概述,请参阅 MCP 说明。
✨ 功能
| 类别 | 功能 |
|---|---|
| 核心 | ✅ JSON-RPC 2.0 消息处理 ✅ 协议初始化 ✅ 能力协商 |
| 工具 | ✅ 使用 JSON Schema 注册工具 ✅ 工具调用与验证 ✅ 标准化错误处理 |
| 传输 | ✅ 支持 STDIO ✅ 支持 HTTP+SSE |
| 测试 | ✅ 测试客户端 |
📁 项目结构
src/
├── core/ # Core MCP server implementation
├── transports/ # Transport layer implementations (stdio, HTTP+SSE)
├── tools/ # Tool definitions and handlers
├── examples/ # Example servers and clients
│ └── public/ # Static files for HTTP server
└── index.js # Main entry point for the library
🚀 快速入门
前置条件
- Node.js 20.x 或更高版本
- npm 或 pnpm
安装
# Clone the repository
git clone https://github.com/AshikNesin/learn-mcp-by-building
cd learn-mcp-by-building
# Install dependencies
npm install
# or
pnpm install
🏃♂️ 运行示例
STDIO 服务端和客户端
运行 STDIO 服务端:
npm run server:stdio
# or
node src/examples/stdio-server.js
使用 STDIO 客户端进行测试:
npm run client:stdio
# or
node src/examples/stdio-client.js
同时运行二者以查看完整测试:
npm run test:stdio
# or
node src/examples/stdio-client.js | node src/examples/stdio-server.js
HTTP+SSE 服务端和客户端
运行 HTTP+SSE 服务端:
npm run server:sse
# or
node src/examples/http-sse-server.js --port 5000
可用选项:
--port:监听端口(默认值:5000)--host:绑定主机(默认值:localhost)--path:端点路径(默认值:/sse)--cors:启用 CORS(默认值:true)--serve-static:从 src/examples/public 提供静态文件(默认值:true)
使用 HTTP+SSE 客户端进行测试:
npm run client:sse
# or
node src/examples/http-sse-client.js --server http://localhost:5000/sse
运行后,你还可以在浏览器中访问 http://localhost:5000 打开 Web 客户端界面:

该界面提供了与 MCP 服务交互的便捷方式,采用并排布局,展示计算器控件和实时日志。
🔍 使用 MCP Inspector
你可以使用官方 MCP Inspector 调试服务端:
npm run debug
MCP Inspector 提供可视化界面,用于监控和调试 MCP 服务端:

🧮 计算器工具
|
运算
|
参数
|
错误处理
|
🧑💻 使用 MCP 框架进行开发
创建新的服务端
import { McpServer } from '../core/index.js';
import { StdioTransport } from '../transports/index.js';
import { calculatorToolDefinition, handleCalculatorTool } from '../tools/index.js';
// Create server instance
const server = new McpServer(
{ name: 'my-server', version: '1.0.0' },
{ capabilities: { tools: { listChanged: true } } }
);
// Register tool handlers
server.setRequestHandler('tools/list', () => ({ tools: [calculatorToolDefinition] }));
server.setRequestHandler('tools/call', async (params) => {
if (params.name === 'calculator') {
return handleCalculatorTool(params.arguments);
}
throw new Error(`Tool ${params.name} not found`);
});
// Start the server
const transport = new StdioTransport();
server.connect(transport)
.then(() => console.error('Server ready!'));
创建新的工具
- 在
src/tools/中创建一个新文件:
// src/tools/my-tool.js
export const myToolDefinition = {
name: 'my-tool',
description: 'Description of my tool',
inputSchema: {
type: 'object',
properties: {
// Define parameters
},
required: []
}
};
export async function handleMyTool(args) {
// Implement tool logic
return {
content: [
{
type: 'text',
text: 'Result from my tool'
}
]
};
}
- 在
src/tools/index.js中导出该工具:
export * from './my-tool.js';
🛠️ 协议功能
- ✅ 能力协商
- ✅ 工具列表变更通知
- ✅ 标准化错误处理
- ✅ JSON Schema 验证
- ✅ 结构化工具结果
- ✅ 传输层抽象
📚 外部资源
📝 许可证
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。