fix: rework lifecycle management to optimize cloud deployment by phernandez · Pull Request #320 · basicmachines-co/basic-memory · GitHub
Skip to content

fix: rework lifecycle management to optimize cloud deployment - #320

Merged
phernandez merged 1 commit into
mainfrom
api-mcp-lifecycle
Sep 28, 2025
Merged

fix: rework lifecycle management to optimize cloud deployment#320
phernandez merged 1 commit into
mainfrom
api-mcp-lifecycle

Conversation

@phernandez

Copy link
Copy Markdown
Member

Summary

This PR reworks the lifecycle management architecture to solve critical cloud deployment performance issues while maintaining proper initialization for different deployment modes.

Problems Solved

1. MCP Recycling Overhead in Cloud

Issue: In cloud deployments, MCP gets recycled for every request, causing the app_lifespan to run repeatedly

  • initialize_app() running on every request
  • File sync setup happening repeatedly
  • Migration checks on every request
  • Massive performance overhead

Solution: Removed all lifecycle complexity from MCP server

  • MCP server is now just a plain FastMCP instance
  • No per-request initialization overhead
  • Much faster response times in cloud proxy scenarios

2. FastAPI Not Loading Projects

Issue: FastAPI app wasn't properly calling initialize_app()

  • Projects from config weren't being loaded
  • Missing database initialization for web context
  • Broken project reconciliation

Solution: FastAPI explicitly handles full lifecycle

  • Added initialize_app(app_config) call in FastAPI lifespan
  • Proper project loading when running as web API
  • Database and project setup happens once on startup

Architecture Changes

MCP CLI Command (mcp.py)

  • Now handles initialization before starting MCP
  • Calls initialize_file_sync() to set up file watching
  • Starts sync in background thread, then runs clean MCP server

MCP Server (server.py)

  • Significantly simplified - removed app_lifespan completely
  • No background task management
  • Just a pure FastMCP instance

FastAPI App (app.py)

  • Enhanced lifecycle management for web API mode
  • Explicit initialize_app() call ensures project loading
  • Clear separation: lifecycle only runs when needed

Deployment Mode Optimization

MCP CLI Mode:

  • Initialize once → Run lightweight MCP server
  • No per-request overhead

FastAPI Mode:

  • Full lifecycle management with project loading
  • Proper database and project setup

Cloud Proxy Mode:

  • FastAPI handles initialization
  • MCP requests are now lightweight and fast

Performance Impact

  • Before: Every MCP request triggered full initialization cycle
  • After: MCP requests have minimal overhead
  • Cloud performance: Should see significant improvement in response times
  • Resource usage: Reduced CPU and memory overhead per request

Testing

  • Verified MCP CLI mode still works correctly
  • Confirmed FastAPI mode properly loads projects
  • Architecture supports both local and cloud deployments

This change should resolve the cloud performance issues identified in recent deployments.

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

- MCP CLI initializes file sync then starts clean MCP server
- Removed lifecycle complexity from MCP server to prevent recycling overhead
- FastAPI app handles full lifecycle when running as web API
- Fixes cloud performance issues where MCP recycling caused repeated initialization
- Ensures FastAPI properly loads projects from config

Performance impact:
- Eliminates MCP request overhead in cloud proxy scenarios
- Faster response times by removing per-request initialization
- Proper project loading in FastAPI web context

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: phernandez <paul@basicmachines.co>
@github-actions

github-actions Bot commented Sep 28, 2025

Copy link
Copy Markdown
Contributor

@phernandez
phernandez merged commit ea2e93d into main Sep 28, 2025
12 checks passed
@phernandez
phernandez deleted the api-mcp-lifecycle branch September 28, 2025 20:11
@phernandez phernandez added this to the v0.15.0 milestone Oct 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant