MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / makeCloudMcpAgentHandler

Function makeCloudMcpAgentHandler

apps/cloud/src/mcp/agent-handler.ts:232–396  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

230 });
231
232export const makeCloudMcpAgentHandler = () => {
233 const serveOptions = {
234 binding: "MCP_SESSION",
235 transport: "streamable-http",
236 } as const;
237 // The agents SDK builds an exact-match `URLPattern` from the path handed to
238 // `serve` (see `createStreamingHttpHandler` in `agents/dist/mcp/index.js`) —
239 // a single `/mcp` handler never matches `/mcp/toolkits/<slug>` and falls
240 // through to its own internal 404. A second `serve` mounted on the
241 // parameterized path picks it up (`URLPattern` supports `:slug` segments);
242 // the auth/ownership/props logic above is unchanged and shared, only the
243 // final dispatch target differs.
244 const serve = McpSessionDOSqlite.serve("/mcp", serveOptions);
245 const serveToolkit = McpSessionDOSqlite.serve("/mcp/toolkits/:slug", serveOptions);
246
247 const ALLOWED_METHODS = new Set(["GET", "POST", "DELETE", "OPTIONS"]);
248
249 return async (request: Request, env: Env, ctx: ExecutionContext): Promise<Response> => {
250 if (request.method === "OPTIONS") return corsPreflightResponse();
251 // The old envelope (packages/hosts/mcp/src/envelope.ts) answered anything
252 // outside GET/POST/DELETE/OPTIONS with a JSON-RPC 405; the agents SDK
253 // handler only understands its own transport verbs and falls through to
254 // a bare 404. Reject before authenticating so PUT/PATCH/etc never reach
255 // the session engine.
256 if (!ALLOWED_METHODS.has(request.method)) {
257 return jsonRpcResponse(405, -32001, "Method not allowed");
258 }
259 const sessionId = request.headers.get("mcp-session-id");
260
261 const { auth, outcome } = await runTraced(request, env, authenticate(request));
262 if (!Predicate.isTagged(outcome, "Authenticated")) {
263 // Destroying a live session on auth grounds requires a POSITIVE
264 // determination that access is genuinely gone — only `Forbidden` carries
265 // that (valid bearer, org absent/revoked). `Unavailable` (transient WorkOS
266 // / JWKS failure) and `Unauthorized` (retry with a fresh token) must leave
267 // the session intact, so the condemn path is gated on `Forbidden` alone.
268 if (Predicate.isTagged(outcome, "Forbidden") && sessionId) {
269 await Effect.runPromise(
270 Effect.ignore(
271 Effect.tryPromise(() =>
272 mcpSessionStub(env.MCP_SESSION, sessionId)._cf_scheduleDestroy(),
273 ),
274 ),
275 );
276 }
277 return renderAuthError(auth, request, outcome);
278 }
279
280 if (!sessionId && request.method === "DELETE") {
281 // Matches the old envelope's contract (@modelcontextprotocol/sdk's
282 // `WebStandardStreamableHTTPServerTransport.handleDeleteRequest`): 200,
283 // not 204 — see e2e/cloud/mcp-protocol.test.ts.
284 return new Response(null, {
285 status: 200,
286 headers: { "access-control-allow-origin": "*" },
287 });
288 }
289

Callers 1

server.tsFile · 0.90

Calls 15

mcpSessionStubFunction · 0.90
wrapMcpSseResponseFunction · 0.90
runTracedFunction · 0.85
resourceFromPathFunction · 0.85
jsonRpcErrorBodyFunction · 0.85
corsPreflightResponseFunction · 0.70
jsonRpcResponseFunction · 0.70

Tested by

no test coverage detected