Skip to content

cunicopia-dev/enterprise-ai-backend

Repository files navigation

FastAPI Multi-Provider LLM Platform with MCP Integration

Python FastAPI PostgreSQL License PRs Welcome

A production-ready FastAPI backend featuring native Model Context Protocol (MCP) support, multi-provider LLM integration, PostgreSQL database, and comprehensive REST API endpoints.

FastAPI Β Β Β Β  PostgreSQL

πŸš€ Key Features

πŸ”Œ Native MCP (Model Context Protocol) Integration

  • "The USB-C of AI Applications": Standardized tool calling across all LLM providers
  • Always-On Tools: MCP tools automatically available without special configuration
  • Multi-Tool Chaining: Complex workflows with automatic error recovery
  • Tool Execution Visibility: Complete transparency showing all tool executions
  • Multiple MCP Servers: Support for filesystem, GitHub, Notion, and custom servers
  • Zero Configuration: Tools discovered and integrated automatically

πŸ€– Multi-Provider LLM Support

  • Anthropic Claude: Full support for Opus 4, Sonnet 4.0, Haiku (including 2025 models)
  • OpenAI GPT: GPT-4o, GPT-4o mini, o1 preview/mini models
  • Google Gemini: Complete 2.5 family (Pro, Flash, Flash Lite) with thinking capabilities
  • Ollama: Local model support with OpenAI-compatible function calling
  • Unified Interface: Single API for all providers with automatic format conversion
  • Provider Hot-Swapping: Switch providers mid-conversation seamlessly

πŸ—οΈ Enterprise-Grade Architecture

  • PostgreSQL Database: Robust data persistence with proper indexing
  • Repository Pattern: Clean separation of concerns for maintainability
  • Dependency Injection: FastAPI's powerful DI system throughout
  • Async/Await: High-performance async operations
  • Connection Pooling: Optimized database connections
  • Rate Limiting: Per-user rate limiting with database tracking

πŸ” Security & Authentication

  • Database-Backed Auth: User management with API key generation
  • Multi-User Support: Complete data isolation between users
  • Rate Limiting: Configurable per-user rate limits
  • API Key Management: Secure token generation and validation
  • Role-Based Access: User-level permissions (extensible to roles)

πŸ“Š Advanced Features

  • Usage Tracking: Monitor tokens, costs, and usage per provider/model
  • System Prompt Library: Create, manage, and share prompt templates
  • Conversation Management: Multiple chat sessions with full history
  • Provider Health Monitoring: Real-time status checks for all providers
  • Automatic Migration: Seamless upgrade from file-based to database storage
  • Comprehensive Testing: Full test coverage with pytest

πŸ“‹ Table of Contents

Quick Start

Get up and running in 5 minutes with Docker:

# 1. Clone and enter the repository
git clone https://github.com/yourusername/fast-api-agents.git
cd fast-api-agents

# 2. Copy configuration files
cp .env.example .env
cp mcp_servers_config.example.json mcp_servers_config.json

# 3. Start with Docker Compose
docker-compose up -d

# 4. Access the API
# API: http://localhost:8000
# Docs: http://localhost:8000/docs
# PostgreSQL: localhost:5435

Or run locally without Docker:

# 3. Install dependencies and set up database
pip install -r requirements.fastapi.txt
createuser -U postgres fastapi_user
createdb -U postgres fastapi_db
psql -U fastapi_user -d fastapi_db -f sql/setup.sql

# 4. Start the application
python src/main.py

That's it! The application will:

  • βœ… Connect to your PostgreSQL database
  • βœ… Start the FastAPI backend with MCP support
  • βœ… Connect to configured MCP servers
  • βœ… Provide interactive API documentation

First Steps

  1. Get your API key: Check the logs or use the default from .env
  2. Test the API: Use the interactive docs at http://localhost:8000/docs
  3. Send a request: Try the /chat endpoint with your API key
  4. Try MCP tools: Send "Create a test file and read it back"

Architecture Overview

The application follows a sophisticated multi-layer architecture with MCP at its core:

