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
27 Effect.map(DbService.asEffect(), ({ db }) => makeService(makeUserStore(db))),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 */
39export 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
93 Effect.acquireRelease(Effect.sync(makePostgresResource), (resource) => resource.close()),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 */
107export 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
16import { ApiKeyService } from "../auth/api-keys";16import { ApiKeyService } from "../auth/api-keys";
17import { BEARER_PREFIX } from "../auth/bearer";17import { BEARER_PREFIX } from "../auth/bearer";
18import { authorizeOrganization } from "../auth/organization";18import { authorizeOrganization } from "../auth/organization";
19import { UserStoreService } from "../auth/context";19import { makeUserStoreLayer } from "../auth/context";
20import { CoreSharedServices } from "../auth/workos";20import { CoreSharedServices } from "../auth/workos";
21import { DbService } from "../db/db";21import { makeDbLayer } from "../db/db";
22import { bearerChallenge } from "./responses";22import { bearerChallenge } from "./responses";
23import { McpJwtVerificationError, verifyWorkOSMcpAccessToken, type VerifiedToken } from "./jwt";23import { McpJwtVerificationError, verifyWorkOSMcpAccessToken, type VerifiedToken } from "./jwt";
24 24
@@ -150,15 +150,25 @@ const verifyJwt = (token: string) => · 125 unmodified lines
150 audience: WORKOS_CLIENT_ID,150 audience: WORKOS_CLIENT_ID,
151 });151 });
152 152
153const DbLive = DbService.Live;153// Built FRESH per `authorize` call. `McpOrganizationAuthLive` is constructed
154const UserStoreLive = UserStoreService.Live.pipe(Layer.provide(DbLive));154// once by the facade but `authorize` runs on every MCP request; a shared
155const McpOrganizationAuthServices = Layer.mergeAll(DbLive, UserStoreLive, CoreSharedServices);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.
161const makeMcpOrganizationAuthServices = () => {
162 const dbLive = makeDbLayer();
163 const userStoreLive = makeUserStoreLayer().pipe(Layer.provide(dbLive));
164 return Layer.mergeAll(dbLive, userStoreLive, CoreSharedServices);
165};
156 166
157export const McpOrganizationAuthLive = Layer.succeed(McpOrganizationAuth)({167export const McpOrganizationAuthLive = Layer.succeed(McpOrganizationAuth)({
158 authorize: (accountId, organizationId) =>168 authorize: (accountId, organizationId) =>
159 authorizeOrganization(accountId, organizationId).pipe(169 authorizeOrganization(accountId, organizationId).pipe(
160 Effect.map((org) => org !== null),170 Effect.map((org) => org !== null),
161 Effect.provide(McpOrganizationAuthServices),171 Effect.provide(makeMcpOrganizationAuthServices()),
162 ),172 ),
163});173});
164 174

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