mcp-compose

AliQambari/mcp-compose

3.2

If you are the rightful owner of mcp-compose and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

The Modular MCP Composition Architecture is a production-ready server framework designed to unify multiple MCP servers into a single endpoint, enhancing performance, reliability, and developer experience.

Tools
2
Resources
0
Prompts
0

๐Ÿš€ Modular MCP Composition Architecture

Python FastMCP PostgreSQL MySQL Redis Docker

A production-ready MCP server composition - compose multiple MCP servers into one unified endpoint

๐Ÿš€ Quick Start โ€ข ๐Ÿ“– Documentation โ€ข ๐ŸŽฏ Features โ€ข ๐Ÿ”ง Configuration


๐ŸŽฏ Features

๐Ÿš€ Performance๐Ÿ›ก๏ธ Reliability๐Ÿ”ง Developer Experience
Redis CachingConnection PoolingType Safety
Async OperationsError HandlingClean Architecture
Sub-ms ResponseGraceful FailuresComprehensive Logging

โšก Lightning Fast

  • Redis Caching: 100x faster responses after first query
  • Connection Pooling: Efficient database connection management
  • Async/Await: Non-blocking operations for maximum throughput

๐Ÿ—๏ธ Production Ready

  • Modular Architecture: Clean separation of concerns
  • Type Safety: Comprehensive type hints prevent runtime errors
  • Error Handling: Structured exception hierarchy with user-friendly messages
  • Observability: Detailed logging with cache hit/miss tracking

๐Ÿ”Œ Modular Composition Architecture

  • Server Composition: Combine multiple MCP servers into one endpoint
  • Pluggable Services: Database, Math, and custom service modules
  • Dynamic Loading: Enable/disable servers via configuration
  • Tag-based Filtering: Fine-grained control over tool availability
  • Extensible Framework: Add new MCP servers without touching core code

๐Ÿš€ Quick Start

1๏ธโƒฃ Installation

# Clone the repository
git clone <your-repo-url>
cd mcp_composition_server

# Install dependencies
pip install -e .

# Install Redis (if not already installed)
# Windows: Download from https://redis.io/download
# Linux: sudo apt install redis-server
# macOS: brew install redis

2๏ธโƒฃ Configuration

Create your .env file:

# PostgreSQL Configuration
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=your_password

# MySQL Configuration  
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=your_password

# Redis Cache Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0

# Application Settings
LOG_LEVEL=INFO
SAVE_FILES=false

3๏ธโƒฃ Start the Server

# Start Redis server
redis-server

# Start the composed MCP server
python main.py

4๏ธโƒฃ Test with Client

from fastmcp import Client

async def test_database():
    client = Client("http://localhost:8000/mcp")
    async with client:
        # List tables in PostgreSQL database
        result = await client.call_tool("list_tables", {
            "db_type": "postgres",
            "db_name": "your_database"
        })
        print(result.content[0].text)

import asyncio
asyncio.run(test_database())

๐Ÿ“Š Performance Comparison

OperationWithout CacheWith Redis CacheImprovement
First Call~500ms~500ms-
Subsequent Calls~500ms~5ms100x faster ๐Ÿš€
Database LoadHighMinimal95% reduction

๐Ÿ—๏ธ Architecture Overview

graph TB
    Client[MCP Client] --> Composer[MCP Composition Server]
    Composer --> DB[Database Server]
    Composer --> Math[Math Server]
    Composer --> Custom[Custom Server N...]
    
    DB --> Cache{Redis Cache}
    Math --> Cache
    
    Cache -->|Hit| Return[Cached Response]
    Cache -->|Miss| Process[Process Request]
    
    Process --> MySQL[(MySQL)]
    Process --> Postgres[(PostgreSQL)]
    Process --> Store[Store in Cache]
    Store --> Return

๐ŸŽจ Design Patterns Used

  • ๐Ÿ”— Composition Pattern: Multiple MCP servers composed into unified interface
  • ๐Ÿญ Factory Pattern: Database client creation without exposing instantiation logic
  • ๐Ÿ“š Repository Pattern: Abstract database operations through consistent interfaces
  • ๐Ÿ’‰ Dependency Injection: Services receive dependencies through constructors
  • ๐ŸŽฏ Single Responsibility: Each server/service has one clear purpose
  • ๐Ÿ”„ Strategy Pattern: Different implementations with same interface
  • ๐Ÿ—๏ธ Builder Pattern: Dynamic server composition based on configuration

๐Ÿ”ง Configuration

๐Ÿ“ Project Structure

mcp-composition-server/
โ”œโ”€โ”€ ๐Ÿ“ database/              # Database layer
โ”‚   โ”œโ”€โ”€ __init__.py          # Package exports
โ”‚   โ”œโ”€โ”€ base.py              # Abstract interfaces
โ”‚   โ”œโ”€โ”€ mysql_client.py      # MySQL implementation
โ”‚   โ”œโ”€โ”€ postgres_client.py   # PostgreSQL implementation
โ”‚   โ””โ”€โ”€ factory.py           # Database factory
โ”œโ”€โ”€ ๐Ÿ“ services/             # Business logic layer
โ”‚   โ”œโ”€โ”€ __init__.py          # Package exports
โ”‚   โ”œโ”€โ”€ table_service.py     # Table operations
โ”‚   โ””โ”€โ”€ math_service.py      # Math operations
โ”œโ”€โ”€ ๐Ÿ“ servers/              # Server modules
โ”‚   โ”œโ”€โ”€ __init__.py          # Package exports
โ”‚   โ”œโ”€โ”€ database_server.py   # Database MCP server
โ”‚   โ””โ”€โ”€ math_server.py       # Math MCP server
โ”œโ”€โ”€ ๐Ÿ“ utils/                # Utility modules
โ”‚   โ”œโ”€โ”€ __init__.py          # Package exports
โ”‚   โ”œโ”€โ”€ logger.py           # Logging utilities
โ”‚   โ”œโ”€โ”€ file_utils.py       # File operations
โ”‚   โ””โ”€โ”€ cache_decorator.py   # Redis caching with aiocache
โ”œโ”€โ”€ ๐Ÿ“ outputs/              # Output files (optional)
โ”œโ”€โ”€ config.py               # Configuration management
โ”œโ”€โ”€ main.py                 # Composed MCP server
โ”œโ”€โ”€ test_client.py          # Test client example
โ”œโ”€โ”€ pyproject.toml          # Project configuration & dependencies
โ”œโ”€โ”€ .env                    # Environment variables
โ””โ”€โ”€ README.md              # This documentation

