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

  1. 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).
  2. Phase 2: Implement MCP Server with fastmcp

    • In main.py, use the fastmcp library to initialize the MCP server and create the /mcp router.
    • Use decorators provided by fastmcp (e.g., @mcp_server.tool) to expose the build_profile and generate_context functions as MCP tools.
  3. Phase 3: Implement Core Business Logic

    • Following the detailed steps in docs/03_build_profile_step_by_step.md and docs/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.
  4. Phase 4: Frontend Adaptation & Final Configuration

    • Update the frontend (app/playground/page.tsx and lib/api-clients.ts) to use use_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 to http://localhost:8000/mcp.

This represents our finalized, agreed-upon plan. Are you ready to proceed with the implementation?