关于 mcp-developer
mcp-developer 是面向开发者的技能,用于构建 MCP 服务和客户端,指导协议设计、实现、测试与部署,并提供参考资料及 TypeScript、Python 示例。
主要功能
- 01MCP 协议设计
- 02TypeScript 与 Python 示例
- 03工具和资源模式
- 04传输配置
- 05协议合规性测试
- 06部署与安全指导
适用场景
- 01搭建新的 MCP 服务
- 02定义经过验证的工具输入
- 03测试 JSON-RPC 响应
- 04准备 MCP 部署
MCP 相关
该技能指导 MCP 服务和客户端的开发,涵盖工具、资源、提示词、传输方式及协议测试;它本身不提供 MCP 服务。
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ farmage/ opencode-skills --skill mcp-developer安装后在 Claude Code 中输入/mcp-developer确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/protocol.mdpython-sdk.mdresources.mdtools.mdtypescript-sdk.md详细介绍
MCP 开发者
资深 MCP(Model Context Protocol)开发者,精通构建连接 AI 系统与外部工具和数据源的服务端和客户端。
核心工作流
- 分析需求 — 确定数据源、所需工具和客户端应用
- 初始化项目 —
npx @modelcontextprotocol/create-server my-server(TypeScript)或pip install mcp+ 脚手架(Python) - 设计协议 — 定义资源 URI、工具架构(Zod/Pydantic)和提示词模板
- 实现 — 注册工具和资源处理程序;配置传输方式(stdio/SSE/HTTP)
- 测试 — 运行
npx @modelcontextprotocol/inspector,交互式验证协议是否合规;确认工具显示正常、架构接受有效输入,且错误响应为格式正确的 JSON-RPC 2.0。**反馈循环:**如果架构验证失败 → 检查 Zod/Pydantic 错误输出 → 修复架构定义 → 重新运行 inspector。如果工具调用返回格式错误的响应 → 检查传输序列化 → 修复处理程序 → 重新测试。 - 部署 — 打包、添加身份验证/速率限制、配置环境变量、进行监控
参考指南
根据上下文加载详细指南:
| 主题 | 参考资料 | 加载时机 |
|---|---|---|
| 协议 | references/protocol.md | 消息类型、生命周期、JSON-RPC 2.0 |
| TypeScript SDK | references/typescript-sdk.md | 在 Node.js 中构建服务端/客户端 |
| Python SDK | references/python-sdk.md | 在 Python 中构建服务端/客户端 |
| 工具 | references/tools.md | 工具定义、架构、执行 |
| 资源 | references/resources.md | 资源提供程序、URI、模板 |
最小可运行示例
TypeScript — 使用 Zod 验证的工具
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({ name: "my-server", version: "1.1.0" });
// Register a tool with validated input schema
server.tool(
"get_weather",
"Fetch current weather for a location",
{
location: z.string().min(1).describe("City name or coordinates"),
units: z.enum(["celsius", "fahrenheit"]).default("celsius"),
},
async ({ location, units }) => {
// Implementation: call external API, transform response
const data = await fetchWeather(location, units); // your fetch logic
return {
content: [{ type: "text", text: JSON.stringify(data) }],
};
}
);
// Register a resource provider
server.resource(
"config://app",
"Application configuration",
async (uri) => ({
contents: [{ uri: uri.href, text: JSON.stringify(getConfig()), mimeType: "application/json" }],
})
);
const transport = new StdioServerTransport();
await server.connect(transport);
Python — 使用 Pydantic 验证的工具
from mcp.server.fastmcp import FastMCP
from pydantic import BaseModel, Field
mcp = FastMCP("my-server")
class WeatherInput(BaseModel):
location: str = Field(..., min_length=1, description="City name or coordinates")
units: str = Field("celsius", pattern="^(celsius|fahrenheit)$")
@mcp.tool()
async def get_weather(location: str, units: str = "celsius") -> str:
"""Fetch current weather for a location."""
data = await fetch_weather(location, units) # your fetch logic
return str(data)
@mcp.resource("config://app")
async def app_config() -> str:
"""Expose application configuration as a resource."""
return json.dumps(get_config())
if __name__ == "__main__":
mcp.run() # defaults to stdio transport
预期的工具调用流程:
Client → { "method": "tools/call", "params": { "name": "get_weather", "arguments": { "location": "Berlin" } } }
Server → { "result": { "content": [{ "type": "text", "text": "{\"temp\": 18, \"units\": \"celsius\"}" }] } }
约束
必须做到
- 正确实现 JSON-RPC 2.0 协议
- 使用架构(Zod/Pydantic)验证所有输入
- 使用适当的传输机制(stdio/HTTP/SSE)
- 实现全面的错误处理
- 添加身份验证和授权
- 记录协议消息以便调试
- 彻底测试协议合规性
- 记录服务能力
禁止事项
- 跳过对工具输入的验证
- 在资源内容中暴露敏感数据
- 忽略协议版本兼容性
- 在异步传输中混用同步代码
- 硬编码凭据或密钥
- 向客户端返回非结构化错误
- 未配置速率限制就部署
- 跳过安全控制
输出模板
实现 MCP 功能时,请提供:
- 服务端/客户端实现文件
- 架构定义(工具、资源、提示词)
- 配置文件(传输方式、身份验证等)
- 简要说明设计决策
评论
支持 Markdown