MCP Template

MCP Template

A production-ready Python template for building MCP servers with enterprise features including registry integration, configuration management, structured logging, and extensible patterns for tools, resources, and prompts.

Category
访问服务器

README

MCP Template

Production-ready MCP (Model Context Protocol) server template in Python with registry integration, comprehensive configuration management, and extensibility patterns.

Features

  • Production-Ready: Enterprise-grade error handling, structured logging, and graceful shutdown
  • Registry Integration: Automatic server registration, heartbeats, and deregistration
  • Configuration Management: YAML-based config with environment variable overrides and Pydantic validation
  • Extensible Architecture: Easy-to-extend base classes for tools, resources, and prompts
  • Type Safety: Full type hints throughout the codebase
  • Developer Experience: Comprehensive CLI, extensive documentation, and example implementations
  • Testing: Unit and integration test examples with pytest

Quick Start

Installation

# Clone or copy this template
cd mcp-template

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

# Install dependencies
pip install -e ".[dev]"

# Or using requirements.txt
pip install -r requirements-dev.txt

Initialize Configuration

# Initialize default configuration
mcp-template init

# This creates:
# - config/config.yaml (server configuration)
# - .env (environment variables)

Run the Server

# Run with default configuration
mcp-template run

# Run with custom config
mcp-template run --config path/to/config.yaml

# Run in debug mode
mcp-template run --debug

# Validate configuration
mcp-template validate

# Check health
mcp-template health

Project Structure

mcp-template/
├── src/mcp_template/
│   ├── core/                  # Core server components
│   │   ├── server.py          # Base MCP server implementation
│   │   ├── settings.py        # Pydantic settings models
│   │   ├── config_loader.py   # Configuration loader
│   │   └── logger.py          # Structured logging setup
│   ├── registry/              # Registry integration
│   │   ├── base.py            # Registry client interface
│   │   ├── http_client.py     # HTTP registry implementation
│   │   └── manager.py         # Registry lifecycle manager
│   ├── tools/                 # MCP tools
│   │   ├── calculator.py      # Example calculator tool
│   │   └── search.py          # Example search tool
│   ├── resources/             # MCP resources
│   │   ├── config.py          # Configuration resource
│   │   └── status.py          # Status resource
│   ├── prompts/               # MCP prompts
│   │   └── example.py         # Example prompt
│   ├── app.py                 # Main application
│   └── cli.py                 # Command-line interface
├── config/
│   └── config.yaml            # Server configuration
├── tests/
│   ├── unit/                  # Unit tests
│   └── integration/           # Integration tests
├── pyproject.toml             # Project metadata and dependencies
├── requirements.txt           # Production dependencies
├── requirements-dev.txt       # Development dependencies
├── Makefile                   # Development commands
└── README.md                  # This file

Configuration

Configuration File (config/config.yaml)

The main configuration file supports:

  • Server settings: Name, version, description, debug mode
  • Logging: Level, format (JSON/text), file output
  • Registry: URL, authentication, heartbeat settings, metadata
  • Tools/Resources/Prompts: Enable/disable specific components

Example:

server:
  name: "my-mcp-server"
  version: "0.1.0"
  description: "My custom MCP server"

logging:
  level: "INFO"
  format: "json"

registry:
  enabled: true
  url: "https://registry.example.com/api/v1"
  auth:
    type: "api_key"
    api_key: "${REGISTRY_API_KEY}"
  heartbeat:
    enabled: true
    interval: 60

tools:
  enabled:
    - "example_calculator"
    - "example_search"

Environment Variables

Override configuration using environment variables with double underscore as separator:

# Override server name
export SERVER__NAME="production-server"

# Override registry URL
export REGISTRY__URL="https://prod-registry.example.com"

# Set API key
export REGISTRY__AUTH__API_KEY="your-api-key"

# Override logging level
export LOGGING__LEVEL="DEBUG"

Environment Variables File (.env)

Create a .env file for local development:

# Registry
REGISTRY_API_KEY=your-api-key-here

# Server overrides
SERVER__DEBUG=false

# Logging
LOGGING__LEVEL=INFO

Adding Custom Components

Adding a New Tool

  1. Create a new file in src/mcp_template/tools/:
# src/mcp_template/tools/my_tool.py
import mcp.types as types

async def my_tool_handler(arguments: dict) -> str:
    """Handle the tool call."""
    # Your implementation here
    return "Result"

