History

Fix MCP org-auth reusing a DB connection across requests (#887)

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.

Rhys Sullivan committed 5 months ago

Commit bec7b7b1 parent 56292b03 files · +44 −6Browse files

Unified · Split

3 files
  1. apps/cloud/src/auth/context.ts +13 −0
  2. apps/cloud/src/db/db.ts +15 −0
  3. apps/cloud/src/mcp/auth.ts +16 −6
apps/cloud/src/auth/context.ts+13−0
@@ -27,3 +27,16 @@ export class UserStoreService extends Context.Service<UserStoreService, UserStor · 26 unmodified lines
2727 Effect.map(DbService.asEffect(), ({ db }) => makeService(makeUserStore(db))),
2828 );
2929 }
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+ );
apps/cloud/src/db/db.ts+15−0
@@ -93,3 +93,18 @@ export class DbService extends Context.Service<DbService, DbServiceShape>()( · 92 unmodified lines
9393 Effect.acquireRelease(Effect.sync(makePostgresResource), (resource) => resource.close()),
9494 );
9595 }
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+ );
apps/cloud/src/mcp/auth.ts+16−6
@@ -16,9 +16,9 @@ import { createCachedRemoteJWKSet } from "../auth/jwks-cache"; · 15 unmodified lines
1616 import { ApiKeyService } from "../auth/api-keys";
1717 import { BEARER_PREFIX } from "../auth/bearer";
1818 import { authorizeOrganization } from "../auth/organization";
19−import { UserStoreService } from "../auth/context";
19+import { makeUserStoreLayer } from "../auth/context";
2020 import { CoreSharedServices } from "../auth/workos";
21−import { DbService } from "../db/db";
21+import { makeDbLayer } from "../db/db";
2222 import { bearerChallenge } from "./responses";
2323 import { McpJwtVerificationError, verifyWorkOSMcpAccessToken, type VerifiedToken } from "./jwt";
2424
@@ -150,15 +150,25 @@ const verifyJwt = (token: string) => · 125 unmodified lines
150150 audience: WORKOS_CLIENT_ID,
151151 });
152152
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+};
156166
157167 export const McpOrganizationAuthLive = Layer.succeed(McpOrganizationAuth)({
158168 authorize: (accountId, organizationId) =>
159169 authorizeOrganization(accountId, organizationId).pipe(
160170 Effect.map((org) => org !== null),
161− Effect.provide(McpOrganizationAuthServices),
171+ Effect.provide(makeMcpOrganizationAuthServices()),
162172 ),
163173 });
164174

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t