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

Function mcpDispatch

packages/hosts/mcp/src/envelope.ts:200–259  ·  view source on GitHub ↗
(resource: McpResource)

Source from the content-addressed store, hash-verified

198
199/** Dispatch an MCP request through authenticate -> store.dispatch -> transport. */
200const mcpDispatch = (resource: McpResource) =>
201 Effect.gen(function* () {
202 const httpRequest = yield* HttpServerRequest.HttpServerRequest;
203 const auth = yield* McpAuthProvider;
204 const store = yield* McpSessionStore;
205 const request = yield* toWebRequest(httpRequest);
206
207 // CORS preflight: answer before auth so unauthenticated clients can probe.
208 if (request.method === "OPTIONS") {
209 return HttpServerResponse.raw(corsPreflightResponse());
210 }
211
212 // Streamable-HTTP only defines GET/POST/DELETE on the endpoint. Any other
213 // method (PUT/PATCH/…) is rejected with a JSON-RPC 405 BEFORE auth/dispatch —
214 // otherwise it would fall through and spin up a session engine for a method
215 // the transport can't serve.
216 if (!ALLOWED_MCP_METHODS.has(request.method)) {
217 return HttpServerResponse.raw(jsonRpcResponse(405, -32001, "Method not allowed"));
218 }
219
220 const sessionId = request.headers.get("mcp-session-id");
221
222 // Authenticate (and, for session-aware providers, authorize) on EVERY
223 // request. Non-authenticated outcomes render directly. Session teardown is
224 // only safe after the store can validate the authenticated principal and MCP
225 // resource; an auth-level Forbidden may not carry either.
226 const outcome = yield* auth.authenticate(request);
227 if (!Predicate.isTagged(outcome, "Authenticated")) {
228 return HttpServerResponse.raw(renderAuthError(auth, request, outcome));
229 }
230 const principal = outcome.principal;
231
232 // No session id: per the streamable-HTTP transport contract, only POST opens
233 // a session. A GET needs an existing id (400); a DELETE on nothing is a
234 // no-op (204). Both short-circuit BEFORE dispatch so the store never spins up
235 // an engine for a bare GET/DELETE.
236 if (!sessionId) {
237 if (request.method === "GET") {
238 return HttpServerResponse.raw(
239 jsonRpcResponse(400, -32000, "mcp-session-id header required for SSE"),
240 );
241 }
242 if (request.method === "DELETE") {
243 return HttpServerResponse.raw(
244 new Response(null, { status: 204, headers: { "access-control-allow-origin": "*" } }),
245 );
246 }
247 }
248
249 const result: McpDispatchResult = yield* store.dispatch({
250 request,
251 principal,
252 resource,
253 sessionId,
254 method: request.method,
255 });
256 return HttpServerResponse.raw(
257 result instanceof Response ? result : renderDispatchError(result),

Callers 1

mcpRouteFunction · 0.85

Calls 6

toWebRequestFunction · 0.85
renderDispatchErrorFunction · 0.85
corsPreflightResponseFunction · 0.70
jsonRpcResponseFunction · 0.70
renderAuthErrorFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected