名称: MCP SDK Python 启动器 描述: 使用 Python SDK 引导 MCP 服务器,包含传输配置、工具/资源处理器和适当的项目结构。 允许工具: 读取, 写入, 编辑, Bash, Glob, Grep
MCP SDK Python 启动器
使用 Python SDK 引导一个完整的 MCP 服务器,并建立适当的项目结构。
功能
- 生成 Python MCP 服务器项目结构
- 创建工具和资源处理器
- 配置 stdio/SSE 传输层
- 使用 Pydantic 设置正确的类型
- 实现错误处理模式
- 配置 Poetry/pip 项目
使用场景
在以下情况时调用此技能:
- 在 Python 中引导一个新的 MCP 服务器
- 为 AI 消费创建工具和资源
- 设置 MCP 传输层
- 实现 MCP 协议处理器
输入参数
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| 项目名称 | 字符串 | 是 | MCP 服务器项目的名称 |
| 描述 | 字符串 | 是 | 服务器的描述 |
| 工具 | 数组 | 否 | 要实现的工具列表 |
| 资源 | 数组 | 否 | 要暴露的资源列表 |
| 传输类型 | 字符串 | 否 | 传输类型:stdio, sse (默认: stdio) |
工具结构
{
"tools": [
{
"name": "search_files",
"description": "搜索匹配模式的文件",
"parameters": {
"pattern": { "type": "string", "description": "搜索模式" },
"path": { "type": "string", "description": "基础路径", "default": "." }
}
}
]
}
输出结构
<项目名称>/
├── pyproject.toml
├── README.md
├── .gitignore
├── src/
│ └── <包名>/
│ ├── __init__.py
│ ├── __main__.py # 入口点
│ ├── server.py # MCP 服务器设置
│ ├── tools/
│ │ ├── __init__.py
│ │ └── search.py # 工具实现
│ ├── resources/
│ │ ├── __init__.py
│ │ └── files.py # 资源提供者
│ └── types/
│ ├── __init__.py
│ └── schemas.py # Pydantic 模型
└── tests/
└── test_tools.py
生成的代码模式
服务器设置 (src/<包名>/server.py)
import asyncio
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, Resource
from .tools import register_tools
from .resources import register_resources
# 创建服务器实例
server = Server("<项目名称>")
# 注册处理器
register_tools(server)
register_resources(server)
async def main():
"""运行 MCP 服务器。"""
async with stdio_server() as (read_stream, write_stream):
await server.run(
read_stream,
write_stream,
server.create_initialization_options()
)
def run():
"""服务器的入口点。"""
asyncio.run(main())
工具实现 (src/<包名>/tools/search.py)
from typing import Any
from pydantic import BaseModel, Field
from mcp.server import Server
from mcp.types import Tool, TextContent
class SearchFilesInput(BaseModel):
"""search_files 工具的输入模式。"""
pattern: str = Field(description="搜索模式 (glob)")
path: str = Field(default=".", description="搜索的基础路径")
def register(server: Server) -> None:
"""注册 search_files 工具。"""
@server.list_tools()
async def list_tools() -> list[Tool]:
return [
Tool(
name="search_files",
description="搜索匹配模式的文件",
inputSchema=SearchFilesInput.model_json_schema()
)
]
@server.call_tool()
async def call_tool(name: str, arguments: dict[str, Any]) -> list[TextContent]:
if name != "search_files":
raise ValueError(f"未知工具: {name}")
# 验证输入
input_data = SearchFilesInput(**arguments)
# 执行搜索
from pathlib import Path
matches = list(Path(input_data.path).glob(input_data.pattern))
return [
TextContent(
type="text",
text="
".join(str(m) for m in matches)
)
]
资源提供者 (src/<包名>/resources/files.py)
from mcp.server import Server
from mcp.types import Resource, TextResourceContents
def register(server: Server) -> None:
"""注册文件资源。"""
@server.list_resources()
async def list_resources() -> list[Resource]:
return [
Resource(
uri="file:///config",
name="配置",
description="服务器配置",
mimeType="application/json"
)
]
@server.read_resource()
async def read_resource(uri: str) -> TextResourceContents:
if uri == "file:///config":
return TextResourceContents(
uri=uri,
mimeType="application/json",
text='{"version": "1.0.0"}'
)
raise ValueError(f"未知资源: {uri}")
依赖项
[tool.poetry.dependencies]
python = ">=3.10"
mcp = "^1.0.0"
pydantic = "^2.0.0"
[tool.poetry.group.dev.dependencies]
pytest = "^8.0.0"
pytest-asyncio = "^0.23.0"
工作流程
- 创建项目结构 - 设置 Python 包
- 生成服务器 - 带传输的 MCP 服务器
- 创建工具 - 带模式的工具处理器
- 创建资源 - 资源提供者
- 添加类型 - Pydantic 模型
- 设置测试 - 异步测试夹具
目标流程
- mcp-服务器-引导
- mcp-工具-实现
- mcp-资源-提供者