graph TB
    subgraph "Client Applications"
        WebClients[🌐 Web Apps]
        MobileClients[πŸ“± Mobile Apps]
        CLIClients[⚑ CLI Tools]
        APIClients[πŸ”§ API Clients]
    end
    
    subgraph "FastAPI Backend"
        subgraph "API Layer"
            FastAPI[πŸš€ FastAPI Application]
            Auth[πŸ” Authentication]
            RateLimit[⏱️ Rate Limiting]
            CORS[🌍 CORS Middleware]
        end
        
        subgraph "Business Logic"
            ChatService[πŸ’¬ Chat Service]
            UserService[πŸ‘€ User Management]
            ProviderMgr[πŸ€– Provider Manager]
        end
        
        subgraph "MCP Integration"
            MCPHost[πŸ”Œ MCP Host]
            MCPClients[πŸ“‘ MCP Clients]
            ToolRouter[πŸ› οΈ Tool Router]
        end
        
        subgraph "LLM Providers"
            Anthropic[🧠 Anthropic Claude]
            OpenAI[🎯 OpenAI GPT]
            Google[🌟 Google Gemini]
            Ollama[🏠 Ollama Local]
        end
    end
    
    subgraph "External Services"
        subgraph "MCP Servers"
            FileSystem[πŸ“‚ Filesystem]
            GitHub[πŸ™ GitHub]
            Notion[πŸ“ Notion]
            CustomMCP[βš™οΈ Custom Servers]
        end
        
        subgraph "AI APIs"
            AnthropicAPI[Anthropic API]
            OpenAIAPI[OpenAI API]
            GoogleAPI[Google API]
        end
    end
    
    subgraph "Data Layer"
        PostgreSQL[(🐘 PostgreSQL)]
        Tables[πŸ“Š Users, Chats, Messages, Usage]
    end
    
    %% Client connections
    WebClients --> FastAPI
    MobileClients --> FastAPI
    CLIClients --> FastAPI
    APIClients --> FastAPI
    
    %% Internal API flow
    FastAPI --> Auth
    FastAPI --> RateLimit
    FastAPI --> ChatService
    ChatService --> ProviderMgr
    ProviderMgr --> MCPHost
    
    %% MCP connections
    MCPHost --> MCPClients
    MCPClients --> FileSystem
    MCPClients --> GitHub
    MCPClients --> Notion
    MCPClients --> CustomMCP
    
    %% Provider connections
    ProviderMgr --> Anthropic
    ProviderMgr --> OpenAI
    ProviderMgr --> Google
    ProviderMgr --> Ollama
    
    %% External API connections
    Anthropic --> AnthropicAPI
    OpenAI --> OpenAIAPI
    Google --> GoogleAPI
    
    %% Database connections
    ChatService --> PostgreSQL
    UserService --> PostgreSQL
    Auth --> PostgreSQL
    
    %% Styling
    classDef client fill:#e3f2fd,stroke:#1976d2,stroke-width:2px,color:#000000
    classDef api fill:#f1f8e9,stroke:#689f38,stroke-width:2px,color:#000000
    classDef mcp fill:#fff3e0,stroke:#f57c00,stroke-width:2px,color:#000000
    classDef provider fill:#fce4ec,stroke:#c2185b,stroke-width:2px,color:#000000
    classDef external fill:#f3e5f5,stroke:#7b1fa2,stroke-width:2px,color:#000000
    classDef data fill:#e8f5e8,stroke:#388e3c,stroke-width:2px,color:#000000
    
    class WebClients,MobileClients,CLIClients,APIClients client
    class FastAPI,Auth,RateLimit,CORS,ChatService,UserService api
    class MCPHost,MCPClients,ToolRouter,FileSystem,GitHub,Notion,CustomMCP mcp
    class ProviderMgr,Anthropic,OpenAI,Google,Ollama provider
    class AnthropicAPI,OpenAIAPI,GoogleAPI external
    class PostgreSQL,Tables data
Loading

Core Components

  1. MCP Host (src/utils/mcp/host.py)

    • Central coordinator for all MCP operations
    • Manages multiple MCP clients (1:1 with servers)
    • Aggregates tools from all connected servers
    • Handles tool routing and execution
  2. MCP Enhanced Providers (src/utils/provider/mcp_enhanced_provider.py)

    • Wraps base providers with MCP capabilities
    • Handles multi-tool chaining loops
    • Provider-specific message format conversion
    • Tool execution visibility
  3. Provider Implementations

    • Each provider has native function calling support
    • Automatic format conversion between providers
    • Multi-tool chaining with proper stop reasons
    • Complete error handling and recovery

MCP Integration Details

How MCP Works in This Application

  1. Always-On Architecture

    # MCP tools are automatically available to all providers
    response = await chat_interface.send_message(
        "Create a file and read it back",
        provider="anthropic",  # Works with any provider!
        model="claude-3-5-haiku-20241022"
    )
  2. Multi-Tool Chaining

    • Automatic execution of multiple tools in sequence
    • Error recovery (e.g., access denied β†’ find allowed directories)
    • Complete visibility of all tool executions
    • Provider-specific protocol handling
  3. Tool Execution Flow

    User Message β†’ LLM decides tools needed β†’ Execute tools β†’ 
    Feed results back β†’ LLM continues β†’ Repeat until done
    

Supported MCP Servers

Configure in mcp_servers_config.json:

{
  "mcp_servers": {
    "filesystem": {
      "transport_type": "stdio",
      "config": {
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/dir"]
      },
      "enabled": true
    },
    "github": {
      "transport_type": "stdio",
      "config": {
        "command": "npx",
        "args": ["-y", "@modelcontextprotocol/server-github"]
      },
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      },
      "enabled": true
    }
  }
}

Provider-Specific Tool Calling

Each provider has unique tool calling formats, all handled transparently:

Provider Tool Format Stop Reason Multi-Tool Support
Anthropic tool_use blocks stop_reason="tool_use" βœ… Native
OpenAI tool_calls array finish_reason="tool_calls" βœ… Native
Google Python functions Check for function calls βœ… Manual loop
Ollama OpenAI-compatible Always "stop", check calls βœ… Manual loop

Installation

Prerequisites

For Docker (Recommended):

  • Docker and Docker Compose
  • Ollama (optional, for local models)

For Local Development:

  • Python 3.13+
  • PostgreSQL 15+
  • Node.js (for MCP servers)
  • Ollama (optional, for local models)

Quick Start

  1. Clone the repository

    git clone https://github.com/yourusername/fast-api-agents.git
    cd fast-api-agents
  2. Install Python dependencies

    pip install -r requirements.fastapi.txt
    pip install -r requirements.test.txt  # Optional, for testing
  3. Set up environment variables

    cp .env.example .env
    # Edit .env with your configuration
  4. Set up the database

    # Option 1: Use Docker Compose (recommended)
    docker-compose up -d postgres
    
    # Option 2: Local PostgreSQL setup
    createuser -U postgres fastapi_user
    createdb -U postgres fastapi_db
    psql -U fastapi_user -d fastapi_db -f sql/setup.sql
  5. Configure MCP servers

    # Edit mcp_servers_config.json with your MCP server configurations
    # Install MCP servers (example for filesystem)
    npm install -g @modelcontextprotocol/server-filesystem

Configuration

Environment Variables (.env)

# API Security
API_KEY=your-secure-api-key-here

# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_NAME=fastapi_db
DB_USER=fastapi_user
DB_PASSWORD=your-secure-password

# LLM Provider API Keys
ANTHROPIC_API_KEY=your-anthropic-api-key
OPENAI_API_KEY=your-openai-api-key
GOOGLE_API_KEY=your-google-api-key

# MCP Configuration (optional)
GITHUB_TOKEN=your-github-token  # For GitHub MCP server
NOTION_TOKEN=your-notion-token  # For Notion MCP server

# Rate Limiting
RATE_LIMIT_PER_HOUR=1000

# Provider Settings
DEFAULT_PROVIDER=anthropic
OLLAMA_BASE_URL=http://localhost:11434

MCP Server Configuration

Edit mcp_servers_config.json to configure MCP servers:

  • Set allowed directories for filesystem access
  • Configure authentication tokens for services
  • Enable/disable specific servers
  • Add custom MCP servers

Running the Application

Docker Mode (Recommended)

# Start with Docker Compose
docker-compose up -d

# View logs
docker-compose logs -f

