feat: add authenticated integration request MCP tool (#6835)

This commit is contained in:
Thomas authored and GitHub committed 2026-09-13 11:05:23 +02:00
1 parent c32a5317ca
commit c39a8854aa
10 files changed
+573 -6

No files matched your search

@@ -1,6 +1,7 @@
import { lookup } from "node:dns/promises";
import { BlockList, isIP } from "node:net";
import type { LookupFunction } from "node:net";
import type { ConnectionOptions } from "node:tls";
import { Agent } from "undici";
import type { CustomJsxNetworkScope } from "../core";
@@ -191,7 +192,11 @@ export function assertSafeStaticHeaders(headers: Record<string, string> | undefi
}
}
export function createPinnedAgent(addresses: ResolvedAddress[], timeoutMs: number) {
export function createPinnedAgent(
addresses: ResolvedAddress[],
timeoutMs: number,
tls?: Pick<ConnectionOptions, "ca" | "checkServerIdentity">,
) {
const customLookup: LookupFunction = (_hostname, options, callback) => {
const family = options.family === 4 || options.family === 6 ? options.family : undefined;
const candidates = family ? addresses.filter((entry) => entry.family === family) : addresses;
@@ -204,7 +209,7 @@ export function createPinnedAgent(addresses: ResolvedAddress[], timeoutMs: numbe
else callback(null, selected.address, selected.family);
};
return new Agent({
connect: { lookup: customLookup },
connect: { ...tls, lookup: customLookup },
connectTimeout: timeoutMs,
headersTimeout: timeoutMs,
bodyTimeout: timeoutMs,
@@ -1,4 +1,5 @@
import { Buffer } from "node:buffer";
import type { ConnectionOptions } from "node:tls";
import { STATUS_CODES } from "node:http";
import { Headers, Response } from "undici";
@@ -52,6 +53,9 @@ export interface CustomWidgetHttpRequest {
auth?: CustomWidgetAuthConfig;
networkScope: CustomJsxNetworkScope;
kind: "query" | "action";
timeoutMs?: number;
textFallback?: boolean;
tls?: Pick<ConnectionOptions, "ca" | "checkServerIdentity">;
cacheKey?: string;
cacheTtlSeconds?: number;
logError?: (event: { origin: string; method: CustomWidgetMethod; errorName: string; reason?: "timeout" }) => void;
@@ -73,7 +77,10 @@ type RequestHopResult =
async function performRequest(input: CustomWidgetHttpRequest): Promise<CustomWidgetHttpResponse> {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), MAX_REQUEST_DURATION_MS);
const timeout = setTimeout(
() => controller.abort(),
Math.min(input.timeoutMs ?? MAX_REQUEST_DURATION_MS, MAX_REQUEST_DURATION_MS),
);
try {
return await performRequestWithinDeadline(input, controller.signal);
} catch (error) {
@@ -113,6 +120,7 @@ async function performRequestWithinDeadline(
const dispatcher = createPinnedAgent(
await resolveAndValidateHost(currentUrl.hostname, input.networkScope, { signal: deadlineSignal }),
REQUEST_TIMEOUT_MS,
input.tls,
);
const headers = buildHeaders(input, currentUrl, currentBody);
const controller = new AbortController();
@@ -141,7 +149,7 @@ async function performRequestWithinDeadline(
ok: response.ok,
status: response.status,
statusText: response.statusText,
data: await parseResponseBody(response),
data: await parseResponseBody(response, input.textFallback),
},
};
} else {
@@ -45,7 +45,7 @@ async function readLimitedBody(response: Response): Promise<string> {
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
}
export async function parseResponseBody(response: Response): Promise<unknown> {
export async function parseResponseBody(response: Response, textFallback = false): Promise<unknown> {
const text = await readLimitedBody(response);
if (!text) return null;
try {
@@ -54,7 +54,7 @@ export async function parseResponseBody(response: Response): Promise<unknown> {
return json;
} catch (error) {
if (error instanceof CustomWidgetDomainError) throw error;
if (response.headers.get("content-type")?.toLowerCase().includes("json")) {
if (!textFallback && response.headers.get("content-type")?.toLowerCase().includes("json")) {
throw new CustomWidgetDomainError({ code: "BAD_REQUEST", message: "Upstream returned invalid JSON" });
}
return text;