MCP FastAPI Agent
FastAPI agent server leveraging the Model Context Protocol (MCP) for seamless LM Studio integration. Features async request handling, real-time WebRTC through LiveKit, and a complete containerized architecture. Demonstrates advanced FastAPI patterns including dependency injection, background tasks, and scalable async operations.
Built With
- FastAPI
- MCP
- React
- LiveKit
- Docker
- PostgreSQL
Technical Breakdown
Model Context Protocol (MCP) provides the bridge between the FastAPI backend and LM Studio, enabling structured communication with local language models.
- Direct LM Studio Connection: MCP client connects to locally running LM Studio server.
- Structured Context Passing: Protocol handles context management and conversation state.
- Async Communication: Non-blocking integration with FastAPI's async request handling.
- Error Resilience: Connection retry logic and graceful degradation for model unavailability.
1# MCP integration in FastAPI
2from mcp import MCPClient
3from fastapi import FastAPI
4
5app = FastAPI()
6mcp_client = MCPClient(host="localhost", port=1234)
7
8@app.post("/chat")
9async def chat_endpoint(message: str):
10 response = await mcp_client.send_message(
11 message=message,
12 context=conversation_context
13 )
14 return {"response": response}
Evolved from custom STUN/TURN implementation to LiveKit for production-grade WebRTC communication and media processing.
- LiveKit Server: Local development server with full WebRTC capabilities.
- Media Streams: Bidirectional audio, video, and screen sharing through React components.
- Pipecat Integration: Foundation for advanced audio processing pipelines.
- Session Management: Persistent room state with automatic reconnection handling.
1# LiveKit development setup
2livekit-server --dev
3
4# Environment configuration
5LIVEKIT_API_KEY=devkey
6LIVEKIT_API_SECRET=secret
Full Docker orchestration with infrastructure separation and development-optimized workflows.
- Infrastructure Isolation: Separate compose file for database and supporting services.
- Development Workflow: Multi-terminal startup scripts for organized service management.
- PostgreSQL Integration: Persistent data layer with Alembic migrations.
- Host Integration: Proper Docker host networking for LM Studio connectivity.
1# Multi-service startup sequence
2# Terminal 1: LiveKit
3livekit-server --dev
4
5# Terminal 2: Database
6docker compose -f compose.infra.yml up --build
7
8# Terminal 3: Backend
9uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
10
11# Terminal 4: Frontend
12cd frontend && npm run dev
TypeScript React frontend with integrated WebRTC controls for comprehensive media interaction with the AI agent.
- Media Components: Custom React components for microphone, camera, and screen sharing.
- Real-Time Updates: Live state synchronization with backend through WebSocket connections.
- User Experience: Intuitive controls for managing multiple media streams simultaneously.
- Development Tools: Vite-powered development with hot reloading and TypeScript support.
PostgreSQL backend with proper schema management and development-to-production migration support.
- Alembic Migrations: Version-controlled database schema with auto-generated migration scripts.
- Docker Integration: Containerized PostgreSQL with persistent volumes.
- Development Safety: Separate infrastructure compose for clean development workflows.
- Connection Management: Proper database connection pooling and Docker host networking.
1# Database management
2alembic revision --autogenerate -m "Create initial tables"
3alembic upgrade head
4
5# Docker infrastructure
6docker compose -f compose.infra.yml up --build