⚡ Bolt: Fix N+1 query in MCP OAuth token injection - #159
Conversation
Extracted token fetching from the `Promise.all` loop when resolving MCP servers in `register-hooks.ts`. Leverages Drizzle's `inArray` to fetch all relevant tokens for a batch of servers at once. Uses a compound key to correctly map both shared and per-user tokens in memory.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What
Extracted the MCP OAuth token fetch operation from the
Promise.allloop when enriching MCP server records. It now pre-fetches all required tokens using a batchedinArrayDrizzle query and maps them in memory using a compound key (${server_id}:${user_id ?? 'shared'}).🎯 Why
Previously, when the
/mcp-serversendpoint returned multiple configured servers, the FeathersJSafterhook would iterate through each server and query the database sequentially for OAuth tokens, resulting in a severe N+1 query performance bottleneck as the server count increased.📊 Impact
Reduces database roundtrips for
/mcp-serversreads fromO(N)(whereNis the number of returned MCP servers) down toO(1). This speeds up dashboard loading and token checks dramatically when many integrations are configured.🔬 Measurement
Verify by loading the MCP servers tab in the UI or performing a large
findagainst themcp-serversservice. Monitor the SQLite query logs to observe only oneSELECT * FROM user_mcp_oauth_tokens WHERE mcp_server_id IN (...)instead of numerous single-record lookups.PR created automatically by Jules for task 17972844638617198844 started by @Donach