Hello World MCP服务器
一个全面的示例,演示如何将现有服务转换为模型上下文协议(MCP)服务器。
什么是MCP?
模型上下文协议(MCP)是像Claude这样的人工智能助手连接到外部数据源和服务的标准化方式。将其视为一座桥梁,使人工智能模型能够以安全和可控的方式与您的应用程序、数据库、API和业务逻辑进行交互。
如何将您的服务转换为MCP服务器
该项目展示了 5种常见模式 将现有服务转换为MCP工具:
1. 数据服务→ MCP工具
将简单的数据检索服务(如用户管理)转换为可查询的工具。
// Your existing service method
getUserById(id: string): User | null
// Becomes an MCP tool
server.tool("get-user", "Retrieve user information by ID", ...)2. API服务→ MCP工具
公开外部API调用作为MCP工具供人工智能助手使用。
// Your existing API service
getWeatherData(location: string): WeatherData
// Becomes an MCP tool
server.tool("get-weather", "Get current weather for a location", ...)3. 数据库服务→ MCP工具
将数据库操作转换为可搜索和可查询的工具。
// Your existing database service
searchUsers(query: string): User[]
// Becomes an MCP tool
server.tool("search-users", "Search users by name or email", ...)4. 文件系统服务→ MCP工具
将文件操作转换为AI可以用来创建和管理文件的工具。
// Your existing file service
createFile(filename: string, content: string): boolean
// Becomes an MCP tool
server.tool("create-file", "Create a new file with content", ...)5. 业务逻辑服务→ MCP工具
将复杂的业务规则和计算转换为AI可访问的工具。
// Your existing business logic
calculateDiscount(amount: number, customerType: string): DiscountResult
// Becomes an MCP tool
server.tool("calculate-discount", "Calculate discount based on business rules", ...)MCP服务器的主要优势
- 🔒 安全访问:对您的数据和服务的受控访问
- ⚡ 实时数据:AI可以访问实时、最新的信息
- 🛠️ 自定义工具:将您的业务逻辑作为AI可以使用的工具公开
- 📈 可扩展的:易于在不同环境中部署和管理
- 🔄 标准化:AI模型交互的一致界面
安装和设置
- 克隆和安装依赖关系:
npm install- 构建服务器:
npm run build- 测试服务器:
npm start可用工具
此示例服务器公开了5个演示不同模式的工具:
| 工具 | 说明 | 参数 |
|---|---|---|
get-user | 按ID检索用户信息 | userId: string |
get-weather | 获取某个地点的当前天气 | location: string |
search-users | 按姓名或电子邮件搜索用户 | query: string, limit?: number |
create-file | 创建一个包含内容的新文件 | filename: string, content: string |
calculate-discount | 根据业务规则计算折扣 | amount: number, customerType: enum, itemCount: number |
与Claude Desktop一起使用
- 配置Claude桌面 通过编辑配置文件:
# macOS/Linux
~/.config/Claude/claude_desktop_config.json
# Windows
%APPDATA%\Claude\claude_desktop_config.json- 添加您的服务器配置:
{
"mcpServers": {
"hello-world-mcp": {
"command": "node",
"args": ["/absolute/path/to/hello_world/build/index.js"]
}
}
}- 重新启动克劳德桌面 并查找MCP工具图标。
开发脚本
npm run build-构建TypeScript项目npm run start-构建并启动服务器npm run dev-开发观看模式npm test-运行测试(占位符)
项目结构
hello_world/
├── src/
│ └── index.ts # Main MCP server implementation
├── build/ # Compiled JavaScript output
├── .github/
│ └── copilot-instructions.md # Copilot customization
├── .vscode/
│ └── mcp.json # VS Code MCP configuration
├── package.json # Project configuration
├── tsconfig.json # TypeScript configuration
└── README.md # This file调试您的MCP服务器
- VS代码集成:使用
.vscode/mcp.json配置以直接在VS代码中调试服务器。
- 检查克劳德日志:
# macOS
tail -f ~/Library/Logs/Claude/mcp*.log
# Linux
tail -f ~/.local/share/Claude/logs/mcp*.log- 手动测试:直接运行服务器并测试工具响应:
npm start转换您自己的服务
要将现有服务转换为MCP工具,请遵循以下模式:
- 确定服务方法:列出您的服务公开的所有函数/方法
- 定义工具架构:使用Zod创建输入验证模式
- 机具逻辑:将您的服务调用封装在MCP工具处理程序中
- 处理错误:实施适当的错误处理和用户友好的消息
- 测试集成:验证工具是否适用于Claude Desktop或其他MCP客户端
示例模式:
// Your existing service
class MyService {
async getData(id: string): Promise {
// Your existing logic
}
}
// Convert to MCP tool
server.tool(
"get-data",
"Retrieve data by ID",
{
id: z.string().describe("The unique identifier"),
},
async ({ id }) => {
try {
const data = await myService.getData(id);
return {
content: [
{
type: "text",
text: JSON.stringify(data, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Error retrieving data: ${error.message}`,
},
],
};
}
}
);后续步骤
- 探索代码:查看
src/index.ts查看实现模式 - 添加您的工具:用实际的维修方法替换示例工具
- 部署:考虑生产使用的部署选项
- 规模:添加更复杂的错误处理、日志记录和监控
资源
许可证
ISC许可证-欢迎将此作为您自己的MCP服务器的起点!
