AI字段模板
使用FastMCP构建模型上下文协议(MCP)服务器的模板。
特性
- 🚀 基于FastMCP的服务器实现
- 🐳 Docker和Docker Compose支持
- 📦 使用pyproject.toml配置Python包
- 🔧 环境变量配置
- 📁 组织良好的文件夹结构
- 🤖 使用OpenAI/OpenRouter的内置AI文本摘要工具
- 🧪 具有SSE客户端支持的全面测试套件
项目结构
.
├── docs/ # Documentation files
├── tests/ # Test files
├── src/ # Source code
├── main.py # Main entry point
├── pyproject.toml # Python project configuration
├── Dockerfile # Docker configuration
├── docker-compose.yml
├── .env # Environment variables
├── .gitignore # Git ignore rules
└── README.md # This file安装
使用pip
pip install -e .使用Docker
docker-compose up --build用法
在本地运行
# stdio mode (default)
python main.py
# HTTP mode (default port: 8322)
python main.py --transport streamable-http --port 8322使用Docker运行
# Build and run
docker-compose up --build
# Run in detached mode
docker-compose up -d
# Stop the container
docker-compose down服务器将在以下时间可用 http://localhost:8322
配置
创建一个 .env 示例中的文件:
cp env.example .env编辑 .env 文件以配置API密钥:
OpenRouter配置
如果你想使用 OpenRouter 用于访问多个LLM提供商:
可用工具
1.分类_by_llm
使用LLM将每个输入精确地分类到一个最匹配的类别中。
参数:
input(列表):要分类的项目categories(list\[str\]):可能的类别(互斥,至少2个)prompt(str,可选):自定义分类说明args(dict,可选):附加配置(型号、温度等)
例子:
result = await session.call_tool(
"classify_by_llm",
{
"input": ["Apple releases iPhone", "Lakers win game"],
"categories": ["tech", "sports", "politics"]
}
)
# Returns: [{"id": 0, "result": "tech"}, {"id": 1, "result": "sports"}]2.标签_by_llm
将预定义集合中的多个相关标签应用于每个输入。
参数:
input(列表):要标记的项目tags(list\[str\]):可能的标签(非互斥)prompt(str,可选):自定义标记说明args(dict,可选):附加配置(max_tags、min_relevance等)
例子:
result = await session.call_tool(
"tag_by_llm",
{
"input": ["Python REST API", "React app"],
"tags": ["python", "javascript", "backend", "frontend"]
}
)
# Returns: [{"id": 0, "result": ["python", "backend"]}, ...]3.提取物_by_llm
使用LLM从非结构化文本中提取特定字段。
参数:
input(列表):要提取的项目fields(list\[str\],可选):要提取的字段名response_format(dict,可选):用于结构化输出的JSON模式args(dict,可选):附加配置(型号、温度等)
例子:
result = await session.call_tool(
"extract_by_llm",
{
"input": ["Article by Wade on 2025-10-12"],
"fields": ["author", "date"]
}
)
# Returns: [{"id": 0, "result": {"author": "Wade", "date": "2025-10-12"}}]测试
该项目包括对所有MCP工具的全面测试。
运行测试
# Make sure the server is running first
python main.py --transport streamable-http --port 8322
# In another terminal, run the tests
# Run all tests
rye run python tests/test_mcp_client.py --env=local --test=all
# Run specific test
rye run python tests/test_mcp_client.py --env=local --test=classify
rye run python tests/test_mcp_client.py --env=local --test=tag
rye run python tests/test_mcp_client.py --env=local --test=extract
# Test against a remote server
rye run python tests/test_mcp_client.py --env=remote --url=https://your-server.com --test=all测试要求
为了使LLM工具测试工作,您需要:
- 集
OPENAI_API_KEY在你的.env文件(请参阅env.example) - 可选配置
OPENAI_BASE_URL适用于OpenRouter - 确保服务器在端口8322上运行
发展
项目设置
- 克隆此仓库
- 安装依赖项:
pip install -e . - 在中配置环境变量
.env - 开始开发
src/目录
添加工具
在中添加您的MCP工具 main.py 或在中创建单独的模块 src/ 目录:
@mcp.tool()
def your_tool(param: str) -> str:
"""
Description of your tool.
Args:
param: Parameter description
Returns:
Result description
"""
# Your implementation
return resultDocker支持
模板包括:
- Dockerfile:采用安全最佳实践的多阶段构建
- docker-compose.yml:容器编排简单
- 非root用户:出于安全考虑,以无特权用户身份运行
- 健康检查:内置健康监测
需求
- Python>=3.11
- 快速MCP>=2.3.3
- Docker(可选)
许可证
MIT许可证
贡献
欢迎投稿!请随时提交拉取请求。
