import { McpCorsOptions, McpTransportArmorOptions, SupabaseMcpAccessOptions, SupabaseMcpClientPolicy, SupabaseMcpContext, SupabaseMcpDurableStateOptions, SupabaseMcpErrorEvent, SupabaseMcpLifecycleEvent, SupabaseMcpProfilesOptions, SupabaseMcpResultMiddleware, SupabaseMcpRunCorrelation, SupabaseMcpServer, SupabaseMcpSurfaceProof } from "./types.js"; import { Implementation, McpServer, ServerEventBus } from "@modelcontextprotocol/server"; import { BaseContext, Middleware, NoConflict, SingleKeyEntry } from "@supabase/middleware"; import { SupabaseClient } from "@supabase/supabase-js"; import { SupabaseContext } from "@supabase/server"; //#region src/middleware.d.ts /** * Required upstream context keys contributed by `@supabase/server`'s * `withSupabase(...)`. Used as `withChumbo`'s `In` prerequisite so placing * `withChumbo` before `withSupabase` fails at compile time, while optional * keys such as `oauthProtectedResource` remain truly optional in `pipeline`. */ type ChumboUpstreamPrerequisites = Pick, "supabase" | "userClaims" | "jwtClaims" | "authMode">; /** * Full upstream context read by `withChumbo` when running downstream of * `withSupabase(...)` and optionally `withOAuthProtectedResource()`. */ interface ChumboUpstreamSupabaseContext extends BaseContext, ChumboUpstreamPrerequisites { readonly supabaseAdmin?: SupabaseClient; readonly authKeyName?: string; readonly oauthProtectedResource?: { readonly resourceMetadataUrl: string; }; } type SupabaseMcpPipelineAccessOptions = SupabaseMcpAccessOptions; /** * Contribution attached at `ctx.chumbo` by `withChumbo(...)`. * Extends `SupabaseMcpContext` with `instrument(server)` so an * existing official `new McpServer(...)` block gains `withScopes`, * `resultMiddleware`, `onEvent`, `runCorrelation`, and lazy per-family * `listChanged: false` hardening without replacing `index.ts`. */ interface SupabaseMcpPipelineContext extends SupabaseMcpContext { /** * Wrap an official `@modelcontextprotocol/server` `McpServer` instance with * Chumbo's scope proxy (`server.withScopes`), `resultMiddleware` runner, * `onEvent` / `runCorrelation` lifecycle emitter, and lazy per-family * `{ listChanged: false }` / `{ subscribe: false }` seeding. * * - Idempotent if called again on an already-instrumented `McpServer`. * - Throws `TypeError` if `tools`, `resources`, or `prompts` were already * registered on `server` prior to `instrument(server)`. */ instrument(server: McpServer): SupabaseMcpServer; } interface WithChumboOptions { /** Server identity stamped onto lifecycle events and surface proofs. */ readonly server: Implementation; /** * Canonical public MCP URL. When omitted, derived from * `ctx.oauthProtectedResource.resourceMetadataUrl` (stripping both RFC 9728 * origin-prefix and path-suffix forms) or from the Edge Function / request URL. */ readonly resourceUrl?: string | URL; /** Optional stable strategy name override for `ctx.chumbo.authentication.strategy`. */ readonly strategy?: string; /** Optional Postgres-backed durable state (`ctx.chumbo.state` & `ctx.chumbo.sessionState`). */ readonly state?: SupabaseMcpDurableStateOptions; /** Optional explicit signed run correlation (`ctx.chumbo.run` & lifecycle v2). */ readonly runCorrelation?: SupabaseMcpRunCorrelation; /** Optional `client_id` allowlist, required scopes, and `resolveScopes` resolver. */ readonly access?: SupabaseMcpPipelineAccessOptions; /** Optional multi-audience tool profile catalog & selector. */ readonly profiles?: SupabaseMcpProfilesOptions; /** Client capability heuristics and fallback policies. */ readonly clientPolicy?: SupabaseMcpClientPolicy; /** Result composition middleware applied to tools registered on `ctx.chumbo.instrument(server)`. */ readonly resultMiddleware?: SupabaseMcpResultMiddleware | readonly SupabaseMcpResultMiddleware[]; /** Transport armor & session ID behavior. Enabled by default (`subscriptions: "disabled"`, `sessionId: "stateless"`). */ readonly transport?: McpTransportArmorOptions; /** Lifecycle telemetry sink (`capability.started`, `capability.finished`). */ onEvent?(event: SupabaseMcpLifecycleEvent): void | Promise; /** Canonical redacted `tools/list` surface proof sink. */ onSurface?(proof: SupabaseMcpSurfaceProof): void | Promise; /** Non-throwing operator error hook. */ onError?(event: SupabaseMcpErrorEvent): void; } interface McpCorsContribution { readonly origin: string | null; readonly allowed: boolean; } interface McpTransportContribution { readonly sessionId: string | null; readonly protocolVersion?: string; } /** * Strip both RFC 9728 origin-prefix (`/.well-known/oauth-protected-resource/`) * and path-suffix (`//oauth-protected-resource` or `.../.well-known/oauth-protected-resource`) * forms to recover the canonical MCP resource URL. */ declare function deriveResourceUrlFromMetadataUrl(metadataUrl: string): string; /** * Pre-auth pipeline entry for browser MCP hosts. Place FIRST in `pipeline([...])` * (ahead of `withOAuthProtectedResource()` and `withSupabase({ cors: 'disabled' })`) * so `OPTIONS` preflights succeed without credentials and `401` OAuth challenges * expose `WWW-Authenticate` and `Mcp-Session-Id` to browser `fetch()`. */ declare const withMcpCors: Middleware<"mcpCors", McpCorsOptions, Record, McpCorsContribution>; /** * Standalone transport-armor pipeline entry for non-Chumbo MCP handlers that * only want immediate `405` / `-32601` SSE listen-stream refusal and stateless * `Mcp-Session-Id` issuance without `withChumbo`. */ declare const withMcpTransportArmor: Middleware<"mcpTransport", McpTransportArmorOptions | undefined, Record, McpTransportContribution>; interface WithChumboFunction { & BaseContext = ChumboUpstreamPrerequisites & BaseContext>(options: WithChumboOptions, handler: NoConflict<"chumbo", Base, (req: Request, ctx: NoInfer & { chumbo: SupabaseMcpPipelineContext; }) => Promise>): (req: Request, ctx: Base) => Promise; (options: WithChumboOptions): SingleKeyEntry<"chumbo", ChumboUpstreamPrerequisites, SupabaseMcpPipelineContext>; } /** * Primary Chumbo pipeline entry for `@supabase/server` MCP servers. * Place immediately AFTER `withSupabase(...)` in `pipeline([...], handler)`. */ declare const withChumbo: WithChumboFunction; interface ChumboMcpHandlerOptions { readonly server: Implementation; readonly instructions?: string | ((context: SupabaseMcpPipelineContext) => string | undefined | Promise); readonly protocol?: { readonly legacy?: "stateless" | "reject"; readonly responseMode?: "auto" | "json" | "sse"; readonly subscriptions?: "disabled" | "in-memory" | ServerEventBus; }; onError?(event: SupabaseMcpErrorEvent): void; register(server: SupabaseMcpServer, context: SupabaseMcpPipelineContext): void | Promise; } /** * Reusable terminal handler for `pipeline([...withChumbo(...)], chumboMcpHandler(...))`. * Instantiates `createMcpHandler` once at module initialization rather than per * request, resolves `instructions`, instruments `McpServer`, and calls `register`. */ declare function chumboMcpHandler(options: ChumboMcpHandlerOptions): (req: Request, ctx: { readonly chumbo: SupabaseMcpPipelineContext; }) => Promise; //#endregion export { ChumboMcpHandlerOptions, ChumboUpstreamPrerequisites, ChumboUpstreamSupabaseContext, McpCorsContribution, McpTransportContribution, SupabaseMcpPipelineAccessOptions, SupabaseMcpPipelineContext, WithChumboFunction, WithChumboOptions, chumboMcpHandler, deriveResourceUrlFromMetadataUrl, withChumbo, withMcpCors, withMcpTransportArmor }; //# sourceMappingURL=middleware.d.ts.map