Application Structure
The backend is a FastAPI application (app/main.py) with async/await throughout. All I/O operations (database, Neo4j, HTTP calls) are non-blocking.
Application Lifecycle
Thelifespan context manager in app/main.py handles startup and shutdown:
Startup Sequence
Startup Sequence
- Run Alembic migrations - Apply any pending database schema changes
- Create PostgreSQL tables - Ensure all SQLAlchemy models are present
- Initialize Neo4j driver - Connect to knowledge graph
- Create Neo4j schema - Set up constraints and indexes
- Backfill repos - Scan connected repos not yet in Neo4j (background task)
Shutdown Sequence
Shutdown Sequence
- Dispose PostgreSQL engine - Close all database connections
- Close Neo4j driver - Gracefully disconnect from graph
API Routes
Authentication Routes
GET /auth/github
Starts GitHub OAuth flow. Redirects to
github.com/login/oauth/authorize with client_id and scope=repo,read:org.GET /auth/github/callback
OAuth callback endpoint. Exchanges authorization code for access token, creates/updates user, sets JWT cookie, redirects to frontend.
GET /auth/me
Returns current user profile (requires JWT cookie).
POST /auth/logout
Clears auth cookie.
Webhook Routes
POST /api/v1/webhooks/github
Per-repo webhook receiver
- Verify HMAC-SHA256 signature against
webhook_secret - Deduplicate (ignore duplicate events within 1 hour)
- Create
Eventrow withstatus=pending - Return
HTTP 200immediately (< 1 second) - Process PR in background via
BackgroundTask
Repository Routes
GET /api/v1/repos
Lists all GitHub repos accessible to the user with connection status.
POST /api/v1/repos/{owner}/{repo}/install
Connects a repository:
- Create
Installationrecord - Install webhook (per-repo secret)
- Scan file tree → Neo4j graph (background task)
POST /api/v1/repos/{owner}/{repo}/rescan
Re-scans repository file tree and rebuilds Neo4j graph. Useful after repo restructure.
DELETE /api/v1/repos/{owner}/{repo}/install
Disconnects repository:
- Remove GitHub webhook
- Mark
Installationas inactive - (Optional) Delete Neo4j nodes/edges
Review Routes
GET /api/v1/reviews
Returns PR review history with filters:
repo: Filter by repositorystatus: Filter by status (completed, failed, pending)limit: Max results (default 20)
Analytics Routes
GET /api/v1/analytics
Returns team metrics:
- Total PRs reviewed
- Verdict distribution (approve/request_changes/needs_discussion)
- Top contributors
- Recent review timeline
Memory Routes
GET /api/v1/memory
Lists Mem0 memories for a repository.
POST /api/v1/memory
Manually adds a project rule or developer pattern to Mem0.
DELETE /api/v1/memory/{id}
Removes a memory from Mem0.
GET /api/v1/memory/project-map
Returns aggregated project context summary (all memories).
Middleware Stack
CORS Middleware
allow_credentials=True is required because the frontend sends JWT cookies with withCredentials: true in axios.Request Logging Middleware
MCP Server Mount
Why
mount() instead of include_router()?FastMCP’s sse_app() returns a Starlette ASGI application, not an APIRouter. Mounting allows the MCP server to handle its own routing for SSE (GET /mcp/sse) and JSON-RPC (POST /mcp/messages).Health Check Endpoint
Example Response
Example Response
Environment Configuration
All settings are managed via Pydantic inapp/core/config.py:
All MCP integration URLs are optional. If not set, Nectr gracefully skips that integration and logs an info message.
Next Steps
Service Layer
Deep dive into PR review, AI, and context services
Data Flow
Follow a webhook event through the entire system
Database Schema
PostgreSQL tables and relationships
Neo4j Graph
Knowledge graph schema and queries