Template MCP Server
Description
A production-ready template for developing Model Context Protocol (MCP) servers using Python and FastMCP. This server provides a foundation for creating MCP-compliant servers with comprehensive examples of tools, structured logging, configuration management, and containerized deployment.
The template includes three example MCP tools: a multiply calculator, a code review prompt generator, and a Red Hat logo resource handler. It demonstrates best practices for MCP server development including proper error handling, health checks, multiple transport protocols (HTTP, SSE, streamable-HTTP), SSL support, and comprehensive development tooling.
Architecture
System Architecture
Control Flow
Code Structure
Key Components
main.py
: Application entry point with configuration validation, error handling, and uvicorn server startupapi.py
: FastAPI application setup with transport protocol selection (HTTP/SSE/streamable-HTTP) and health endpointsmcp.py
: Core MCP server class that registers tools using FastMCP decoratorssettings.py
: Environment-based configuration using Pydantic BaseSettings with validationtools/
: MCP tool implementations demonstrating arithmetic, prompts, and resource access patternsutils/pylogger.py
: Structured JSON logging using structlog with comprehensive processors
Current MCP Tools
multiply_numbers
: Demonstrates basic arithmetic operations with error handlingread_redhat_logo_content
: Shows resource access patterns with base64 encodingget_code_review_prompt
: Illustrates prompt generation for code analysis
How to Run the Code Locally
Prerequisites
- Python 3.12 or higher
- uv (fast Python package installer and resolver)
Setup
- Install uv (if not already installed):
- Clone the repository:
- Create and activate a virtual environment with uv:
- Install the package and dependencies:
- Configure environment variables:
- Run the server:
Configuration Options
The server configuration is managed through environment variables:
Variable | Default | Description |
---|---|---|
MCP_HOST | 0.0.0.0 | Server bind address |
MCP_PORT | 3000 | Server port (1024-65535) |
MCP_TRANSPORT_PROTOCOL | streamable-http | Transport protocol (http , sse , streamable-http ) |
MCP_SSL_KEYFILE | None | SSL private key file path |
MCP_SSL_CERTFILE | None | SSL certificate file path |
PYTHON_LOG_LEVEL | INFO | Logging level (DEBUG , INFO , WARNING , ERROR , CRITICAL ) |
Using Podman
- Build and run with Podman Compose:
- Or build manually:
Verify Installation
- Health check:
- Test MCP tools:
How to Test the Code Locally
Development Environment Setup
- Install development dependencies:
- Install pre-commit hooks:
Running Tests
The project includes a comprehensive test suite with 81+ tests covering unit tests, integration tests, and various edge cases.
- Run all tests:
- Run tests with coverage reporting:
- Run tests by category:
- Run specific test modules:
- Run tests with different output formats:
Code Quality Checks
- Linting and formatting with Ruff:
- Type checking with MyPy:
- Docstring validation:
- Run all pre-commit checks:
Test Suite Overview
The project includes a comprehensive test suite with the following structure:
Test Category | Count | Description |
---|---|---|
Unit Tests | 71 | Individual component testing with mocking |
Integration Tests | 10 | End-to-end workflow testing |
Total Tests | 81+ | Complete test coverage |
Test Files:
test_multiply_tool.py
- 12 tests covering arithmetic operations, edge cases, error handlingtest_redhat_logo.py
- 10 tests covering async file operations, base64 encoding, error scenariostest_code_review_prompt.py
- 14 tests covering prompt generation, multiple languages, formattingtest_settings.py
- 20 tests covering configuration, environment variables, validationtest_mcp_server.py
- 15 tests covering server initialization, tool registration, error handlingtest_integration.py
- 10 tests covering complete workflows and system integration
Test Features:
- ✅ Comprehensive error handling validation
- ✅ Async function testing support
- ✅ Mock external dependencies
- ✅ Environment isolation with fixtures
- ✅ Performance testing for large data
- ✅ Concurrent usage simulation
- ✅ Configuration validation testing
Manual Testing
- Container testing:
- SSL testing (if configured):
Continuous Integration & Deployment
This project uses GitHub Actions for automated CI/CD workflows to ensure code quality, security, and reliability.
CI/CD Workflows
Workflow | Trigger | Purpose |
---|---|---|
CI | Push to main, PRs | Run tests, linting, type checking, security scans |
Dependency Updates | Weekly schedule | Automated dependency updates and security audits |
CI Pipeline Features
✅ Code Quality Assurance:
- Multi-Python version testing (3.12, 3.13)
- Comprehensive test suite execution (81+ tests)
- Code coverage reporting (80%+ requirement)
- Ruff linting and formatting validation
- MyPy type checking
- Docstring validation with pydocstyle
✅ Security & Compliance:
- Bandit security linting
- Safety dependency vulnerability scanning
✅ Automation & Maintenance:
- Dependabot configuration for automated dependency updates
- Pre-commit hook automation
- Weekly security audits
- Automated PR creation for dependency updates
Running CI Checks Locally
Before pushing code, run the same checks that CI runs:
Branch Protection
The main
branch is protected with the following requirements:
- All CI checks must pass
- Pull request reviews required
- Up-to-date branches required
- No direct pushes to main
How to Contribute
Development Workflow
- Fork and clone:
- Create feature branch:
- Set up development environment:
- Make changes following our standards
- Run comprehensive testing:
- Commit and push:
- Create Pull Request
Coding Standards
- Python Style: Follow PEP 8 (enforced by Ruff)
- Type Annotations: Required for all public functions and methods
- Documentation: Google-style docstrings for all public APIs
- Testing: Write tests for new functionality with pytest
- Commits: Use conventional commit format (
feat:
,fix:
,docs:
, etc.) - Error Handling: Use structured logging and proper exception handling
Adding New MCP Tools
- Create tool module:
- Register in MCP server:
- Add tests:
- Update documentation
Adding New Resources
- Place assets in:
template_mcp_server/src/tools/assets/
- Create resource handler in:
template_mcp_server/src/tools/
- Register in:
template_mcp_server/src/mcp.py
- Add tests and documentation
Code Review Guidelines
- All changes require pull request review
- Automated checks must pass (tests, linting, type checking)
- Documentation should be updated for user-facing changes
- Breaking changes require discussion and versioning consideration
Getting Help
- Issues: Open GitHub issues for bugs or feature requests
- Discussions: Use GitHub Discussions for questions
- Documentation: Check existing docs and code examples
- Testing: Provide minimal reproduction cases for bugs
This server cannot be installed
hybrid server
The server is able to function both locally and remotely, depending on the configuration or use case.
A production-ready foundation template for building Model Context Protocol (MCP) servers with FastAPI, featuring modular tools, comprehensive testing, and OpenShift deployment configurations. Includes automated transformation scripts to create custom domain-specific MCP servers.