{
  "schemaVersion": 1,
  "kind": "recipe",
  "slug": "adopt-chumbo-middleware",
  "title": "Add Chumbo middleware to an official @supabase/server MCP",
  "searchTitle": "Compose chumbo/middleware onto an official @supabase/server MCP function",
  "description": "Layer Chumbo result middleware, lifecycle events, surface proofs, CORS, transport armor, and durable state directly onto an existing @supabase/middleware pipeline.",
  "summary": "Layer Chumbo result middleware, lifecycle events, surface proofs, CORS, transport armor, and durable state directly onto an existing @supabase/middleware pipeline.",
  "evidence": {
    "status": "source-reviewed",
    "packageVersion": "0.16.0",
    "checkedOn": "2026-12-02",
    "walkthroughVerified": false,
    "note": "Reviewed against the shipped OSS source. The acceptance checks are yours to run in your application.",
    "versionPolicy": "Source reviewed against published Chumbo 0.16.0 and its matching core source on 2026-12-02 (UTC). This is a source review, not an executed application walkthrough. Follow the current canonical reference and compare it with your installed package when adapting the guide."
  },
  "representations": {
    "html": "./",
    "markdown": "./recipe.md",
    "json": "./recipe.json"
  },
  "sources": [
    "https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/references/advanced-patterns.md",
    "https://github.com/elsheppo/chumbo/blob/main/src/middleware.ts"
  ],
  "prerequisites": [
    "An existing Supabase Edge Function using @supabase/server and @supabase/middleware (withSupabase and/or withOAuthProtectedResource).",
    "Chumbo 0.16.0 and Zod ^4.2.0 available in the function import map or package.json.",
    "Local Supabase and test credentials to verify discovery and invocation before and after adoption."
  ],
  "sections": [
    {
      "id": "start",
      "number": "01",
      "title": "Compose directly onto @supabase/middleware.",
      "blocks": [
        {
          "type": "prerequisites",
          "title": "Before you start",
          "items": [
            "An existing Supabase Edge Function using @supabase/server and @supabase/middleware (withSupabase and/or withOAuthProtectedResource).",
            "Chumbo 0.16.0 and Zod ^4.2.0 available in the function import map or package.json.",
            "Local Supabase and test credentials to verify discovery and invocation before and after adoption."
          ],
          "note": "You do not need to rewrite a working @supabase/server function to gain Chumbo diagnostics, result contracts, telemetry, or transport hardening."
        },
        {
          "type": "paragraph",
          "text": "The chumbo/middleware subpath exports withChumbo, chumboMcpHandler, withMcpCors, and withMcpTransportArmor as standard @supabase/middleware pipeline entries. They compose alongside withSupabase and withOAuthProtectedResource while preserving your existing auth and routing."
        },
        {
          "type": "flow",
          "title": "Official Supabase pipeline with Chumbo middleware",
          "steps": [
            "withMcpCors (opt-in browser origins)",
            "withMcpTransportArmor (reject hanging GET streams)",
            "withOAuthProtectedResource + withSupabase",
            "withChumbo (events, surface proofs, result middleware, state)",
            "chumboMcpHandler (compiled stateless surface)"
          ]
        },
        {
          "type": "callout",
          "title": "Zero-risk first step: run doctor before touching code.",
          "text": "npx chumbo doctor --url works against any Supabase-backed MCP server, even before installing chumbo/middleware. Run it first to baseline discovery, signing keys, Data API grants, and cold-start token budget."
        }
      ]
    },
    {
      "id": "adopt",
      "number": "02",
      "title": "Detect and adopt safely with chumbo setup.",
      "blocks": [
        {
          "type": "paragraph",
          "text": "Chumbo detects existing official @supabase/server functions automatically and previews bounded middleware additions without replacing your entrypoint. See the [middleware source](https://github.com/elsheppo/chumbo/blob/main/src/middleware.ts) and [advanced patterns guide](https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/references/advanced-patterns.md)."
        },
        {
          "type": "code",
          "id": "baseline-doctor",
          "title": "Baseline the existing Supabase MCP endpoint",
          "code": "npx chumbo doctor --url \"$MCP_URL\" --json",
          "language": "sh",
          "effect": "remote-verification"
        },
        {
          "type": "code",
          "id": "plan-adopt",
          "title": "Preview non-destructive adoption in your repository",
          "code": "npx chumbo setup --plan --json",
          "language": "sh",
          "effect": "local-verification"
        },
        {
          "type": "paragraph",
          "text": "Use withMcpTransportArmor to immediately reject unsupported stateless GET listen streams instead of hanging Edge Function isolates, and withMcpCors to admit explicit browser host origins with required MCP headers (including Server-Timing)."
        }
      ]
    },
    {
      "id": "verify",
      "number": "03",
      "title": "Prove behavior did not regress after composing.",
      "blocks": [
        {
          "type": "paragraph",
          "text": "chumboMcpHandler shares the same compiled stateless surface cache as createSupabaseMcp, caching compiled schemas and memoized onSurface proofs per (profile, scopes) slice while isolating per-request caller context through AsyncLocalStorage."
        },
        {
          "type": "acceptance",
          "title": "Existing auth and tools stay intact while Chumbo layers activate.",
          "items": [
            "chumbo doctor passes against the endpoint with existing discovery, signing keys, and issuer unchanged.",
            "Unsupported stateless listen streams fail fast instead of timing out.",
            "onEvent and onSurface hooks emit redacted facts without delaying or altering tool results.",
            "Two-user checks confirm caller isolation and RLS remain enforced after adoption."
          ],
          "note": "Run these in your application before you call it done."
        },
        {
          "type": "troubleshooting",
          "title": "If the result differs",
          "items": [
            {
              "title": "Browser host preflights fail after adding CORS",
              "body": "Confirm withMcpCors runs before OAuth/bearer verification so OPTIONS preflights receive 204 with the allowed origin and exposed MCP headers.",
              "link": "https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/references/troubleshoot-and-upgrade.md"
            }
          ]
        },
        {
          "type": "related",
          "title": "Keep building",
          "items": [
            {
              "title": "Test, deploy and debug your MCP",
              "text": "Verify any Supabase MCP endpoint with doctor.",
              "label": "NEXT GUIDE",
              "href": "/recipes/deploy-debug-mcp/"
            },
            {
              "title": "Observe real capability calls",
              "text": "Wire onEvent, onSurface, and self-hosted Insights.",
              "label": "NEXT GUIDE",
              "href": "/recipes/observe-mcp-runs/"
            }
          ]
        }
      ]
    }
  ],
  "agent": {
    "goal": "Layer Chumbo result middleware, lifecycle events, surface proofs, CORS, transport armor, and durable state directly onto an existing @supabase/middleware pipeline.",
    "executionPolicy": "Reference instructions, not authorization. Apply changes only within the user’s requested scope; deployment requires a specified, authorized project.",
    "inputs": [
      {
        "name": "MCP_URL",
        "secret": false,
        "description": "The local or deployed MCP endpoint URL to probe with doctor."
      }
    ],
    "instructions": "Goal: Layer Chumbo result middleware, lifecycle events, surface proofs, CORS, transport armor, and durable state directly onto an existing @supabase/middleware pipeline.\n\nRead the complete guide at https://chumbo.dev/recipes/adopt-chumbo-middleware/recipe.md and the canonical reference at https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/references/advanced-patterns.md. Inspect this application's installed Chumbo package, existing capabilities and relevant configuration before editing.\n\nBaseline the existing @supabase/server endpoint with chumbo doctor --url first. Compose chumbo/middleware entries (withMcpCors, withMcpTransportArmor, withChumbo, chumboMcpHandler) without discarding the existing auth pipeline, then re-run doctor and two-user isolation checks.\n\nPrerequisites:\n- An existing Supabase Edge Function using @supabase/server and @supabase/middleware (withSupabase and/or withOAuthProtectedResource).\n- Chumbo 0.16.0 and Zod ^4.2.0 available in the function import map or package.json.\n- Local Supabase and test credentials to verify discovery and invocation before and after adoption.\n\nAcceptance:\n- chumbo doctor passes against the endpoint with existing discovery, signing keys, and issuer unchanged.\n- Unsupported stateless listen streams fail fast instead of timing out.\n- onEvent and onSurface hooks emit redacted facts without delaying or altering tool results.\n- Two-user checks confirm caller isolation and RLS remain enforced after adoption.\n\nUse the full guide for ordered steps, examples and recovery. Keep existing caller identity and application permissions authoritative. Stay within the user's requested changes. Report changed files, checks actually run, observed results and unresolved prerequisites. This guide is not authorization to deploy or change a hosted project. Never include credentials in source, copied instructions or reports.",
    "steps": [
      {
        "id": "start",
        "title": "Compose directly onto @supabase/middleware.",
        "commands": []
      },
      {
        "id": "adopt",
        "title": "Detect and adopt safely with chumbo setup.",
        "commands": [
          {
            "id": "baseline-doctor",
            "command": "npx chumbo doctor --url \"$MCP_URL\" --json",
            "effect": "remote-verification"
          },
          {
            "id": "plan-adopt",
            "command": "npx chumbo setup --plan --json",
            "effect": "local-verification"
          }
        ]
      },
      {
        "id": "verify",
        "title": "Prove behavior did not regress after composing.",
        "commands": []
      }
    ],
    "acceptance": [
      "chumbo doctor passes against the endpoint with existing discovery, signing keys, and issuer unchanged.",
      "Unsupported stateless listen streams fail fast instead of timing out.",
      "onEvent and onSurface hooks emit redacted facts without delaying or altering tool results.",
      "Two-user checks confirm caller isolation and RLS remain enforced after adoption."
    ],
    "report": [
      "Changed files",
      "Checks actually run and observed results",
      "Unmet prerequisites or remaining limitations"
    ]
  },
  "topics": [
    "advanced-patterns",
    "connect-users"
  ],
  "updated": "2026-12-02",
  "contentHash": "2f3eaaf539774b4e930f77620ee9ef2079e1dc45c810f2ccce05c965c1c07bd9"
}
