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

Function execute

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

Source from the content-addressed store, hash-verified

129 httpClientLayer: Layer.Layer<HttpClient.HttpClient>,
130): FetchLike => {
131 const execute: FetchLike = async (url, init) => {
132 const headers = headersFrom(init?.headers);
133 const requestWithoutBody = HttpClientRequest.make(httpMethodFrom(init?.method))(url, {
134 headers: recordFromHeaders(headers),
135 });
136 const request = await applyBody(requestWithoutBody, headers, init?.body);
137 const effect = Effect.gen(function* () {
138 const client = yield* HttpClient.HttpClient;
139 const response = yield* client.execute(request);
140 const responseHeaders = new Headers();
141 for (const [key, value] of Object.entries(response.headers)) {
142 if (value !== undefined) responseHeaders.set(key, value);
143 }
144 const body =
145 response.status === 204 || response.status === 205 || response.status === 304
146 ? null
147 : Stream.toReadableStream(response.stream);
148 return new Response(body, {
149 status: response.status,
150 headers: responseHeaders,
151 });
152 }).pipe(Effect.provide(httpClientLayer));
153 // A 403 carrying an RFC 6750 insufficient_scope challenge is intercepted
154 // HERE, below the SDK: with an authProvider the SDK would consume the
155 // challenge and re-run auth ("upscoping"), which our static-token
156 // provider can only answer by demanding reauthorization — misclassifying
157 // an unfixable scope shortfall as oauth_reauth_required. Thrown as the
158 // tagged error from the fetch adapter (a true runtime edge: the SDK
159 // consumes promise rejections) so it reaches the invoke/connect catch
160 // sites verbatim.
161 const promise = Effect.runPromise(effect).then((response) => {
162 if (response.status === 403) {
163 const challenge = response.headers.get("www-authenticate");
164 if (
165 challenge !== null &&
166 detectInsufficientScope({ headers: { "www-authenticate": challenge } }) !== null
167 ) {
168 // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: Fetch-compatible adapter can only signal through a rejected promise
169 throw new McpInsufficientScopeError({
170 message:
171 "MCP server rejected the call: the OAuth grant does not cover the required scope",
172 });
173 }
174 }
175 return response;
176 });
177 // Mark the request promise observed (a no-op handler on the ORIGINAL
178 // promise; callers still see the rejection). The MCP SDK fires some
179 // requests without a rejection handler — a cancellation notification
180 // after a request timeout, an SSE dial raced against an abort — and when
181 // the upstream is already gone that rejection is unhandled, which kills
182 // the whole Bun server process, not just this call. Browsers never crash
183 // on an unobserved fetch rejection; this adapter must match.
184 // oxlint-disable-next-line executor/no-promise-catch -- boundary: Fetch-compatible adapter must observe rejections the SDK abandons
185 promise.catch(() => undefined);
186 if (!init?.signal) return promise;
187 // oxlint-disable-next-line executor/no-promise-reject -- boundary: Fetch-compatible adapter mirrors abort rejection semantics
188 if (init.signal.aborted) return Promise.reject(abortError(init.signal));

Callers

nothing calls this directly

Calls 10

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

Tested by

no test coverage detected