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

Function makeCloudMcpAgentHandler

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

Source from the content-addressed store, hash-verified

197 });
198
199export const makeCloudMcpAgentHandler = () => {
200 const serveOptions = {
201 binding: "MCP_SESSION",
202 transport: "streamable-http",
203 } as const;
204 // The agents SDK builds an exact-match `URLPattern` from the path handed to
205 // `serve` (see `createStreamingHttpHandler` in `agents/dist/mcp/index.js`) —
206 // a single `/mcp` handler never matches `/mcp/toolkits/<slug>` and falls
207 // through to its own internal 404. A second `serve` mounted on the
208 // parameterized path picks it up (`URLPattern` supports `:slug` segments);
209 // the auth/ownership/props logic above is unchanged and shared, only the
210 // final dispatch target differs.
211 const serve = McpSessionDOSqlite.serve("/mcp", serveOptions);
212 const serveToolkit = McpSessionDOSqlite.serve("/mcp/toolkits/:slug", serveOptions);
213
214 const ALLOWED_METHODS = new Set(["GET", "POST", "DELETE", "OPTIONS"]);
215
216 return async (request: Request, env: Env, ctx: ExecutionContext): Promise<Response> => {
217 if (request.method === "OPTIONS") return corsPreflightResponse();
218 // The old envelope (packages/hosts/mcp/src/envelope.ts) answered anything
219 // outside GET/POST/DELETE/OPTIONS with a JSON-RPC 405; the agents SDK
220 // handler only understands its own transport verbs and falls through to
221 // a bare 404. Reject before authenticating so PUT/PATCH/etc never reach
222 // the session engine.
223 if (!ALLOWED_METHODS.has(request.method)) {
224 return jsonRpcResponse(405, -32001, "Method not allowed");
225 }
226 const sessionId = request.headers.get("mcp-session-id");
227
228 const { auth, outcome } = await runTraced(request, authenticate(request));
229 if (!Predicate.isTagged(outcome, "Authenticated")) {
230 // Destroying a live session on auth grounds requires a POSITIVE
231 // determination that access is genuinely gone — only `Forbidden` carries
232 // that (valid bearer, org absent/revoked). `Unavailable` (transient WorkOS
233 // / JWKS failure) and `Unauthorized` (retry with a fresh token) must leave
234 // the session intact, so the condemn path is gated on `Forbidden` alone.
235 if (Predicate.isTagged(outcome, "Forbidden") && sessionId) {
236 await Effect.runPromise(
237 Effect.ignore(
238 Effect.tryPromise(() =>
239 mcpSessionStub(env.MCP_SESSION, sessionId)._cf_scheduleDestroy(),
240 ),
241 ),
242 );
243 }
244 return renderAuthError(auth, request, outcome);
245 }
246
247 if (!sessionId && request.method === "DELETE") {
248 // Matches the old envelope's contract (@modelcontextprotocol/sdk's
249 // `WebStandardStreamableHTTPServerTransport.handleDeleteRequest`): 200,
250 // not 204 — see e2e/cloud/mcp-protocol.test.ts.
251 return new Response(null, {
252 status: 200,
253 headers: { "access-control-allow-origin": "*" },
254 });
255 }
256

Callers 1

server.tsFile · 0.90

Calls 15

mcpSessionStubFunction · 0.90
wrapMcpSseResponseFunction · 0.90
runTracedFunction · 0.85
resourceFromPathFunction · 0.85
withOrgWriteAccessFunction · 0.85
jsonRpcErrorBodyFunction · 0.85

Tested by

no test coverage detected