MCP服务器模板
一个用于构建MCP(模型上下文协议)服务器的起始模板。这个模板提供了一个干净的基础,用于创建您自己的MCP服务器,该服务器可以与Claude、Cursor或其他兼容MCP的AI助手集成。
目的
这个模板能帮助你快速开始构建:
- 为AI助手定制的工具
- 动态内容的资源提供商
- 常见操作的提示模板
- 外部API和服务的集成点
特点/特性
- 简单的“hello-world”工具示例
- 带有适当类型定义的 TypeScript 支持
- 适用于不同MCP客户端的简易安装脚本
- 清晰的项目结构,随时准备进行定制化
它是如何工作的
这个MCP服务器模板提供:
- 使用MCP SDK进行基本服务器设置
- 示例工具实现
- 构建和安装脚本
- 用于开发的TypeScript配置
附带的示例展示了如何创建一个简单的工具,该工具接受一个名称参数并返回一条问候语。
入门指南/开始使用
# Clone the boilerplate
git clone
cd mcp-server-boilerplate
# Install dependencies
pnpm install
# Build the project
pnpm run build
# Start the server
pnpm start安装脚本
这个模板包含了针对不同MCP客户端的便捷安装脚本:
# For Claude Desktop
pnpm run install-desktop
# For Cursor
pnpm run install-cursor
# For Claude Code
pnpm run install-code
# Generic installation
pnpm run install-server这些脚本将构建项目并自动更新相应的配置文件。
与Claude Desktop的配合使用
安装脚本会自动添加配置,但您也可以手动将其添加到您的 claude_desktop_config.json 文件:
{
"mcpServers": {
"your-server-name": {
"command": "node",
"args": ["/path/to/your/dist/index.js"]
}
}
}然后重启Claude Desktop以连接到服务器。
定制您的服务器
添加工具
工具是AI助手可以调用的功能。以下是基本结构:
server.tool(
"tool-name",
"Description of what the tool does",
{
// Zod schema for parameters
param1: z.string().describe("Description of parameter"),
param2: z.number().optional().describe("Optional parameter"),
},
async ({ param1, param2 }) => {
// Your tool logic here
return {
content: [
{
type: "text",
text: "Your response",
},
],
};
}
);添加资源
资源为人工智能提供了可访问的动态内容:
server.resource(
"resource://example/{id}",
"Description of the resource",
async (uri) => {
// Extract parameters from URI
const id = uri.path.split("/").pop();
return {
contents: [
{
uri,
mimeType: "text/plain",
text: `Content for ${id}`,
},
],
};
}
);添加提示
提示是可重用的模板:
server.prompt(
"prompt-name",
"Description of the prompt",
{
// Parameters for the prompt
topic: z.string().describe("The topic to discuss"),
},
async ({ topic }) => {
return {
description: `A prompt about ${topic}`,
messages: [
{
role: "user",
content: {
type: "text",
text: `Please help me with ${topic}`,
},
},
],
};
}
);项目结构
├── src/
│ └── index.ts # Main server implementation
├── scripts/ # Installation and utility scripts
├── dist/ # Compiled JavaScript (generated)
├── package.json # Project configuration
├── tsconfig.json # TypeScript configuration
└── README.md # This file发展
- 做出更改至
src/index.ts - 跑
pnpm run build编译 - 用(工具/方法)测试你的服务器
pnpm start - 使用安装脚本来更新您的MCP客户端配置
下一步行动
- 更新
package.json附上您的项目详情 - 自定义服务器名称和工具
src/index.ts - 添加您自己的工具、资源和提示
- 根据需要与外部API或数据库集成
许可证
麻省理工学院(MIT)
