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

Function makeCloudMcpAgentHandler

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

Source from the content-addressed store, hash-verified

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

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