Automatically converts FastAPI endpoints into MCP tools and resources, enabling semantic search, data ingestion (text, JSON, code, conversations), and knowledge graph operations through the ZepAI Memory Layer backend
Inherits Pydantic model validation from FastAPI endpoints for type-safe MCP tool schemas and data validation
Provides access to OpenAPI/Swagger documentation interface for testing and exploring the auto-generated MCP tools alongside the original FastAPI endpoints
FastMCP 2.0 Server - ZepAI Memory Layer
Auto-generated MCP server từ FastAPI backend sử dụng FastMCP 2.0
🏗️ Architecture
Server này sử dụng FastMCP.from_fastapi()
để tự động convert tất cả endpoints từ FastAPI app (memory_layer
) thành MCP tools và resources.
Key Components:
server_http.py
- Main MCP server file, auto-generates tools từ FastAPI endpointsmemory_layer/
- FastAPI backend (required dependency, not included in this repo)config.py
- Configuration settingstest/
- Test suite and examples
🚀 Features
Auto-generated MCP Tools:
All tools are automatically generated from FastAPI POST endpoints:
🔍 Search Tools:
search
- Semantic search với reranking strategiessearch_code
- Search code changes với metadata filters
📥 Ingest Tools:
ingest_text
- Ingest plain text vào knowledge graphingest_message
- Ingest conversation messagesingest_json
- Ingest structured JSON dataingest_code
- Ingest code changes với LLM importance scoringingest_code_context
- Ingest advanced code metadata với TTLingest_conversation
- Ingest full conversation context
📊 Admin Tools (Read-only):
Admin POST endpoints are filtered out for safety
Only GET endpoints are exposed as MCP Resources
Includes: stats, cache info, health checks
Auto-generated MCP Resources:
All GET endpoints with path parameters become Resource Templates:
📦 Installation
Prerequisites:
memory_layer FastAPI backend phải running tại
http://localhost:8000
Folder structure:
ZepAI/ ├── memory_layer/ # FastAPI backend (required) │ └── app/ │ └── main.py # Contains FastAPI app └── fastmcp_server/ # This repository ├── server_http.py ├── config.py └── requirements.txt
Install Dependencies:
⚙️ Configuration
Create .env
file (optional, có defaults):
🏃 Running the Server
1. Start memory_layer backend first:
2. Start MCP server:
Server will run on http://localhost:8002
📡 Available Endpoints
Combined FastAPI + MCP routes:
MCP Endpoints (at /mcp):
GET /mcp/sse
- Server-Sent Events connectionPOST /mcp/messages
- MCP message endpointMCP Client connection:
http://localhost:8002/mcp
Original FastAPI Routes:
GET /docs
- OpenAPI documentationGET /
- API root and health checkAll original endpoints from memory_layer
Key MCP Paths:
Tools list: Call via MCP client
Resources list: Call via MCP client
Test connection:
curl http://localhost:8002/mcp/sse
🧪 Testing
Run Test Suite:
Test suite includes:
Basic functionality tests
Tool calling tests
Resource reading tests
Search and ingest workflows
Comprehensive scenario tests
Using FastMCP Client:
Using curl:
📊 Comparison: FastMCP vs Custom Implementation
Aspect | Custom MCP | FastMCP 2.0 (Auto-generated) |
Lines of Code | ~2,900 | ~180 (94% reduction) |
Setup Time | 5 weeks | 1 day |
Tools Definition | Manual (11 tools) | Auto-generated from FastAPI |
Tools Registration | Manual (254 lines) | Automatic via
|
Validation | Manual Pydantic | Inherits from FastAPI |
Transport | Custom HTTP+SSE | Built-in HTTP/SSE |
Error Handling | Manual | Automatic |
Testing | Custom client | FastMCP Client + test suite |
Maintenance | Update 2 places | Update FastAPI only |
Deployment | Complex |
|
🔄 How It Works
Auto-conversion Process:
Route Mapping Rules:
POST/PUT/DELETE → MCP Tools (writable operations)
GET with → MCP Resource Templates (dynamic data)
GET without params → MCP Resources (static data)
Admin POST endpoints → Filtered out (safety)
Benefits:
✅ Single source of truth - Update FastAPI, MCP updates automatically
✅ No code duplication - Tools inherit FastAPI validation
✅ Type safety - Pydantic models from FastAPI = MCP schemas
✅ Zero maintenance - Add new FastAPI endpoint = new MCP tool automatically
✅ Combined access - Use via MCP client OR direct HTTP/OpenAPI
🎯 Key Design Decisions
1. Why Auto-generation?
DRY principle - FastAPI already defines all endpoints, schemas, validation
Zero maintenance - No manual tool registration needed
Type safety - Inherits Pydantic validation from FastAPI
2. Why Filter Admin Endpoints?
Safety - Prevent accidental cache clearing via MCP client
Read-only monitoring - Admin GET endpoints still exposed as resources
Explicit control - Destructive operations require direct API access
3. Why Combined Routes?
Flexibility - Access via MCP client OR OpenAPI/Swagger
Debugging - Use
/docs
for quick endpoint testingMigration path - Existing API clients continue working
4. File Structure:
📖 Documentation
🎯 Benefits of This Approach
✅ 94% less code - 180 lines vs 2,900 lines
✅ Zero tool registration - Auto-generated from FastAPI
✅ Single source of truth - Update FastAPI once
✅ Type-safe - Inherits Pydantic validation
✅ Dual access - MCP client OR OpenAPI/Swagger
✅ Easy testing - Built-in test utilities + /docs
✅ Safe by default - Admin operations filtered
✅ Future-proof - New FastAPI endpoints = new MCP tools automatically
🔗 Links
📝 License
Same as original project.
Note: This server requires the memory_layer
FastAPI backend to be running. The MCP server acts as a protocol adapter, exposing FastAPI endpoints as MCP tools and resources.
This server cannot be installed
remote-capable server
The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.
Auto-converts FastAPI endpoints into MCP tools for semantic search, data ingestion (text, code, conversations), and knowledge graph operations. Provides dual access through MCP protocol and direct API calls with automatic tool generation from FastAPI routes.