Final Refactoring Plan: Evolving the FastAPI Backend into an MCP-Compliant Service
This plan outlines the process for refactoring the existing Python backend to implement the full architecture specified in docs/
, leveraging the fastmcp
library to ensure MCP compliance.
Target Architecture (Python/FastAPI + fastmcp)
The architecture remains the same, but our implementation will be significantly simplified and standardized by using the fastmcp
library to handle the MCP communication layer.
Loading diagram...
Execution Phases
-
Phase 1: Backend Restructuring
- Restructure the backend from a single script into a modular Python project.
- Create a
requirements.txt
file and add all necessary dependencies:fastapi
,uvicorn
,fastmcp
,langchain
,redis
,supabase-py
, and any others needed for data scraping and processing. - Organize code into logical modules (
database.py
,cache.py
,llm.py
,services.py
,main.py
).
-
Phase 2: Implement MCP Server with
fastmcp
- In
main.py
, use thefastmcp
library to initialize the MCP server and create the/mcp
router. - Use decorators provided by
fastmcp
(e.g.,@mcp_server.tool
) to expose thebuild_profile
andgenerate_context
functions as MCP tools.
- In
-
Phase 3: Implement Core Business Logic
- Following the detailed steps in
docs/03_build_profile_step_by_step.md
anddocs/04_generate_context_flow.md
, implement the complete Python logic for both tools. This includes the full data ingestion pipeline and the user-facing context generation flow.
- Following the detailed steps in
-
Phase 4: Frontend Adaptation & Final Configuration
- Update the frontend (
app/playground/page.tsx
andlib/api-clients.ts
) to useuse_mcp_tool
. - Guide you through setting up the necessary environment variables for the FastAPI server (API keys, etc.).
- Update your
mcp_settings.json
file to register the FastAPI server as a remote MCP server, pointing tohttp://localhost:8000/mcp
.
- Update the frontend (
This represents our finalized, agreed-upon plan. Are you ready to proceed with the implementation?