Skip to content

Commit 8e4a8a4

Browse files
authored
Send the default User-Agent on the MCP endpoint probe (#2258)
1 parent 9fa93c2 commit 8e4a8a4

4 files changed

Lines changed: 33 additions & 13 deletions

File tree

‎.changeset/mcp-probe-user-agent.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"executor": patch
3+
---
4+
5+
Send the default User-Agent on the MCP endpoint probe and its OAuth metadata request too, so adding a server that rejects requests without one no longer fails. A configured User-Agent still wins.

‎packages/plugins/mcp/src/sdk/connection.ts‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
McpOAuthReauthorizationRequired,
2222
} from "./errors";
2323
import { connectionHttpStatusFromCause, isStreamableHttpProtocolError } from "./http-status";
24+
import { withDefaultUserAgent } from "./user-agent";
2425
import { detectInsufficientScope } from "@executor-js/sdk/core";
2526

2627
// ---------------------------------------------------------------------------
@@ -72,17 +73,6 @@ const buildEndpointUrl = (endpoint: string, queryParams: Record<string, string>)
7273
return url;
7374
};
7475

75-
// Some upstreams (Cloudflare bot rules among them) reject requests without a
76-
// User-Agent, and Workers' fetch adds none. Both remote SDK transports apply
77-
// `requestInit.headers` to every request they send, including OAuth metadata
78-
// discovery, so the default goes there. A configured User-Agent wins.
79-
const DEFAULT_USER_AGENT = "executor";
80-
81-
const withDefaultUserAgent = (headers: Record<string, string>): Record<string, string> =>
82-
Object.keys(headers).some((name) => name.toLowerCase() === "user-agent")
83-
? headers
84-
: { "User-Agent": DEFAULT_USER_AGENT, ...headers };
85-
8676
type HttpMethod = Parameters<typeof HttpClientRequest.make>[0];
8777
const HTTP_METHODS = new Set<HttpMethod>([
8878
"DELETE",
@@ -636,7 +626,9 @@ export const createMcpConnector = (input: ConnectorInput): McpConnector => {
636626
});
637627
}
638628

639-
// Remote transport
629+
// Remote transport. Both remote SDK transports apply `requestInit.headers`
630+
// to every request they send, including OAuth metadata discovery, so the
631+
// default User-Agent goes there.
640632
const headers = withDefaultUserAgent(input.headers ?? {});
641633
const remoteTransport = input.remoteTransport ?? "auto";
642634
const requestInit = { headers };

‎packages/plugins/mcp/src/sdk/probe-shape.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import { Data, Duration, Effect, Layer, Option, Schema } from "effect";
4646
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http";
47+
import { userAgentFor } from "./user-agent";
4748

4849
/** MCP initialize request body used as the shape probe. Any real MCP
4950
* server either answers it (unauth-OK server) or returns the spec-
@@ -277,7 +278,13 @@ export const probeMcpEndpointShape = (
277278
Effect.gen(function* () {
278279
const timeoutMs = options.timeoutMs ?? 8_000;
279280
const outcome = yield* Effect.gen(function* () {
280-
const client = yield* HttpClient.HttpClient;
281+
// Every probe request — including the RFC 9728 metadata fallback, which
282+
// carries none of the configured headers — sends the User-Agent.
283+
const client = (yield* HttpClient.HttpClient).pipe(
284+
HttpClient.mapRequest(
285+
HttpClientRequest.setHeader("user-agent", userAgentFor(options.headers ?? {})),
286+
),
287+
);
281288

282289
const readBody = (response: {
283290
readonly text: Effect.Effect<string, unknown>;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Some upstreams (Cloudflare bot rules among them) reject requests without a
2+
// User-Agent, and Workers' fetch adds none. Every request the MCP plugin sends
3+
// to an MCP server or its OAuth metadata carries this default. A configured
4+
// User-Agent wins.
5+
export const DEFAULT_USER_AGENT = "executor";
6+
7+
const configuredUserAgent = (headers: Readonly<Record<string, string>>): string | undefined =>
8+
Object.entries(headers).find(([name]) => name.toLowerCase() === "user-agent")?.[1];
9+
10+
export const withDefaultUserAgent = (headers: Record<string, string>): Record<string, string> =>
11+
configuredUserAgent(headers) === undefined
12+
? { "User-Agent": DEFAULT_USER_AGENT, ...headers }
13+
: headers;
14+
15+
export const userAgentFor = (headers: Readonly<Record<string, string>>): string =>
16+
configuredUserAgent(headers) ?? DEFAULT_USER_AGENT;

0 commit comments

Comments
 (0)