MCP生成器
用于构建模型上下文协议服务器、定义工具和编写 Claude Skills 可以信赖的评估套件的代理技能手册。
来源:内容改编自人类/技能(麻省理工学院)。
概述
创建 MCP(模型上下文协议)服务器,使 LLM 能够通过精心设计的工具与外部服务交互。 MCP 服务器的质量是通过它使 LLM 完成实际任务的能力来衡量的。
过程
高级工作流程
创建高质量的 MCP 服务器涉及四个主要阶段:
第一阶段:深度研究与规划
1.1 了解现代 MCP 设计
API 覆盖率与工作流程工具: 通过专门的工作流程工具平衡全面的 API 端点覆盖范围。工作流工具可以更方便地执行特定任务,而全面的覆盖范围使代理可以灵活地组合操作。性能因客户而异,一些客户可以从结合基本工具的代码执行中受益,而另一些客户则可以通过更高级别的工作流程更好地工作。当不确定时,优先考虑全面的 API 覆盖范围。
工具命名和可发现性:
清晰、描述性的工具名称可帮助代理快速找到合适的工具。使用一致的前缀(例如,github_create_issue、github_list_repos)和面向操作的命名。
上下文管理: 代理受益于简洁的工具描述和过滤/分页结果的能力。设计可返回重点相关数据的工具。一些客户端支持代码执行,这可以帮助代理有效地过滤和处理数据。
可操作的错误消息: 错误消息应引导客服人员找到包含具体建议和后续步骤的解决方案。
1.2 研究MCP协议文档
浏览 MCP 规范:
从站点地图开始查找相关页面:https://modelcontextprotocol.io/sitemap.xml
然后获取带有.md后缀的 Markdown 格式的特定页面(例如https://modelcontextprotocol.io/specification/draft.md)。
要查看的关键页面:
- 规范概述和架构
- 传输机制(流式 HTTP、stdio)
- 工具、资源和提示定义
1.3 研究框架文档
推荐堆栈:
- 语言:TypeScript(高质量的 SDK 支持以及在许多执行环境(例如 MCPB)中的良好兼容性。此外,AI 模型擅长生成 TypeScript 代码,受益于其广泛的用途、静态类型和良好的 linting 工具)
- 传输:用于远程服务器的流式 HTTP,使用无状态 JSON(更容易扩展和维护,而不是有状态会话和流式响应)。用于本地服务器的 stdio。
加载框架文档:
- MCP 最佳实践:查看最佳实践 - 核心指南
对于 TypeScript(推荐):
- TypeScript SDK:使用WebFetch加载
https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/main/README.md - TypeScript 指南 - TypeScript 模式和示例
对于Python:
- Python SDK:使用WebFetch加载
https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md - Python 指南 - Python 模式和示例
1.4 规划您的实施
了解API: 查看服务的 API 文档以确定关键端点、身份验证要求和数据模型。根据需要使用网络搜索和 WebFetch。
工具选择: 优先考虑全面的 API 覆盖范围。列出要实现的端点,从最常见的操作开始。
第二阶段:实施
2.1 设置项目结构
请参阅项目设置的特定语言指南:
- TypeScript 指南 - 项目结构、package.json、tsconfig.json
- Python 指南 - 模块组织、依赖关系
2.2 实施核心基础设施
创建共享实用程序:
- 具有身份验证功能的 API 客户端
- 错误处理助手
- 响应格式(JSON/Markdown)
- 分页支持
2.3 实施工具
对于每个工具:
输入架构:
- 使用 Zod (TypeScript) 或 Pydantic (Python)
- 包括约束和清晰的描述
- 在字段描述中添加示例
输出架构:
- 尽可能为结构化数据定义
outputSchema - 在工具响应中使用
structuredContent(TypeScript SDK 功能) - 帮助客户理解和处理工具输出
工具说明:
- 功能简明总结
- 参数说明
- 返回类型架构
执行:
- 异步/等待 I/O 操作
- 通过可操作的消息进行正确的错误处理
- 在适用的情况下支持分页
- 使用现代 SDK 时返回文本内容和结构化数据
注释:
readOnlyHint:真/假destructiveHint:真/假idempotentHint:真/假openWorldHint:真/假
第三阶段:审查和测试
3.1 代码质量
评论:
- 没有重复代码(DRY原则)
- 一致的错误处理
- 全类型覆盖
- 清晰的工具说明
3.2 构建和测试
打字稿:
- 运行
npm run build验证编译 - 使用 MCP Inspector 进行测试:
npx @modelcontextprotocol/inspector
Python:
- 验证语法:
python -m py_compile your_server.py - 使用 MCP Inspector 进行测试
请参阅特定语言的指南,了解详细的测试方法和质量检查表。
第 4 阶段:创建评估
实施 MCP 服务器后,创建全面的评估以测试其有效性。
加载评估指南以获取完整的评估指南。
4.1 了解评估目的
使用评估来测试法学硕士是否可以有效地使用您的 MCP 服务器来回答现实、复杂的问题。
4.2 创建 10 个评估问题
要进行有效的评估,请遵循评估指南中概述的流程:
- 工具检查:列出可用工具并了解其功能
- 内容探索:使用只读操作来探索可用数据
- 问题生成:创建 10 个复杂、现实的问题
- 答案验证:自己解决每个问题以验证答案
4.3 评估要求
确保每个问题是:
- 独立:不依赖于其他问题
- 只读:仅需要非破坏性操作
- 复杂:需要多次工具调用和深入探索
- 现实:基于人类会关心的真实用例
- 可验证:单一、清晰的答案,可以通过字符串比较来验证
- 稳定:答案不会随时间而改变
4.4 输出格式
创建具有以下结构的 XML 文件:
<evaluation>
<qa_pair>
<question>Find discussions about AI model launches with animal codenames. One model needed a specific safety designation that uses the format ASL-X. What number X was being determined for the model named after a spotted wild cat?</question>
<answer>3</answer>
</qa_pair>
<!-- More qa_pairs... -->
</evaluation>参考文件
文档库
开发过程中根据需要加载这些资源:
核心 MCP 文档(首先加载)
- MCP协议:从
https://modelcontextprotocol.io/sitemap.xml的站点地图开始,然后获取带有.md后缀的特定页面 - MCP 最佳实践 - 通用 MCP 指南包括:
- 服务器和工具命名约定
- 响应格式指南(JSON 与 Markdown)
- 分页最佳实践
- 传输选择(流式 HTTP 与 stdio)
- 安全和错误处理标准
SDK 文档(在第 1/2 阶段加载)
- Python SDK:从
https://raw.githubusercontent.com/modelcontextprotocol/python-sdk/main/README.md获取 - TypeScript SDK:从
https://raw.githubusercontent.com/modelcontextprotocol/typescript-sdk/main/README.md获取
特定于语言的实施指南(在第 2 阶段加载)
-
Python 实施指南 - 完整的 Python/FastMCP 指南:
- 服务器初始化模式
- Pydantic 模型示例
- 使用
@mcp.tool进行工具注册 - 完整的工作示例
- 质量检查表
-
TypeScript 实施指南 - 完整的 TypeScript 指南:
- 项目结构
- Zod 模式模式
- 使用
server.registerTool进行工具注册 - 完整的工作示例
- 质量检查表
评估指南(第 4 阶段的负载)
- 评估指南 - 完整的评估创建指南:
- 问题创建指南
- 答案验证策略
- XML 格式规范
- 示例问题和答案
- 使用提供的脚本运行评估
资源文件
许可证.txt
二进制资源
参考/评估.md
二进制资源
参考/mcp_best_practices.md
二进制资源
参考/node_mcp_server.md
二进制资源
参考/python_mcp_server.md
二进制资源
脚本/连接.py
"""Lightweight connection handling for MCP servers."""
from abc import ABC, abstractmethod
from contextlib import AsyncExitStack
from typing import Any
from mcp import ClientSession, StdioServerParameters
from mcp.client.sse import sse_client
from mcp.client.stdio import stdio_client
from mcp.client.streamable_http import streamablehttp_client
class MCPConnection(ABC):
"""Base class for MCP server connections."""
def __init__(self):
self.session = None
self._stack = None
@abstractmethod
def _create_context(self):
"""Create the connection context based on connection type."""
async def __aenter__(self):
"""Initialize MCP server connection."""
self._stack = AsyncExitStack()
await self._stack.__aenter__()
try:
ctx = self._create_context()
result = await self._stack.enter_async_context(ctx)
if len(result) == 2:
read, write = result
elif len(result) == 3:
read, write, _ = result
else:
raise ValueError(f"Unexpected context result: {result}")
session_ctx = ClientSession(read, write)
self.session = await self._stack.enter_async_context(session_ctx)
await self.session.initialize()
return self
except BaseException:
await self._stack.__aexit__(None, None, None)
raise
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Clean up MCP server connection resources."""
if self._stack:
await self._stack.__aexit__(exc_type, exc_val, exc_tb)
self.session = None
self._stack = None
async def list_tools(self) -> list[dict[str, Any]]:
"""Retrieve available tools from the MCP server."""
response = await self.session.list_tools()
return [
{
"name": tool.name,
"description": tool.description,
"input_schema": tool.inputSchema,
}
for tool in response.tools
]
async def call_tool(self, tool_name: str, arguments: dict[str, Any]) -> Any:
"""Call a tool on the MCP server with provided arguments."""
result = await self.session.call_tool(tool_name, arguments=arguments)
return result.content
class MCPConnectionStdio(MCPConnection):
"""MCP connection using standard input/output."""
def __init__(self, command: str, args: list[str] = None, env: dict[str, str] = None):
super().__init__()
self.command = command
self.args = args or []
self.env = env
def _create_context(self):
return stdio_client(
StdioServerParameters(command=self.command, args=self.args, env=self.env)
)
class MCPConnectionSSE(MCPConnection):
"""MCP connection using Server-Sent Events."""
def __init__(self, url: str, headers: dict[str, str] = None):
super().__init__()
self.url = url
self.headers = headers or {}
def _create_context(self):
return sse_client(url=self.url, headers=self.headers)
class MCPConnectionHTTP(MCPConnection):
"""MCP connection using Streamable HTTP."""
def __init__(self, url: str, headers: dict[str, str] = None):
super().__init__()
self.url = url
self.headers = headers or {}
def _create_context(self):
return streamablehttp_client(url=self.url, headers=self.headers)
def create_connection(
transport: str,
command: str = None,
args: list[str] = None,
env: dict[str, str] = None,
url: str = None,
headers: dict[str, str] = None,
) -> MCPConnection:
"""Factory function to create the appropriate MCP connection.
Args:
transport: Connection type ("stdio", "sse", or "http")
command: Command to run (stdio only)
args: Command arguments (stdio only)
env: Environment variables (stdio only)
url: Server URL (sse and http only)
headers: HTTP headers (sse and http only)
Returns:
MCPConnection instance
"""
transport = transport.lower()
if transport == "stdio":
if not command:
raise ValueError("Command is required for stdio transport")
return MCPConnectionStdio(command=command, args=args, env=env)
elif transport == "sse":
if not url:
raise ValueError("URL is required for sse transport")
return MCPConnectionSSE(url=url, headers=headers)
elif transport in ["http", "streamable_http", "streamable-http"]:
if not url:
raise ValueError("URL is required for http transport")
return MCPConnectionHTTP(url=url, headers=headers)
else:
raise ValueError(f"Unsupported transport type: {transport}. Use 'stdio', 'sse', or 'http'")脚本/评估.py
二进制资源
脚本/example_evaluation.xml
二进制资源
脚本/需求.txt
anthropic>=0.39.0
mcp>=1.1.0
claudeskills文档