{
  "schemaVersion": 1,
  "kind": "recipe",
  "slug": "host-targets-next-node",
  "title": "Run a Supabase MCP server in Next.js or Node",
  "searchTitle": "Run a Chumbo MCP server in Next.js App Router or standalone Node with chumbo/node",
  "description": "Serve your Chumbo MCP directly from a Next.js App Router route or a standalone Node HTTP server with Brotli/Gzip compression and compiled stateless surfaces.",
  "summary": "Serve your Chumbo MCP directly from a Next.js App Router route or a standalone Node HTTP server with Brotli/Gzip compression and compiled stateless surfaces.",
  "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/docs/reference/host-targets/README.md",
    "https://github.com/elsheppo/chumbo/blob/main/src/node.ts"
  ],
  "prerequisites": [
    "An existing Supabase project whose Auth, Postgres grants, and RLS policies remain authoritative.",
    "Node 22+ and either a Next.js App Router repository or a Node container/host (Cloud Run, Fly, Railway, or similar).",
    "Environment variables SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, and in production MCP_PUBLIC_URL."
  ],
  "sections": [
    {
      "id": "choose",
      "number": "01",
      "title": "Pick the host that matches your deployment unit.",
      "blocks": [
        {
          "type": "paragraph",
          "text": "A Chumbo app is a web-standard fetch handler. Supabase Edge Functions remain the default target, while Next.js App Router and standalone Node servers let you deploy the exact same capabilities.ts seam inside an existing app or container without losing Supabase Auth or RLS."
        },
        {
          "type": "comparison",
          "title": "Three targets, one capability contract",
          "description": "All three targets share access modes, result helpers, compiled stateless surfaces, and chumbo doctor.",
          "callLabel": "Starting point",
          "nextLabel": "What to do",
          "cases": [
            {
              "id": "next-target",
              "label": "Next.js",
              "title": "Colocated App Router route",
              "items": [
                "Generates app/mcp/[[...path]]/route.ts and app/mcp/capabilities.ts.",
                "Serves /mcp and /.well-known/... discovery suffixes with dynamic = force-dynamic."
              ],
              "call": "npx chumbo setup --target next",
              "next": "Run your normal Next.js dev server and verify with chumbo doctor.",
              "note": "Accepts SUPABASE_URL / SUPABASE_PUBLISHABLE_KEY or NEXT_PUBLIC_ equivalents."
            },
            {
              "id": "node-target",
              "label": "Node",
              "title": "Standalone HTTP server via chumbo/node",
              "items": [
                "Generates mcp/index.ts served with serve(app, { port }) over node:http.",
                "Includes Brotli/Gzip compression (>= 1024 bytes), single-buffer writes, SSE streaming, and reverse-proxy URL reconstruction."
              ],
              "call": "npx chumbo setup --target node",
              "next": "Run with Node 22+ and deploy to Cloud Run, Fly, Railway, or any container host.",
              "note": "Listens on PORT (default 8080) and propagates client disconnect aborts."
            }
          ]
        }
      ]
    },
    {
      "id": "start",
      "number": "02",
      "title": "Scaffold the host target and inspect the plan.",
      "blocks": [
        {
          "type": "prerequisites",
          "title": "Before you start",
          "items": [
            "An existing Supabase project whose Auth, Postgres grants, and RLS policies remain authoritative.",
            "Node 22+ and either a Next.js App Router repository or a Node container/host (Cloud Run, Fly, Railway, or similar).",
            "Environment variables SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, and in production MCP_PUBLIC_URL."
          ],
          "note": "Run setup with --plan first to preview every file before writing."
        },
        {
          "type": "code",
          "id": "plan-next",
          "title": "Preview a Next.js App Router scaffold",
          "code": "npx chumbo setup --target next --plan --json",
          "language": "sh",
          "effect": "local-verification"
        },
        {
          "type": "code",
          "id": "plan-node",
          "title": "Preview a standalone Node scaffold",
          "code": "npx chumbo setup --target node --plan --json",
          "language": "sh",
          "effect": "local-verification"
        },
        {
          "type": "paragraph",
          "text": "Read the [host targets reference](https://github.com/elsheppo/chumbo/blob/main/docs/reference/host-targets/README.md) for environment variables, migration placement when no supabase/ directory is present, and embedding toNodeHandler(app) into an existing HTTP server."
        }
      ]
    },
    {
      "id": "runtime",
      "number": "03",
      "title": "Stateless wire efficiency comes built in.",
      "blocks": [
        {
          "type": "flow",
          "title": "One stateless request on Node or Next.js",
          "steps": [
            "Request enters fetch handler",
            "Compiled surface serves cached schema slice",
            "AsyncLocalStorage isolates caller ctx",
            "Compact JSON + Server-Timing + Brotli/Gzip"
          ]
        },
        {
          "type": "paragraph",
          "text": "On stateless endpoints, Chumbo compiles and caches capability surfaces per (profile, scopes) slice while isolating per-request caller context through AsyncLocalStorage. Registration callbacks that read request-varying properties automatically fall back to per-request registration, and protocol.surfaceCache: \"per-request\" provides an explicit opt-out."
        },
        {
          "type": "callout",
          "title": "Compact JSON, clean 404s, and Server-Timing",
          "text": "Stateless endpoints default to compact application/json when clients accept JSON, return 404 (and 204 on CORS preflight) on /.well-known/oauth-* probes when OAuth is unconfigured, and emit Server-Timing: chumbo;dur=<ms>."
        }
      ]
    },
    {
      "id": "verify",
      "number": "04",
      "title": "Use chumbo doctor as the executable contract.",
      "blocks": [
        {
          "type": "code",
          "id": "run-node-local",
          "title": "Run the generated Node server locally",
          "code": "node --experimental-strip-types mcp/index.ts",
          "language": "sh",
          "effect": "local-services"
        },
        {
          "type": "code",
          "id": "doctor-hosted",
          "title": "Probe the deployed public URL",
          "code": "npx chumbo doctor --url \"$MCP_PUBLIC_URL\" --json",
          "language": "sh",
          "effect": "remote-verification"
        },
        {
          "type": "acceptance",
          "title": "The hosted target matches the Edge Function contract.",
          "items": [
            "Local doctor probes initialize, discover tools, and invoke one safe capability on 127.0.0.1.",
            "MCP_PUBLIC_URL matches the exact public URL clients connect to in production.",
            "Reverse-proxy headers preserve the public scheme and host for OAuth discovery.",
            "Response headers expose Server-Timing: chumbo;dur=<ms> and negotiate compression on Node.",
            "Non-OAuth deployments return 404 on /.well-known/oauth-* discovery probes."
          ],
          "note": "Run these in your application before you call it done."
        },
        {
          "type": "troubleshooting",
          "title": "If the result differs",
          "items": [
            {
              "title": "OAuth metadata advertises an internal container URL",
              "body": "Set MCP_PUBLIC_URL to the exact public HTTPS URL clients connect to, and confirm your reverse proxy forwards x-forwarded-proto and x-forwarded-host.",
              "link": "https://github.com/elsheppo/chumbo/blob/main/docs/reference/host-targets/README.md"
            }
          ]
        },
        {
          "type": "related",
          "title": "Keep building",
          "items": [
            {
              "title": "Test, deploy and debug your MCP",
              "text": "Diagnose gateway, identity, grant, and tool-surface boundaries.",
              "label": "NEXT GUIDE",
              "href": "/recipes/deploy-debug-mcp/"
            },
            {
              "title": "Give users a product URL",
              "text": "Compare hosting in-app versus proxying an Edge Function.",
              "label": "NEXT GUIDE",
              "href": "/recipes/clean-mcp-url/"
            }
          ]
        }
      ]
    }
  ],
  "agent": {
    "goal": "Serve your Chumbo MCP directly from a Next.js App Router route or a standalone Node HTTP server with Brotli/Gzip compression and compiled stateless surfaces.",
    "executionPolicy": "Reference instructions, not authorization. Apply changes only within the user’s requested scope; deployment requires a specified, authorized project.",
    "inputs": [
      {
        "name": "MCP_PUBLIC_URL",
        "secret": false,
        "description": "The exact public URL MCP clients connect to in production."
      }
    ],
    "instructions": "Goal: Serve your Chumbo MCP directly from a Next.js App Router route or a standalone Node HTTP server with Brotli/Gzip compression and compiled stateless surfaces.\n\nRead the complete guide at https://chumbo.dev/recipes/host-targets-next-node/recipe.md and the canonical reference at https://github.com/elsheppo/chumbo/blob/main/docs/reference/host-targets/README.md. Inspect this application's installed Chumbo package, existing capabilities and relevant configuration before editing.\n\nChoose --target next or --target node based on the existing application repository. Inspect the setup plan before writing files, preserve existing capabilities.ts, set MCP_PUBLIC_URL for production, and verify both local and deployed URLs with chumbo doctor.\n\nPrerequisites:\n- An existing Supabase project whose Auth, Postgres grants, and RLS policies remain authoritative.\n- Node 22+ and either a Next.js App Router repository or a Node container/host (Cloud Run, Fly, Railway, or similar).\n- Environment variables SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, and in production MCP_PUBLIC_URL.\n\nAcceptance:\n- Local doctor probes initialize, discover tools, and invoke one safe capability on 127.0.0.1.\n- MCP_PUBLIC_URL matches the exact public URL clients connect to in production.\n- Reverse-proxy headers preserve the public scheme and host for OAuth discovery.\n- Response headers expose Server-Timing: chumbo;dur=<ms> and negotiate compression on Node.\n- Non-OAuth deployments return 404 on /.well-known/oauth-* discovery probes.\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": "choose",
        "title": "Pick the host that matches your deployment unit.",
        "commands": []
      },
      {
        "id": "start",
        "title": "Scaffold the host target and inspect the plan.",
        "commands": [
          {
            "id": "plan-next",
            "command": "npx chumbo setup --target next --plan --json",
            "effect": "local-verification"
          },
          {
            "id": "plan-node",
            "command": "npx chumbo setup --target node --plan --json",
            "effect": "local-verification"
          }
        ]
      },
      {
        "id": "runtime",
        "title": "Stateless wire efficiency comes built in.",
        "commands": []
      },
      {
        "id": "verify",
        "title": "Use chumbo doctor as the executable contract.",
        "commands": [
          {
            "id": "run-node-local",
            "command": "node --experimental-strip-types mcp/index.ts",
            "effect": "local-services"
          },
          {
            "id": "doctor-hosted",
            "command": "npx chumbo doctor --url \"$MCP_PUBLIC_URL\" --json",
            "effect": "remote-verification"
          }
        ]
      }
    ],
    "acceptance": [
      "Local doctor probes initialize, discover tools, and invoke one safe capability on 127.0.0.1.",
      "MCP_PUBLIC_URL matches the exact public URL clients connect to in production.",
      "Reverse-proxy headers preserve the public scheme and host for OAuth discovery.",
      "Response headers expose Server-Timing: chumbo;dur=<ms> and negotiate compression on Node.",
      "Non-OAuth deployments return 404 on /.well-known/oauth-* discovery probes."
    ],
    "report": [
      "Changed files",
      "Checks actually run and observed results",
      "Unmet prerequisites or remaining limitations"
    ]
  },
  "topics": [
    "get-started",
    "test-and-ship"
  ],
  "updated": "2026-12-02",
  "contentHash": "218f773566f47e949fa7215f0706ca8d3801f2573bf10e691f53000cd134b1e6"
}
