A production-ready FastAPI backend featuring native Model Context Protocol (MCP) support, multi-provider LLM integration, PostgreSQL database, and comprehensive REST API endpoints.
- "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
- 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
- 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
- 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)
- 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
- Quick Start
- Architecture Overview
- MCP Integration Details
- Installation
- Configuration
- Running the Application
- API Documentation
- Testing
- Provider-Specific Features
- Extending the Platform
- Troubleshooting
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:5435Or 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.pyThat'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
- Get your API key: Check the logs or use the default from
.env - Test the API: Use the interactive docs at http://localhost:8000/docs
- Send a request: Try the
/chatendpoint with your API key - Try MCP tools: Send "Create a test file and read it back"
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
-
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
-
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
-
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
-
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" )
-
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
-
Tool Execution Flow
User Message β LLM decides tools needed β Execute tools β Feed results back β LLM continues β Repeat until done
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
}
}
}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 |
| Python functions | Check for function calls | β Manual loop | |
| Ollama | OpenAI-compatible | Always "stop", check calls |
β Manual loop |
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)
-
Clone the repository
git clone https://github.com/yourusername/fast-api-agents.git cd fast-api-agents -
Install Python dependencies
pip install -r requirements.fastapi.txt pip install -r requirements.test.txt # Optional, for testing -
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
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
-
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
# 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:11434Edit 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
# Start with Docker Compose
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down# Start the backend with auto-reload
python src/main.py# 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:8000Services will be available at:
- API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
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
}GET /mcp/status- Overall MCP integration statusGET /mcp/servers- List all MCP servers and connection statusGET /mcp/tools- List all available toolsPOST /mcp/servers/{server}/reconnect- Reconnect specific server
GET /providers- List all providers with capabilitiesGET /providers/{provider}/models- Get available modelsGET /providers/{provider}/health- Check provider health
Interactive documentation available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
# 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 mcptests/
βββ 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
- 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
- Models: GPT-4o, GPT-4o mini, o1 preview/mini
- Special Features: Parallel function calling, JSON mode
- Best For: General tasks, creative writing, reasoning
- Models: 2.5 Pro/Flash family with thinking capabilities
- Special Features: Built-in thinking, 1M context window
- Best For: Large context tasks, multimodal (planned)
- Models: Any Ollama-compatible model
- Special Features: Complete privacy, no API costs
- Best For: Development, sensitive data, offline use
-
Install the MCP server:
npm install -g @your-org/mcp-server-custom
-
Add configuration to
mcp_servers_config.json:{ "custom_server": { "transport_type": "stdio", "config": { "command": "mcp-server-custom", "args": ["--config", "/path/to/config"] }, "enabled": true } } -
Restart the application to auto-discover new tools
-
Create provider class inheriting from
BaseProvider -
Implement required methods:
chat_completion()chat_completion_stream()list_models()validate_config()
-
Register in
ProviderManager -
Add to database seed data
- 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.jsonsyntax
- Anthropic: Check API key and rate limits
- OpenAI: Verify model access and quotas
- Google: Ensure
google-genaipackage is installed - Ollama: Check if service is running (
ollama list)
- 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_versiontable
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
.
βββ 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
- Model Context Protocol by Anthropic
- FastAPI for the incredible framework
- All LLM providers for their amazing models