3.0 KiB
3.0 KiB
MCP Demo Server
一个使用 TypeScript 构建的 Model Context Protocol (MCP) 服务器示例,展示了如何创建和注册各种工具。
功能特性
此 MCP 服务器提供以下示例工具:
| 工具名称 | 描述 |
|---|---|
echo |
回显传入的消息 |
calculator |
执行基本数学运算(加、减、乘、除) |
get_current_time |
获取当前日期和时间(支持时区) |
random_number |
在指定范围内生成随机数 |
string_utils |
字符串处理工具(大小写转换、反转、计数等) |
快速开始
安装依赖
npm install
编译项目
npm run build
运行服务器
npm start
在 VS Code 中使用
项目已配置好 .vscode/mcp.json,可以直接在 VS Code 中使用此 MCP 服务器:
- 确保已编译项目 (
npm run build) - 在 VS Code 中打开此项目
- MCP 服务器会自动被识别并可用
配置示例
Claude Desktop 配置
将以下内容添加到 Claude Desktop 的配置文件中:
Windows: %APPDATA%\Claude\claude_desktop_config.json
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"mcp-demo": {
"command": "node",
"args": ["D:\\Projects\\McpDemo\\build\\index.js"]
}
}
}
工具使用示例
Echo 工具
输入: { "message": "Hello, MCP!" }
输出: "Echo: Hello, MCP!"
Calculator 工具
输入: { "operation": "add", "a": 10, "b": 5 }
输出: "10 add 5 = 15"
Get Current Time 工具
输入: { "timezone": "Asia/Shanghai" }
输出: "Current time: 1/16/2026, 2:30:00 PM"
Random Number 工具
输入: { "min": 1, "max": 100 }
输出: "Random number between 1 and 100: 42"
String Utils 工具
输入: { "operation": "uppercase", "text": "hello world" }
输出: "HELLO WORLD"
项目结构
McpDemo/
├── src/
│ └── index.ts # MCP 服务器主文件
├── build/ # 编译输出目录
├── .vscode/
│ └── mcp.json # VS Code MCP 配置
├── package.json
├── tsconfig.json
└── README.md
开发指南
添加新工具
在 src/index.ts 中使用 server.registerTool() 添加新工具:
server.registerTool(
"tool_name",
{
description: "工具描述",
inputSchema: {
param1: z.string().describe("参数1描述"),
param2: z.number().describe("参数2描述"),
},
},
async ({ param1, param2 }) => {
// 工具逻辑
return {
content: [
{
type: "text",
text: "结果",
},
],
};
}
);
开发模式
使用 watch 模式进行开发:
npm run dev
相关资源
许可证
MIT