โš™๏ธ Environment Variables

VariableDefaultDescription
POSTGRES_HOSTlocalhostPostgreSQL server host
POSTGRES_PORT5432PostgreSQL server port
POSTGRES_USERpostgresPostgreSQL username
POSTGRES_PASSWORD-PostgreSQL password
MYSQL_HOSTlocalhostMySQL server host
MYSQL_PORT3306MySQL server port
REDIS_HOSTlocalhostRedis server host
REDIS_PORT6379Redis server port
LOG_LEVELINFOLogging level (DEBUG, INFO, WARNING, ERROR)
SAVE_FILESfalseEnable/disable file output
ENABLED_SERVERSdatabase,mathComma-separated list of servers to enable
SERVER_INCLUDE_TAGS-Only include tools with these tags
SERVER_EXCLUDE_TAGSadminExclude tools with these tags

๐Ÿ› ๏ธ Available Tools

๐Ÿ“‹ Database Tools

list_tables

List all tables in a database with caching support.

Parameters:

  • db_type (string): Database type - "mysql" or "postgres"
  • db_name (string): Name of the database

Example:

result = await client.call_tool("list_tables", {
    "db_type": "postgres",
    "db_name": "my_database"
})

๐Ÿงฎ Math Tools

add

Add two numbers with caching support.

Parameters:

  • a (float): First number
  • b (float): Second number

Example:

result = await client.call_tool("add", {
    "a": 5.5,
    "b": 3.2
})

๐Ÿ“ˆ Monitoring & Logging

๐Ÿ” Cache Monitoring

The server provides detailed logging for cache operations:

# Cache Miss (First Call)
๐Ÿ’พ CACHE MISS: list_tables:postgres:mydb - fetching from database
๐Ÿ” Fetching tables from postgres database: mydb
โœ… Database query completed: 25 tables from mydb
โœ… CACHED: list_tables:postgres:mydb (TTL: 300s)

# Cache Hit (Subsequent Calls)  
๐ŸŽฏ CACHE HIT: list_tables:postgres:mydb

๐Ÿ“Š Performance Metrics

Monitor your server performance:

  • Cache Hit Rate: Track how often cache is used vs database queries
  • Response Times: Monitor query execution times
  • Connection Pool: Database connection utilization
  • Error Rates: Track and alert on failures

๐Ÿš€ Extending the Architecture

๐Ÿ”Œ Adding New MCP Servers

  1. Create your server in servers/my_server.py:
from fastmcp import FastMCP

def create_my_server() -> FastMCP:
    server = FastMCP("my-service")
    
    @server.tool()
    async def my_tool(param: str) -> str:
        return f"Processed: {param}"
    
    return server
  1. Add to servers/__init__.py:
from .my_server import create_my_server
__all__ = [..., "create_my_server"]
  1. Update main.py:
if "my_service" in enabled_servers:
    await main_server.import_server(create_my_server())

๐Ÿ› ๏ธ Configuration-Driven Composition

# Enable only specific servers
ENABLED_SERVERS=database,math,my_service

# Filter by tags
SERVER_INCLUDE_TAGS=production
SERVER_EXCLUDE_TAGS=admin,debug

check EXTENDING.md for detailed examples

๐ŸŽฏ Adding Caching to Any Method

from utils.cache_decorator import cached

class MyService:
    @cached(ttl=600)  # 10 minutes cache
    async def expensive_operation(self, param: str) -> Result:
        # Automatically cached!
        pass

๐Ÿณ Docker Support

๐Ÿ“ฆ Docker Compose

version: '3.8'
services:
  mcp-composition-server:
    build: .
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_HOST=postgres
      - REDIS_HOST=redis
      - ENABLED_SERVERS=database,math
    depends_on:
      - postgres
      - redis

  postgres:
    image: postgres:15
    environment:
      POSTGRES_DB: mydb
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"
  - postgres
  - redis

postgres: image: postgres:15 environment: POSTGRES_DB: mydb POSTGRES_USER: user POSTGRES_PASSWORD: password

redis: image: redis:7-alpine ports: - "6379:6379"


### ๐Ÿš€ **Quick Docker Start**

```bash
# Build and start all services
docker-compose up -d

# View logs
docker-compose logs -f mcp-composition-server

# Stop services
docker-compose down

๐Ÿงช Testing

Use the included test client to verify functionality:

# Test the composed server with all tools
python test_client.py

# Test specific server composition
ENABLED_SERVERS=math python test_client.py

๐Ÿค Contributing

We welcome contributions! Here's how to get started:

๐Ÿ“ Contribution Guidelines

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ™ Acknowledgments

  • FastMCP - For the excellent MCP framework
  • aiomysql & asyncpg - For async database drivers
  • aiocache - For Redis caching made simple
  • Redis - For blazing-fast caching