动态工具加载MCP服务器

multi-tenant-github-mcp是一个基于FastMCP的模型上下文协议服务器,支持动态工具加载和配置管理,适用于开发工具集成和自动化测试场景。

  • by christian-posta
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 christian-posta/multi-tenant-github-mcp

使用说明

多租户github mcp

多租户github mcp是一个使用FastMCP构建的模型上下文协议(mcp)服务器,具有动态工具加载功能。

特性

  • 动态工具加载:工具会自动从以下位置发现和加载 src/tools/
  • 每个文件一个工具:每个工具都是一个具有与文件名匹配的函数的单个文件
  • FastMCP集成:利用FastMCP实现强大的MCP协议处理
  • 配置管理:通过工具进行特定配置 kmcp.yaml
  • 快速失败:如果任何工具加载失败,服务器将不会启动
  • 自动生成的测试:工具验证的自动测试生成

项目结构

src/
├── tools/              # Tool implementations (one file per tool)
│   ├── echo.py         # Example echo tool
│   └── __init__.py     # Auto-generated tool registry
├── core/               # Dynamic loading framework
│   ├── server.py       # Dynamic MCP server
│   └── utils.py        # Shared utilities
└── main.py             # Entry point
kmcp.yaml               # Configuration file
tests/                  # Generated tests

快速开始

选项1:本地开发(使用Python/uv)

  1. 再进行:

    uv sync
    
  2. 运行服务器:

    # Stdio mode (default MCP transport)
    uv run python src/main.py
    
    # HTTP mode with WebSocket MCP endpoint
    uv run python src/main.py --http
    
    # HTTP mode with custom host/port
    uv run python src/main.py --http --host 0.0.0.0 --port 8080
    
  3. 使用uv脚本:

    # Development mode (HTTP on port 3000)
    uv run dev
    
    # HTTP mode
    uv run dev-http
    
    # Stdio mode
    uv run start
    
  4. 添加新工具:

    # Create a new tool (no tool types needed!)
    kmcp add-tool weather
    
    # The tool file will be created at src/tools/weather.py
    # Edit it to implement your tool logic
    

选项2:仅Docker开发(不需要本地Python/uv)

  1. 构建Docker镜像:

    kmcp build --verbose
    
  2. 在容器中运行:

    docker run -i multi-tenant-github-mcp:latest
    
  3. 部署到Kubernetes:

    kmcp deploy mcp --apply
    
  4. 添加新工具:

    # Create a new tool
    kmcp add-tool weather
    
    # Edit the tool file, then rebuild
    kmcp build
    

HTTP传输模式

出于开发和集成目的,服务器支持在HTTP模式下运行。

以HTTP模式启动

# Command line flag
python src/main.py --http

# Environment variable
MCP_TRANSPORT_MODE=http python src/main.py

# Custom host and port
python src/main.py --http --host localhost --port 8080

创建工具

基本工具结构

每个工具都是一个Python文件 src/tools/ 包含一个装饰有 @mcp.tool():

# src/tools/weather.py
from core.server import mcp
from core.utils import get_tool_config, get_env_var

@mcp.tool()
def weather(location: str) -> str:
    """Get weather information for a location."""
    
    # Get tool configuration
    config = get_tool_config("weather")
    api_key = get_env_var(config.get("api_key_env", "WEATHER_API_KEY"))
    base_url = config.get("base_url", "https://api.openweathermap.org/data/2.5")
    
    # TODO: Implement weather API call
    return f"Weather for {location}: Sunny, 72°F"

工具示例

生成的工具模板包括常见模式的注释示例:

# HTTP API calls
# async with httpx.AsyncClient() as client:
#     response = await client.get(f"{base_url}/weather?q={location}&appid={api_key}")
#     return response.json()

# Database operations  
# async with asyncpg.connect(connection_string) as conn:
#     result = await conn.fetchrow("SELECT * FROM weather WHERE location = $1", location)
#     return dict(result)

# File processing
# with open(file_path, 'r') as f:
#     content = f.read()
#     return {"content": content, "size": len(content)}

配置

在中配置工具 kmcp.yaml:

tools:
  weather:
    api_key_env: "WEATHER_API_KEY"
    base_url: "https://api.openweathermap.org/data/2.5"
    timeout: 30
  
  database:
    connection_string_env: "DATABASE_URL"
    max_connections: 10

测试

运行生成的测试以验证您的工具是否正确加载:

uv run pytest tests/

发展

添加依赖关系

更新 pyproject.toml 并运行:

uv sync

代码质量

uv run black .
uv run ruff check .
uv run mypy .

部署

码头工人

# Build image (handles lockfile automatically)
kmcp build

# Run container
docker run -i multi-tenant-github-mcp:latest

Kubernetes

# Deploy to Kubernetes
kmcp deploy mcp --apply

# Check deployment status
kubectl get mcpserver multi-tenant-github-mcp