# Stop services
docker-compose down

Development Mode (Local)

# Start the backend with auto-reload
python src/main.py

Production Mode

# Install Gunicorn for production
pip install gunicorn

# Start with Gunicorn
gunicorn src.main:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000

Services will be available at:

API Documentation

Core Endpoints

Chat with MCP Tools

curl -X POST http://localhost:8000/chat \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "message": "Create a test file and read it back",
    "provider": "anthropic",
    "model": "claude-3-5-haiku-20241022"
  }'

Response includes tool execution details:

{
  "response": "I've created the file and read it back...\n\nπŸ”§ **Tool Executions:**\n\n**Round 1:**\n- `filesystem__write_file(path=test.txt, content=Hello World)`\n  β†’ Successfully wrote to test.txt\n\n**Round 2:**\n- `filesystem__read_file(path=test.txt)`\n  β†’ Hello World",
  "chat_id": "unique-chat-id",
  "success": true
}

MCP Management

  • GET /mcp/status - Overall MCP integration status
  • GET /mcp/servers - List all MCP servers and connection status
  • GET /mcp/tools - List all available tools
  • POST /mcp/servers/{server}/reconnect - Reconnect specific server

Provider Management

  • GET /providers - List all providers with capabilities
  • GET /providers/{provider}/models - Get available models
  • GET /providers/{provider}/health - Check provider health

Complete API Reference

Interactive documentation available at:

Testing

Running Tests

# Run all tests
pytest

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

# Run specific test categories
pytest -m unit
pytest -m integration
pytest -m mcp

Test Structure

tests/
β”œβ”€β”€ unit/               # Fast, isolated unit tests
β”‚   β”œβ”€β”€ test_mcp_host.py
β”‚   β”œβ”€β”€ test_providers.py
β”‚   └── test_models.py
β”œβ”€β”€ integration/        # Database and service integration
β”‚   β”œβ”€β”€ test_mcp_integration.py
β”‚   └── test_multi_provider.py
└── e2e/               # End-to-end scenarios
    └── test_mcp_tool_chains.py

Provider-Specific Features

Anthropic Claude

  • Models: Opus 4, Sonnet 4.0, Haiku (all variants)
  • Special Features: Native tool use blocks, streaming with tools
  • Best For: Complex reasoning, code generation, analysis

OpenAI GPT

  • Models: GPT-4o, GPT-4o mini, o1 preview/mini
  • Special Features: Parallel function calling, JSON mode
  • Best For: General tasks, creative writing, reasoning

Google Gemini

  • Models: 2.5 Pro/Flash family with thinking capabilities
  • Special Features: Built-in thinking, 1M context window
  • Best For: Large context tasks, multimodal (planned)

Ollama (Local)

  • Models: Any Ollama-compatible model
  • Special Features: Complete privacy, no API costs
  • Best For: Development, sensitive data, offline use

Extending the Platform

Adding New MCP Servers

  1. Install the MCP server:

    npm install -g @your-org/mcp-server-custom
  2. Add configuration to mcp_servers_config.json:

    {
      "custom_server": {
        "transport_type": "stdio",
        "config": {
          "command": "mcp-server-custom",
          "args": ["--config", "/path/to/config"]
        },
        "enabled": true
      }
    }
  3. Restart the application to auto-discover new tools

Adding New LLM Providers

  1. Create provider class inheriting from BaseProvider

  2. Implement required methods:

    • chat_completion()
    • chat_completion_stream()
    • list_models()
    • validate_config()
  3. Register in ProviderManager

  4. Add to database seed data

Troubleshooting

MCP Connection Issues

  • Check MCP server logs: tail -f logs/mcp-*.log
  • Verify server installation: which mcp-server-name
  • Test server manually: mcp-server-name --help
  • Check mcp_servers_config.json syntax

Provider-Specific Issues

  • Anthropic: Check API key and rate limits
  • OpenAI: Verify model access and quotas
  • Google: Ensure google-genai package is installed
  • Ollama: Check if service is running (ollama list)

Database Issues

  • Reset database: psql -U fastapi_user -d fastapi_db -f sql/setup.sql
  • Check connections: pg_isready -h localhost -p 5432
  • Verify migrations: Check alembic_version table

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

