MCP服务器生成器

一个强大的命令行工具,用于从模板或OpenAPI规范生成生产就绪的MCP(模型上下文协议)服务器,采用测试优先的方法设计,便于部署和维护。

  • by vineetkothari23
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 vineetkothari23/mcp-generator

使用说明

MCP CLI-模型上下文协议服务器生成器

一个强大的命令行工具,用于从模板或OpenAPI规范生成生产就绪的MCP(模型上下文协议)服务器。采用测试优先的方法构建,旨在易于部署和维护。

🚀 特性

  • 快速项目初始化:在几秒钟内生成完整的MCP服务器项目
  • OpenAPI集成:自动将OpenAPI规范转换为MCP服务器
  • 测试优先开发:自动生成全面的测试套件
  • Docker就绪:包括生产就绪的集装箱化
  • 配置驱动:通过YAML配置轻松定制
  • 质量保证:内置验证和linting
  • 模块化架构:干净、可维护的代码结构

📦 安装

来自PyPI(推荐)

pip install mcp-cli

来源

git clone https://github.com/your-org/mcp-cli.git
cd mcp-cli
pip install -e .

开发安装

git clone https://github.com/your-org/mcp-cli.git
cd mcp-cli
pip install -e ".[dev]"

🎯 快速开始

初始化标准MCP服务器

# Create a new MCP server project
mcp-cli init --name "weather-api" --author "Your Name"

# Navigate to the project
cd weather-api

# Install dependencies
pip install -r requirements-dev.txt

# Run tests
python -m pytest tests/

# Start the server
python scripts/run_server.py

根据OpenAPI规范生成

# From URL
mcp-cli from-openapi --spec https://api.example.com/openapi.json

# From local file
mcp-cli from-openapi --spec ./api-spec.yaml --name "my-api-server"

# With examples and documentation
mcp-cli from-openapi --spec ./api-spec.yaml --include-examples --max-tools 25

验证项目

# Validate OpenAPI specification
mcp-cli analyze --spec ./api-spec.yaml --format yaml

# Validate existing project
mcp-cli validate --project-dir ./my-mcp-server

📚 命令

mcp-cli init

使用标准结构初始化新的MCP服务器项目。

选项:

  • --name, -n:项目名称(必填)
  • --description, -d:项目描述
  • --author, -a:作者姓名
  • --output-dir, -o:输出目录(默认:当前目录)
  • --python-version:Python版本(默认:3.11)
  • --no-docker:跳过Docker设置
  • --no-ci:跳过CI/CD设置
  • --test-framework:测试框架(默认:pytest)

例子:

mcp-cli init \
  --name "social-media-api" \
  --description "MCP server for social media management" \
  --author "Jane Developer" \
  --python-version "3.12"

mcp-cli from-openapi

根据OpenAPI规范生成MCP服务器。

选项:

  • --spec, -s:OpenAPI规范文件路径或URL(必填)
  • --name, -n:项目名称(如果未提供,则自动生成)
  • --output-dir, -o:输出目录
  • --author, -a:作者姓名
  • --validate-only:仅验证规格
  • --include-examples:生成使用示例
  • --max-tools:要生成的最大工具数(默认值:50)

例子:

mcp-cli from-openapi \
  --spec https://petstore3.swagger.io/api/v3/openapi.json \
  --name "petstore-mcp" \
  --include-examples \
  --max-tools 30

mcp-cli analyze

分析OpenAPI规范并显示生成计划。

选项:

  • --spec, -s:OpenAPI规范文件路径或URL(必填)
  • --output, -o:输出文件(默认:stdout)
  • --format:输出格式-json或yaml(默认:json)

例子:

mcp-cli analyze --spec ./api-spec.yaml --format yaml --output analysis.yaml

mcp-cli validate

验证现有的MCP服务器项目结构。

选项:

  • --project-dir, -p:要验证的项目目录(默认:当前目录)

例子:

mcp-cli validate --project-dir ./my-mcp-server

🏗️ 项目结构

生成的项目遵循标准化结构:

my-mcp-server/
├── src/
│   └── mcp_my_service/
│       ├── __init__.py          # Package initialization
│       ├── server.py            # Main MCP server implementation
│       ├── client.py            # API client wrapper
│       ├── models.py            # Pydantic data models
│       ├── tools.py             # MCP tool definitions
│       ├── resources.py         # MCP resource definitions
│       └── config.py            # Configuration management
├── tests/
│   ├── unit/                    # Unit tests
│   ├── integration/             # Integration tests
│   ├── fixtures/                # Test data and fixtures
│   └── conftest.py              # Pytest configuration
├── docker/
│   ├── Dockerfile               # Docker container definition
│   └── docker-compose.yml       # Docker Compose configuration
├── config/
│   ├── server_config.yaml       # Server configuration
│   ├── api_config.yaml          # API-specific configuration
│   └── logging.yaml             # Logging configuration
├── scripts/
│   ├── run_server.py            # Server startup script
│   └── deploy.sh                # Deployment script
├── docs/
│   ├── API_REFERENCE.md         # Generated API documentation
│   ├── USAGE_EXAMPLES.md        # Usage examples
│   └── DEPLOYMENT.md            # Deployment instructions
├── requirements.txt             # Production dependencies
├── requirements-dev.txt         # Development dependencies
├── pyproject.toml               # Project configuration
├── .env.example                 # Environment variables template
├── .dockerignore                # Docker ignore file
└── README.md                    # Project documentation

⚙️ 配置

环境变量

# API Configuration
MCP_API_BASE_URL=https://api.example.com
MCP_API_KEY=your-api-key
MCP_TIMEOUT=30

# Server Configuration
MCP_SERVER_NAME=my-mcp-server
MCP_MAX_TOOLS=50
MCP_ENABLE_RESOURCES=true

# Development
MCP_LOG_LEVEL=DEBUG
MCP_ENABLE_CORS=true

配置文件

config/server_config.yaml

server:
  name: "my-mcp-server"
  version: "1.0.0"
  description: "My MCP server description"

api:
  base_url: "https://api.example.com"
  timeout: 30
  retry_attempts: 3

mcp:
  max_tools: 50
  enable_resources: true
  enable_logging: true

🧪 测试

所有生成的项目都包括全面的测试套件:

# Run all tests
python -m pytest

# Run with coverage
python -m pytest --cov=src --cov-report=html

# Run specific test types
python -m pytest tests/unit/          # Unit tests only
python -m pytest tests/integration/   # Integration tests only

# Run with verbose output
python -m pytest -v --tb=short

🐳 Docker部署

生成的项目包括Docker支持:

# Build the container
docker build -f docker/Dockerfile -t my-mcp-server .

# Run with Docker Compose
docker-compose -f docker/docker-compose.yml up

# Run standalone container
docker run -p 8080:8080 --env-file .env my-mcp-server

🔧 发展

设置开发环境

# Clone the repository
git clone https://github.com/your-org/mcp-cli.git
cd mcp-cli

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install in development mode
pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

# Run tests
python -m pytest tests/

添加新模板

  1. 在中创建模板文件 mcp_cli/templates/
  2. 在中更新相应的生成器 mcp_cli/generators.py
  3. 为新模板添加测试
  4. 更新文档

贡献

  1. 分叉存储库
  2. 创建要素分支(git checkout -b feature/amazing-feature)
  3. 通过测试进行更改
  4. 运行测试套件(python -m pytest)
  5. 提交您的更改(git commit -m 'Add amazing feature')
  6. 推到分支(git push origin feature/amazing-feature)
  7. 打开拉取请求

📖 例子

基本天气API MCP服务器

mcp-cli init --name "weather-mcp" --description "Weather data MCP server"

来自OpenAPI的电子商务API

mcp-cli from-openapi \
  --spec https://api.shop.com/openapi.json \
  --name "shop-mcp" \
  --include-examples

社交媒体管理服务器

mcp-cli init \
  --name "social-media-mcp" \
  --description "Social media management MCP server" \
  --author "Social Team"

🐛 故障排除

常见问题

“找不到命令:mcp-cli”

# Ensure the package is installed
pip install mcp-cli

# Check if it's in PATH
which mcp-cli

# Try running with python -m
python -m mcp_cli.cli --help

“OpenAPI验证失败”

# Validate your spec first
mcp-cli analyze --spec your-spec.yaml

# Check for common issues:
# - Missing required fields
# - Invalid URLs
# - Malformed YAML/JSON

“生成后测试失败”

# Install development dependencies
pip install -r requirements-dev.txt

# Check Python version compatibility
python --version  # Should be 3.11+

# Run tests with verbose output
python -m pytest -v

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🤝 支持

🗺️ Roadmap

  • []支持其他API规范(GraphQL、gRPC)
  • []交互式项目设置向导
  • []自定义生成器插件系统
  • []与流行的IDE集成
  • []高级监控和可观察性功能
  • []支持多种MCP协议版本

🙏 致谢


内置于❤️ 对于MCP社区