创建MCP服务器Pro——脚手架生产就绪的MCP服务器
](https://www.npmjs.com/package/create-mcp-server-pro) ](https://www.npmjs.com/package/create-mcp-server-pro)   
用一个命令搭建一个生产就绪的MCP服务器。TypeScript、Vitest测试、GitHub Actions CI、错误处理模式和最佳实践——所有这些都包括在内。
npx create-mcp-server-pro my-server为什么是这个?
| 功能 | 官方(存档) | FastMCP模板 | 创建mcp服务器专业版 |
|---|---|---|---|
| 已维护 | 2024年11月存档 | 是 | 是 |
| 官方MCP SDK | 是 | 否(FastMCP) | 是 |
| Vitest测试 | 否 | 否 | 是 |
| GitHub操作CI | 否 | 否 | 是 |
| 错误处理模式 | 否 | 否 | 是 |
| 对代理友好的自述文件 | 否 | 否 | 是 |
| 验证样板 | 否 | 否 | 是 |
| 语义释放 | 否 | 否 | 是 |
| ESLint 9+预处理 | 否 | 否 | 是 |
快速开始
交互式
npx create-mcp-server-pro my-server您将被要求:
- 服务器名称 --npm包名称(默认为
mcp-server-) - 描述 --你的服务器做什么
- 认证 -是否需要API令牌
- 作者 --你的名字
非交互式(对代理友好)
npx create-mcp-server-pro my-server \
--name mcp-server-weather \
--description "MCP server for weather data" \
--auth \
--author "Your Name"所有标志都是可选的。提供 --name 和 --description 跳过所有提示。
脚手架搭设后
cd my-server
npm install
npm run build
npm test然后添加您的工具 src/tools.ts 并将其注册到 src/index.ts.
生成什么
my-server/
src/
index.ts # McpServer setup, tool registration, StdioServerTransport
tools.ts # Example tools with proper error handling
tests/
tools.test.ts # Vitest tests with fetch mocking
.github/
workflows/
ci.yml # Node 20 + 22 matrix, lint, typecheck, build, test
release.yml # semantic-release on version tags
FUNDING.yml
package.json # bin field, correct deps, scripts, lint-staged
tsconfig.json # ES2022, NodeNext, strict mode
tsup.config.ts # ESM, node20 target, shebang banner
vitest.config.ts # globals, v8 coverage
eslint.config.js # flat config, typescript-eslint strict
.prettierrc.json
.gitignore
LICENSE # MIT
README.md # Agent-friendly with Quick Start configs如何构建MCP服务器
如果你是MCP(模型上下文协议)的新手,以下是你需要知道的。
什么是MCP?
MCP是Anthropic的开放标准,允许AI助手(Claude、Cursor、GitHub Copilot、Windsurf)连接到外部工具和数据。您的MCP服务器暴露 工具 AI代理可以调用。
AI Assistant MCP Protocol Your Server APIs / Data工具剖析
每个MCP工具都有三个部分:
import { z } from "zod";
server.tool(
"tool_name", // 1. Name (snake_case)
"What this tool does — be specific for the AI", // 2. Description
{
// 3. Input schema (Zod)
query: z.string().describe("Search query"),
limit: z
.number()
.int()
.min(1)
.max(100)
.default(10)
.describe("Max results to return"),
},
async ({ query, limit }) => {
// 4. Handler
try {
const results = await searchAPI(query, limit);
return {
content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
};
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return {
content: [{ type: "text", text: `Error: ${message}` }],
isError: true,
};
}
},
);最佳实践
这些图案来自建筑 4台生产MCP服务器:
命名
- 使用
snake_case工具名称(例如。search_packages,get_user) - 避免空格、点或大小写混合——它们会导致LLM中的标记化问题
描述
- 为人工智能而不是人类写描述
- 具体来说:“按关键字搜索npm包并返回名称、版本和描述”比“搜索包”更好
- 每个Zod领域都应该有
.describe()--这就是人工智能知道传递什么的方式
错误处理
- 用try/catch封装外部调用
- 返回
{ content: [...], isError: true }用于可恢复的错误 - 包括可操作的上下文:“未设置API_TOKEN。在MCP客户端环境中设置它。”而不是“Auth failed”
测试
- 测试工具逻辑与MCP注册分开
- 模拟
fetch随着vi.spyOn(globalThis, "fetch") - 测试成功和错误路径
项目结构
- 保持工具逻辑
src/tools.ts(或src/tools/对于许多工具) - 将MCP注册保存在
src/index.ts - 这种分离使得工具可以在没有MCP传输的情况下进行测试
连接到AI客户端
构建服务器后(npm run build),连接它:
克劳德桌面版 — ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"my-server": {
"command": "npx",
"args": ["-y", "my-server"]
}
}
}光标 — .cursor/mcp.json 在您的项目中:
{
"mcpServers": {
"my-server": {
"command": "npx",
"args": ["-y", "my-server"]
}
}
}VS代码(GitHub副本) — .vscode/mcp.json:
{
"servers": {
"my-server": {
"type": "stdio",
"command": "npx",
"args": ["-y", "my-server"]
}
}
}真实世界的例子
这些MCP服务器是使用此脚手架生成的相同模式构建的:
| 服务器 | 工具 | 功能 |
|---|---|---|
| mcp服务器开发工具 | 17 | Base64、UUID、哈希、JWT解码、cron、时间戳、JSON、正则表达式 |
| 6 | 搜索包、查看详细信息、比较、检查下载 | |
| 8 | 创建、阅读、更新、列出和搜索GitHub Gists | |
| mcp服务器cloudflare | 13 | Workers、KV、R2、DNS和缓存管理 |
作者

 ](https://github.com/ofershap)
______________________________________________________________________
README构建于 README生成器
许可证
麻省理工学院
