| 611 | * (`RMcpAuth = never`, cloud), the provide is a harmless no-op. |
| 612 | */ |
| 613 | const buildMcpRoutes = <RMcpAuth>( |
| 614 | mcp: McpProviders<RMcpAuth>, |
| 615 | identity: Layer.Layer<IdentityProvider>, |
| 616 | ): Layer.Layer<never, never, HttpRouter.HttpRouter> => { |
| 617 | // The auth seam may declare `IdentityProvider` as a requirement (self-host's |
| 618 | // genuinely reads it; cloud's ignores it — its MCP JWT/api-key path is separate). |
| 619 | // Either way the identity seam is provided ONCE (memoized) and shared across the |
| 620 | // build-time provide + the per-request `provideRequest`. The provided |
| 621 | // `IdentityProvider` covers `RMcpAuth` whether it is `IdentityProvider` or `never`. |
| 622 | const mcpAuthLive = (mcp.auth as Layer.Layer<McpAuthProvider, never, IdentityProvider>).pipe( |
| 623 | Layer.provide(identity), |
| 624 | ); |
| 625 | // No session store: the host serves `/mcp` transport elsewhere (the Cloudflare |
| 626 | // Agent bridge), so mount only the auth-declared discovery routes. The discovery |
| 627 | // handlers are captured from the auth seam at build, so no per-request seams. |
| 628 | if (!mcp.sessions) { |
| 629 | return McpDiscoveryRoutes.pipe(Layer.provide(mcpAuthLive)); |
| 630 | } |
| 631 | const mcpSeams = Layer.mergeAll(mcpAuthLive, mcp.sessions, mcp.reporter ?? McpErrorReporterNoop); |
| 632 | return McpServingRoutes.pipe(HttpRouter.provideRequest(mcpSeams), Layer.provide(mcpAuthLive)); |
| 633 | }; |
| 634 | |
| 635 | // Re-exported so a strategy author building the `config.failure` value can name |
| 636 | // the `Principal` the strategy renders failures around. |