McpOrganizationAuthLive is built once by the facade but its authorize runs on every MCP request. It provided a module-const services layer (DbService.Live + UserStoreService.Live); the long-lived runtime's memo map memoizes those const layers once, so the postgres socket opened on the first request is reused on later ones — illegal under Cloudflare's per-request I/O ("Cannot perform I/O on behalf of a different request"). The org lookup then fails on every follow-up, so the session connects but tools/list 403s ("connected, tools fetch failed"). Build the DB + UserStore layers fresh per authorize call (distinct layer values bypass memoization) so each request gets its own request-scoped socket.
Unified · Split
| @@ -27,3 +27,16 @@ export class UserStoreService extends Context.Service<UserStoreService, UserStor · 26 unmodified lines | ||
| 27 | 27 | Effect.map(DbService.asEffect(), ({ db }) => makeService(makeUserStore(db))), |
| 28 | 28 | ); |
| 29 | 29 | } |
| 30 | + | |
| 31 | +/** | |
| 32 | + * A FRESH `UserStoreService` layer (new layer value per call). `UserStoreService.Live` | |
| 33 | + * captures its `db` at build time; when a long-lived runtime's shared memo map | |
| 34 | + * memoizes that const layer once, it pins the first request's postgres socket and | |
| 35 | + * reuses it on later requests — illegal under Cloudflare's per-request I/O. Build | |
| 36 | + * this (over a fresh `DbService` layer) anywhere a service is constructed once but | |
| 37 | + * invoked across many requests (the MCP org-authorization seam). See [[makeDbLayer]]. | |
| 38 | + */ | |
| 39 | +export const makeUserStoreLayer = (): Layer.Layer<UserStoreService, never, DbService> => | |
| 40 | + Layer.effect(UserStoreService)( | |
| 41 | + Effect.map(DbService.asEffect(), ({ db }) => makeService(makeUserStore(db))), | |
| 42 | + ); | |
| @@ -93,3 +93,18 @@ export class DbService extends Context.Service<DbService, DbServiceShape>()( · 92 unmodified lines | ||
| 93 | 93 | Effect.acquireRelease(Effect.sync(makePostgresResource), (resource) => resource.close()), |
| 94 | 94 | ); |
| 95 | 95 | } |
| 96 | + | |
| 97 | +/** | |
| 98 | + * A FRESH `DbService` layer (a new layer value on every call). Provide this | |
| 99 | + * — rather than the shared `DbService.Live` — anywhere a service is built ONCE | |
| 100 | + * by the facade but invoked across many Workers requests (e.g. the MCP | |
| 101 | + * org-authorization seam). A single shared `DbService.Live` opens its postgres | |
| 102 | + * socket on the first request and reuses it on later ones, which Cloudflare | |
| 103 | + * forbids ("Cannot perform I/O on behalf of a different request"). A distinct | |
| 104 | + * layer value per call gets its own request-scoped socket, acquired and | |
| 105 | + * released within that request. | |
| 106 | + */ | |
| 107 | +export const makeDbLayer = (): Layer.Layer<DbService> => | |
| 108 | + Layer.effect(DbService)( | |
| 109 | + Effect.acquireRelease(Effect.sync(makePostgresResource), (resource) => resource.close()), | |
| 110 | + ); | |
| @@ -16,9 +16,9 @@ import { createCachedRemoteJWKSet } from "../auth/jwks-cache"; · 15 unmodified lines | ||
| 16 | 16 | import { ApiKeyService } from "../auth/api-keys"; |
| 17 | 17 | import { BEARER_PREFIX } from "../auth/bearer"; |
| 18 | 18 | import { authorizeOrganization } from "../auth/organization"; |
| 19 | −import { UserStoreService } from "../auth/context"; | |
| 19 | +import { makeUserStoreLayer } from "../auth/context"; | |
| 20 | 20 | import { CoreSharedServices } from "../auth/workos"; |
| 21 | −import { DbService } from "../db/db"; | |
| 21 | +import { makeDbLayer } from "../db/db"; | |
| 22 | 22 | import { bearerChallenge } from "./responses"; |
| 23 | 23 | import { McpJwtVerificationError, verifyWorkOSMcpAccessToken, type VerifiedToken } from "./jwt"; |
| 24 | 24 | |
| @@ -150,15 +150,25 @@ const verifyJwt = (token: string) => · 125 unmodified lines | ||
| 150 | 150 | audience: WORKOS_CLIENT_ID, |
| 151 | 151 | }); |
| 152 | 152 | |
| 153 | −const DbLive = DbService.Live; | |
| 154 | −const UserStoreLive = UserStoreService.Live.pipe(Layer.provide(DbLive)); | |
| 155 | −const McpOrganizationAuthServices = Layer.mergeAll(DbLive, UserStoreLive, CoreSharedServices); | |
| 153 | +// Built FRESH per `authorize` call. `McpOrganizationAuthLive` is constructed | |
| 154 | +// once by the facade but `authorize` runs on every MCP request; a shared | |
| 155 | +// `DbService.Live` would open its postgres socket on the first request and | |
| 156 | +// illegally reuse it on later ones ("Cannot perform I/O on behalf of a | |
| 157 | +// different request"), failing the org lookup on every follow-up — the | |
| 158 | +// "connected · tools fetch failed" symptom. A fresh DB + UserStore layer per | |
| 159 | +// call gives each request its own request-scoped socket. `CoreSharedServices` | |
| 160 | +// (WorkOS, no per-request socket) stays shared. | |
| 161 | +const makeMcpOrganizationAuthServices = () => { | |
| 162 | + const dbLive = makeDbLayer(); | |
| 163 | + const userStoreLive = makeUserStoreLayer().pipe(Layer.provide(dbLive)); | |
| 164 | + return Layer.mergeAll(dbLive, userStoreLive, CoreSharedServices); | |
| 165 | +}; | |
| 156 | 166 | |
| 157 | 167 | export const McpOrganizationAuthLive = Layer.succeed(McpOrganizationAuth)({ |
| 158 | 168 | authorize: (accountId, organizationId) => |
| 159 | 169 | authorizeOrganization(accountId, organizationId).pipe( |
| 160 | 170 | Effect.map((org) => org !== null), |
| 161 | − Effect.provide(McpOrganizationAuthServices), | |
| 171 | + Effect.provide(makeMcpOrganizationAuthServices()), | |
| 162 | 172 | ), |
| 163 | 173 | }); |
| 164 | 174 | |