---
schema_version: 1
slug: adopt-chumbo-middleware
status: source-reviewed
walkthrough_verified: false
content_hash: 2f3eaaf539774b4e930f77620ee9ef2079e1dc45c810f2ccce05c965c1c07bd9
---

# Compose chumbo/middleware onto an official @supabase/server MCP function

Layer Chumbo result middleware, lifecycle events, surface proofs, CORS, transport armor, and durable state directly onto an existing @supabase/middleware pipeline.

Reviewed against the shipped OSS source. The acceptance checks are yours to run in your application.

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.

[Structured recipe](./recipe.json) · [Human-readable page](./)

## Sources

- [Canonical pattern](https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/references/advanced-patterns.md)
- [Reference implementation](https://github.com/elsheppo/chumbo/blob/main/src/middleware.ts)

## 01. Compose directly onto @supabase/middleware.

### Before you start

- 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.

You do not need to rewrite a working @supabase/server function to gain Chumbo diagnostics, result contracts, telemetry, or transport hardening.

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.

**Official Supabase pipeline with Chumbo middleware:** withMcpCors (opt-in browser origins) → withMcpTransportArmor (reject hanging GET streams) → withOAuthProtectedResource + withSupabase → withChumbo (events, surface proofs, result middleware, state) → chumboMcpHandler (compiled stateless surface)

> **Zero-risk first step: run doctor before touching code.**
>
> 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.

## 02. Detect and adopt safely with chumbo setup.

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).

### Baseline the existing Supabase MCP endpoint

```sh
npx chumbo doctor --url "$MCP_URL" --json
```

### Preview non-destructive adoption in your repository

```sh
npx chumbo setup --plan --json
```

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).

## 03. Prove behavior did not regress after composing.

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.

### Existing auth and tools stay intact while Chumbo layers activate.

- 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.

Run these in your application before you call it done.

### If the result differs

#### Browser host preflights fail after adding CORS

Confirm withMcpCors runs before OAuth/bearer verification so OPTIONS preflights receive 204 with the allowed origin and exposed MCP headers.

[Read the reference](https://github.com/elsheppo/chumbo/blob/main/skills/chumbo/references/troubleshoot-and-upgrade.md)

### Keep building

- [Test, deploy and debug your MCP](/recipes/deploy-debug-mcp/): Verify any Supabase MCP endpoint with doctor.
- [Observe real capability calls](/recipes/observe-mcp-runs/): Wire onEvent, onSurface, and self-hosted Insights.

## Instructions for your coding agent

Reference instructions, not authorization. Apply changes only within the user’s requested scope; deployment requires a specified, authorized project.

Goal: Layer Chumbo result middleware, lifecycle events, surface proofs, CORS, transport armor, and durable state directly onto an existing @supabase/middleware pipeline.

Read 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.

Baseline 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.

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.

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.

Use 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.

### Required environment inputs

| Variable | Sensitive | Meaning |
| --- | --- | --- |
| `MCP_URL` | No | The local or deployed MCP endpoint URL to probe with doctor. |

### Handoff report

- Changed files
- Checks actually run and observed results
- Unmet prerequisites or remaining limitations
