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

Function execute

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

Source from the content-addressed store, hash-verified

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

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