用于在 TypeScript 中创建模型上下文协议(MCP)服务器的模板。此模板提供了一个坚实的基础,用于构建具有适当工具、类型安全性和最佳实践的 MCP 兼容服务器。
安装依赖项:
npm install
启动带有热重载的开发服务器:
npm run dev
构建项目:
npm run build
运行测试:
npm test
启动生产服务器:
npm start
src/
├── index.ts # 入口点
├── interfaces/ # 接口定义
│ └── tool.ts # 数据处理器接口
└── tools/ # 工具实现
└── example.ts # 示例工具
根据 src/tools/example.ts 中的示例导出你的工具和处理程序:
// 在 your-tool.ts 中
export const YOUR_TOOLS = [
{
name: "your-tool-name",
description: "Your tool description",
parameters: {
// 你的工具参数模式
},
},
];
export const YOUR_HANDLERS = {
"your-tool-name": async (request) => {
// 你的工具处理程序实现
return {
toolResult: {
content: [{ type: "text", text: "结果" }],
},
};
},
};
在 src/index.ts 中的 ALL_TOOLS 和 ALL_HANDLERS 常量中注册你的工具:
// 在 src/index.ts 中
import { YOUR_TOOLS, YOUR_HANDLERS } from "./tools/your-tool.js";
// 组合所有工具
const ALL_TOOLS = [...EXAMPLE_TOOLS, ...YOUR_TOOLS];
const ALL_HANDLERS = { ...EXAMPLE_HANDLERS, ...YOUR_HANDLERS };
服务器将自动:
模板包括一个内置的 TestClient 用于本地测试和 MCP Inspector 用于可视化调试。
TestClient 提供了一种简单的方式来测试你的工具:
import { TestClient } from "./utils/TestClient";
describe("YourTool", () => {
const client = new TestClient();
it("应该正确处理数据", async () => {
await client.assertToolCall(
"your-tool-name",
{ input: "test" },
(result) => {
expect(result.toolResult.content).toBeDefined();
}
);
});
});
模板包括 MCP Inspector 用于你的工具的可视化调试:
启动 inspector:
npx @modelcontextprotocol/inspector node dist/index.js
在 http://localhost:5173 打开 inspector UI
Inspector 提供了:
要使用 Cursor 进行本地 MCP 服务器测试:
构建并链接包:
npm run build
npm run link
验证二进制文件是否工作:
npx example-mcp-tool
将服务器添加到 Cursor:
npx example-mcp-tool通过检查 MCP 服务器部分显示你的服务器正在运行来验证 Cursor 中的服务器是否启动正确。
注意:如果你修改了代码,请记得重新构建和重新链接:
npm run build
npm run link
完成测试后,你可以取消链接包:
npm run unlink
这将删除开发期间创建的全局符号链接。 </中文翻译>