mcp-builder
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseMCP Builder
MCP 服务器构建指南
Principles for building MCP servers.
MCP服务器构建原则。
1. MCP Overview
1. MCP 概述
What is MCP?
什么是MCP?
Model Context Protocol - standard for connecting AI systems with external tools and data sources.
Model Context Protocol - 用于连接AI系统与外部工具及数据源的标准协议。
Core Concepts
核心概念
| Concept | Purpose |
|---|---|
| Tools | Functions AI can call |
| Resources | Data AI can read |
| Prompts | Pre-defined prompt templates |
| 概念 | 用途 |
|---|---|
| Tools(工具) | AI可调用的功能 |
| Resources(资源) | AI可读取的数据 |
| Prompts(提示词) | 预定义的提示词模板 |
2. Server Architecture
2. 服务器架构
Project Structure
项目结构
my-mcp-server/
├── src/
│ └── index.ts # Main entry
├── package.json
└── tsconfig.jsonmy-mcp-server/
├── src/
│ └── index.ts # 主入口文件
├── package.json
└── tsconfig.jsonTransport Types
传输类型
| Type | Use |
|---|---|
| Stdio | Local, CLI-based |
| SSE | Web-based, streaming |
| WebSocket | Real-time, bidirectional |
| 类型 | 用途 |
|---|---|
| Stdio | 本地、基于CLI的传输 |
| SSE | 基于Web的流式传输 |
| WebSocket | 实时双向传输 |
3. Tool Design Principles
3. 工具设计原则
Good Tool Design
优秀工具设计准则
| Principle | Description |
|---|---|
| Clear name | Action-oriented (get_weather, create_user) |
| Single purpose | One thing well |
| Validated input | Schema with types and descriptions |
| Structured output | Predictable response format |
| 原则 | 说明 |
|---|---|
| 清晰的命名 | 面向动作(如get_weather、create_user) |
| 单一职责 | 专注完成一件事 |
| 输入验证 | 带类型和描述的Schema |
| 结构化输出 | 可预测的响应格式 |
Input Schema Design
输入Schema设计
| Field | Required? |
|---|---|
| Type | Yes - object |
| Properties | Define each param |
| Required | List mandatory params |
| Description | Human-readable |
| 字段 | 是否必填? |
|---|---|
| Type(类型) | 是 - 对象类型 |
| Properties(属性) | 定义每个参数 |
| Required(必填项) | 列出必填参数 |
| Description(描述) | 便于人类阅读的说明 |
4. Resource Patterns
4. 资源模式
Resource Types
资源类型
| Type | Use |
|---|---|
| Static | Fixed data (config, docs) |
| Dynamic | Generated on request |
| Template | URI with parameters |
| 类型 | 用途 |
|---|---|
| Static(静态资源) | 固定数据(如配置、文档) |
| Dynamic(动态资源) | 按需生成的数据 |
| Template(模板资源) | 带参数的URI |
URI Patterns
URI 模式
| Pattern | Example |
|---|---|
| Fixed | |
| Parameterized | |
| Collection | |
| 模式 | 示例 |
|---|---|
| 固定URI | |
| 带参数URI | |
| 集合URI | |
5. Error Handling
5. 错误处理
Error Types
错误类型
| Situation | Response |
|---|---|
| Invalid params | Validation error message |
| Not found | Clear "not found" |
| Server error | Generic error, log details |
| 场景 | 响应方式 |
|---|---|
| 参数无效 | 返回验证错误信息 |
| 资源未找到 | 返回清晰的“未找到”提示 |
| 服务器错误 | 返回通用错误,详细信息记录到日志 |
Best Practices
最佳实践
- Return structured errors
- Don't expose internal details
- Log for debugging
- Provide actionable messages
- 返回结构化错误信息
- 不暴露内部细节
- 记录日志用于调试
- 提供可操作的提示信息
6. Multimodal Handling
6. 多模态处理
Supported Types
支持的类型
| Type | Encoding |
|---|---|
| Text | Plain text |
| Images | Base64 + MIME type |
| Files | Base64 + MIME type |
| 类型 | 编码方式 |
|---|---|
| 文本 | 纯文本 |
| 图片 | Base64 + MIME类型 |
| 文件 | Base64 + MIME类型 |
7. Security Principles
7. 安全原则
Input Validation
输入验证
- Validate all tool inputs
- Sanitize user-provided data
- Limit resource access
- 验证所有工具输入
- 清理用户提供的数据
- 限制资源访问权限
API Keys
API密钥管理
- Use environment variables
- Don't log secrets
- Validate permissions
- 使用环境变量存储
- 不记录敏感信息
- 验证权限
8. Configuration
8. 配置
Claude Desktop Config
Claude Desktop 配置
| Field | Purpose |
|---|---|
| command | Executable to run |
| args | Command arguments |
| env | Environment variables |
| 字段 | 用途 |
|---|---|
| command | 要运行的可执行文件 |
| args | 命令参数 |
| env | 环境变量 |
9. Testing
9. 测试
Test Categories
测试分类
| Type | Focus |
|---|---|
| Unit | Tool logic |
| Integration | Full server |
| Contract | Schema validation |
| 类型 | 测试重点 |
|---|---|
| Unit(单元测试) | 工具逻辑 |
| Integration(集成测试) | 完整服务器功能 |
| Contract(契约测试) | Schema验证 |
10. Best Practices Checklist
10. 最佳实践检查清单
- Clear, action-oriented tool names
- Complete input schemas with descriptions
- Structured JSON output
- Error handling for all cases
- Input validation
- Environment-based configuration
- Logging for debugging
Remember: MCP tools should be simple, focused, and well-documented. The AI relies on descriptions to use them correctly.
- 清晰、面向动作的工具命名
- 带完整描述的输入Schema
- 结构化JSON输出
- 覆盖所有场景的错误处理
- 输入验证
- 基于环境的配置
- 用于调试的日志记录
注意: MCP工具应保持简洁、专注且文档完善。AI依赖这些描述来正确使用工具。