MY_TOOL_SCHEMA = types.Tool(
    name="my_tool",
    description="Description of what the tool does",
    inputSchema={
        "type": "object",
        "properties": {
            "param1": {
                "type": "string",
                "description": "Parameter description"
            }
        },
        "required": ["param1"]
    }
)
  1. Register it in src/mcp_template/app.py:
from .tools.my_tool import my_tool_handler, MY_TOOL_SCHEMA

# In MCPTemplateApp._register_components():
self.server.register_tool(
    "my_tool",
    my_tool_handler,
    MY_TOOL_SCHEMA,
)
  1. Enable it in config/config.yaml:
tools:
  enabled:
    - "my_tool"

Adding a New Resource

Similar pattern - create handler and schema, register in app.py, enable in config.

# src/mcp_template/resources/my_resource.py
import mcp.types as types

async def my_resource_handler(uri: str) -> str:
    """Handle resource read."""
    return "Resource data"

MY_RESOURCE_SCHEMA = types.Resource(
    uri="myresource://example",
    name="My Resource",
    description="Resource description",
    mimeType="application/json"
)

Adding a New Prompt

# src/mcp_template/prompts/my_prompt.py
import mcp.types as types

async def my_prompt_handler(arguments: dict) -> types.GetPromptResult:
    """Handle prompt request."""
    return types.GetPromptResult(
        description="Prompt description",
        messages=[
            types.PromptMessage(
                role="user",
                content=types.TextContent(
                    type="text",
                    text="Prompt text"
                )
            )
        ]
    )

MY_PROMPT_SCHEMA = types.Prompt(
    name="my_prompt",
    description="Prompt description",
    arguments=[
        types.PromptArgument(
            name="arg1",
            description="Argument description",
            required=True
        )
    ]
)

Registry Integration

The template includes a pluggable registry system for server discovery and management.

How It Works

  1. Registration: On startup, server registers with the registry
  2. Heartbeat: Periodic heartbeats maintain the registration
  3. Deregistration: Graceful shutdown deregisters the server

Custom Registry Backend

Implement the RegistryClient interface for custom backends:

from mcp_template.registry.base import RegistryClient

class MyRegistryClient(RegistryClient):
    async def register(self, server_info: dict) -> dict:
        # Your implementation
        pass

    async def deregister(self, server_id: str) -> bool:
        # Your implementation
        pass

    # Implement other methods...

Then use it in your application:

from mcp_template.registry.manager import RegistryManager

registry_manager = RegistryManager(settings, client=MyRegistryClient())

Development

Setup Development Environment

# Install with development dependencies
pip install -e ".[dev]"

# Or using make
make install

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov

# Run only unit tests
pytest tests/unit

# Run only integration tests
pytest tests/integration -m integration

# Using make
make test

Code Quality

# Format code
make format

# Lint code
make lint

# Type check
make typecheck

# Run all checks
make check

Pre-commit Hooks

# Install pre-commit hooks
pre-commit install

# Run manually
pre-commit run --all-files

Deployment

Docker

Create a Dockerfile:

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY src/ src/
COPY config/ config/

RUN pip install -e .

CMD ["mcp-template", "run"]

Build and run:

docker build -t mcp-template .
docker run -e REGISTRY_API_KEY=your-key mcp-template

Environment-Specific Configuration

Create different config files for each environment:

config/
├── config.yaml          # Default
├── config.dev.yaml      # Development
├── config.staging.yaml  # Staging
└── config.prod.yaml     # Production

Run with specific config:

mcp-template run --config config/config.prod.yaml

Monitoring and Observability

Structured Logging

All logs are structured (JSON format by default) for easy parsing:

{
  "timestamp": "2024-01-01T12:00:00.000Z",
  "level": "info",
  "event": "Server started",
  "server": "my-server",
  "version": "0.1.0"
}

Health Checks

# CLI health check
mcp-template health

# Programmatic health check
from mcp_template.app import create_app

app = create_app()
health = await app.server.health_check()

Troubleshooting

Common Issues

Configuration file not found

# Initialize configuration
mcp-template init

# Or specify path
mcp-template run --config path/to/config.yaml

Registry connection failed

# Check registry health
mcp-template health

# Disable registry temporarily
# In config.yaml:
registry:
  enabled: false

Import errors

# Reinstall in editable mode
pip install -e .

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests
  5. Run code quality checks: make check
  6. Submit a pull request

License

MIT License - see LICENSE file for details

Resources

Support

For issues, questions, or contributions, please open an issue on GitHub.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选