Project Structure

.
β”œβ”€β”€ src/                            # Backend source code
β”‚   β”œβ”€β”€ main.py                     # FastAPI application entry point
β”‚   └── utils/
β”‚       β”œβ”€β”€ mcp/                    # MCP Integration Layer
β”‚       β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”œβ”€β”€ host.py             # MCP Host - central coordinator
β”‚       β”‚   β”œβ”€β”€ client.py           # MCP Client implementation
β”‚       β”‚   β”œβ”€β”€ models.py           # MCP data models
β”‚       β”‚   β”œβ”€β”€ exceptions.py       # MCP-specific exceptions
β”‚       β”‚   β”œβ”€β”€ config_loader.py    # MCP configuration management
β”‚       β”‚   └── tool_converter.py   # Tool format conversions
β”‚       β”œβ”€β”€ provider/               # LLM Provider Implementations
β”‚       β”‚   β”œβ”€β”€ __init__.py
β”‚       β”‚   β”œβ”€β”€ base.py             # BaseProvider interface
β”‚       β”‚   β”œβ”€β”€ manager.py          # Provider manager with MCP integration
β”‚       β”‚   β”œβ”€β”€ mcp_enhanced_provider.py  # MCP wrapper for providers
β”‚       β”‚   β”œβ”€β”€ anthropic.py        # Anthropic Claude implementation
β”‚       β”‚   β”œβ”€β”€ openai.py           # OpenAI GPT implementation
β”‚       β”‚   β”œβ”€β”€ google.py           # Google Gemini implementation
β”‚       β”‚   └── ollama.py           # Ollama local models
β”‚       β”œβ”€β”€ models/                 # Data Models
β”‚       β”‚   β”œβ”€β”€ api_models.py       # Pydantic API models
β”‚       β”‚   └── db_models.py        # SQLAlchemy database models
β”‚       β”œβ”€β”€ repository/             # Database Repository Pattern
β”‚       β”‚   β”œβ”€β”€ base.py
β”‚       β”‚   β”œβ”€β”€ user_repository.py
β”‚       β”‚   β”œβ”€β”€ chat_repository.py
β”‚       β”‚   β”œβ”€β”€ message_repository.py
β”‚       β”‚   β”œβ”€β”€ provider_repository.py
β”‚       β”‚   └── system_prompt_repository.py
β”‚       β”œβ”€β”€ auth.py                 # Authentication middleware
β”‚       β”œβ”€β”€ chat_interface_db.py    # Chat interface with MCP
β”‚       β”œβ”€β”€ config.py               # Application configuration
β”‚       β”œβ”€β”€ database.py             # Database connection
β”‚       └── system_prompt_db.py     # System prompt management
β”œβ”€β”€ docs/                           # Documentation
β”‚   β”œβ”€β”€ mcp-multi-provider-implementation-report.md
β”‚   β”œβ”€β”€ mcp-client-architecture.md
β”‚   β”œβ”€β”€ mcp-implementation-plan.md
β”‚   └── provider-integration-checklist.md
β”œβ”€β”€ sql/                            # Database schemas
β”‚   β”œβ”€β”€ 01_schema.sql               # Core schema
β”‚   β”œβ”€β”€ 02_seed_data.sql            # Initial data
β”‚   β”œβ”€β”€ 03_multi_provider_schema.sql # Provider tables
β”‚   β”œβ”€β”€ 04_seed_providers.sql       # Provider configurations
β”‚   └── setup.sql                   # Master setup script
β”œβ”€β”€ tests/                          # Test suite
β”‚   β”œβ”€β”€ unit/                       # Unit tests
β”‚   β”œβ”€β”€ integration/                # Integration tests
β”‚   └── conftest.py                 # Test fixtures
β”œβ”€β”€ mcp_servers_config.json         # MCP server configurations
β”œβ”€β”€ requirements.fastapi.txt        # Backend dependencies
β”œβ”€β”€ requirements.test.txt           # Testing dependencies
β”œβ”€β”€ .env.example                    # Environment template
└── README.md                       # This file

Acknowledgements

About

FastAPI LLM Module with MCP Client support

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages