[recipes] Edge function cost optimization — 73% invocation reduction#188
Conversation
Cuts Supabase Edge Function invocations ~73% by combining three fixes: 1. Consolidate N MCP edge functions into 1 (each connector multiplies the 4-step MCP handshake; one connector + module-scope McpServer cuts startup invocations 4x for 4 extensions) 2. Add Mcp-Session-Id reuse so the initialize/notifications/tools-list handshake collapses into a single tools/call within a warm window 3. Cache hot read paths (thought_stats via new SQL aggregation RPC, query embeddings, list_vendors, get_follow_ups_due) with tag-based invalidation; new upsert_thought(text, jsonb, vector) RPC overload eliminates the separate UPDATE for embeddings Measured: 1.84M → ~440K invocations/month projected on a real OB1 deployment with the four canonical extensions. Includes: README walkthrough, before/after example snippets, metadata.json, and the SQL migrations for the two new RPCs (additive, non-breaking). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Hey @JustinTSmith — welcome to Open Brain Source! 👋 Thanks for submitting your first PR. The automated review will run shortly and check things like metadata, folder structure, and README completeness. If anything needs fixing, the review comment will tell you exactly what. Once the automated checks pass, a human admin will review for quality and clarity. Expect a response within a few days. If you have questions, check out CONTRIBUTING.md or open an issue. |
|
Thanks for the contribution, and welcome. This is a well-structured cost-optimization recipe — consolidating per-extension MCP functions into one, Worth noting it overlaps conceptually with #261, which fixes per-request — Alan (community reviewer; non-binding) |
Contribution Type
/recipes)/schemas)/dashboards)/integrations)/skills)What does this do?
Adds a recipe that cuts Supabase Edge Function invocations ~73% by combining three fixes the repo doesn't currently address: consolidating multiple per-extension MCP edge functions into a single function, adding
Mcp-Session-Idreuse so the 4-step MCP handshake collapses into a singletools/callwithin a warm window, and caching hot read paths (with a new SQL aggregation RPC that replaces a JS-side full-table scan inthought_stats).Measured impact on a real OB1 deployment running the four canonical extensions: 1,835,479 → ~440,000 invocations/month projected — comfortably under the 500K free-tier cap.
Why this matters
The repo's architecture (
CLAUDE.md: "MCP servers must be remote — Supabase Edge Functions, one per extension") is correct, but the defaultStreamableHTTPTransportfrom@hono/mcpis stateless and the repo has no recipe addressing the resulting invocation cost. Real users hitting the free-tier cap have no documented path forward besides upgrading. A search of issues, discussions, and existing recipes/primitives at the time of authoring turned up no prior work on this — see Substack/podcast archive, all open + closed issues, all existing recipes.The three fixes are independent and additive:
McpServer+ per-extensionregister(server)modules. 4 connectors → 1 connector cuts startup invocations 4×.Map<sid, Session>+Access-Control-Expose-Headers: mcp-session-idcollapses the initialize/notifications-initialized/tools-list handshake within a warm session. 1 user-visible tool call → 1 HTTP invocation on a warm session.thought_stats_summary()SQL RPC replaces a full-table scan with a single aggregation query. Newupsert_thought(text, jsonb, vector)overload writes content + embedding in one round-trip instead of two (the existing 2-arg signature is preserved).Requirements
thought_stats_summary, 3-argupsert_thought) inmigrations/20260417_edge_fn_optimizations.sql— both additive, no schema changesBuilds on:
primitives/deploy-edge-function— same deployment modelrecipes/content-fingerprint-dedup— the newupsert_thoughtoverload preserves fingerprint behaviorWhat's in the recipe
README.md— problem statement, the three fixes with rationale, measured impact, step-by-step migration guide, troubleshootingmetadata.json— schema-compliantexamples/before/per-request-server.ts— the per-request anti-pattern (annotated)examples/after/server.ts— the singleton McpServer patternexamples/after/index.ts— the session-reuse Hono handlerexamples/after/cache.ts— the TTL + tag-invalidation cacheexamples/after/410-stub.ts— a redirect stub for deprecated functionsmigrations/20260417_edge_fn_optimizations.sql— both new RPCsTest plan
metadata.jsonvalidates against.github/metadata.schema.json.tsfiles passdeno check(verified againstnpm:@hono/mcp@0.1.1+npm:@modelcontextprotocol/sdk@1.24.3+npm:hono@4.9.2)initializereturnsMcp-Session-Idheadertools/listwith reused session enumerates all 22 toolsthought_statsreturns aggregated results via the new SQL RPC (full table scan eliminated)list_vendors,search_recipes,get_follow_ups_due) works through the unified endpointChecklist
README.mdwith prerequisites, step-by-step instructions, and expected outcomemetadata.jsonhas all required fields🤖 Generated with Claude Code