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

Function execute

packages/plugins/mcp/src/sdk/connection.ts:300–408  ·  view source on GitHub ↗
(url, init)

Source from the content-addressed store, hash-verified

298 staticOAuthBearer: boolean,
299): FetchLike => {
300 const execute: FetchLike = async (url, init) => {
301 const headers = headersFrom(init?.headers);
302 const requestWithoutBody = HttpClientRequest.make(httpMethodFrom(init?.method))(url, {
303 headers: recordFromHeaders(headers),
304 });
305 const request = await applyBody(requestWithoutBody, headers, init?.body);
306 const effect = Effect.gen(function* () {
307 const client = yield* HttpClient.HttpClient;
308 const response = yield* client.execute(request);
309 const responseHeaders = new Headers();
310 for (const [key, value] of Object.entries(response.headers)) {
311 if (value !== undefined) responseHeaders.set(key, value);
312 }
313 // Abort must reach the body, not just the pending request: this stream
314 // fiber outlives the `runPromise` below, so without this streamable
315 // http's SSE `GET` stays in flight after `close()`. Interrupted at the
316 // source because the SDK holds a locked reader on that same stream,
317 // which rules out cancelling the ReadableStream.
318 const stream =
319 init?.signal == null
320 ? response.stream
321 : Stream.interruptWhen(response.stream, awaitAbort(init.signal));
322 const body =
323 response.status === 204 || response.status === 205 || response.status === 304
324 ? null
325 : Stream.toReadableStream(stream);
326 return new Response(body, {
327 status: response.status,
328 headers: responseHeaders,
329 });
330 }).pipe(Effect.mapError(normalizeHttpClientFailure), Effect.provide(httpClientLayer));
331 // Executor resolves and refreshes OAuth credentials before constructing
332 // this transport. If that stored bearer is rejected, the MCP SDK cannot
333 // complete its interactive fallback in a catalog refresh and would perform
334 // avoidable DCR first. Stop at the authenticated HTTP boundary instead.
335 // A 403 carrying an RFC 6750 insufficient_scope challenge is intercepted
336 // HERE, below the SDK: with an authProvider the SDK would consume the
337 // challenge and re-run auth ("upscoping"), which our static-token
338 // provider can only answer by demanding reauthorization — misclassifying
339 // an unfixable scope shortfall as oauth_reauth_required. Thrown as the
340 // tagged error from the fetch adapter (a true runtime edge: the SDK
341 // consumes promise rejections) so it reaches the invoke/connect catch
342 // sites verbatim.
343 const promise = Effect.runPromise(effect).then(async (response) => {
344 let settled = response;
345 if (staticOAuthBearer && settled.status === 401) {
346 // One immediate replay before classifying — but only for requests the
347 // allowlist proves read-only (`isReplaySafeRequest`). A lone 401 on
348 // discovery/handshake traffic can be a transient upstream blip (a
349 // proxy hiccup, a racing key rotation on the server), and stamping
350 // reauthorization-required from a single sample forces a needless
351 // reconnect; the replay itself is possible because every body this
352 // adapter builds is buffered (`applyBody`), never a one-shot stream.
353 // A side-effectful method (`tools/call`) never replays: a 401 does
354 // not guarantee the server did no work first, so re-sending could
355 // execute the action twice. Its single 401 classifies directly —
356 // the invocation has already failed either way, and the
357 // transient-blip concern only justified the retry on read-only

Callers

nothing calls this directly

Calls 12

detectInsufficientScopeFunction · 0.90
headersFromFunction · 0.85
httpMethodFromFunction · 0.85
recordFromHeadersFunction · 0.85
applyBodyFunction · 0.85
awaitAbortFunction · 0.85
isReplaySafeRequestFunction · 0.85
abortErrorFunction · 0.85
rejectFunction · 0.85
executeMethod · 0.65
setMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected