Skip to content

Commit dac8069

Browse files
cliffhallclaude
andcommitted
fix(servers): pass-6 polish — scope absorber, thread id, drop empty OAuth, guard in
Pass-6 review at #1353 (comment). Reviewer signed off as ready; landing the four marginal cleanups since they're all small. (a) `connectPromise.catch(() => {})` moves inside the `if (connectTimeoutMs > 0)` branch. The await-only path propagates rejection cleanly through the outer try/catch — the absorber is only needed when the race leaves the original promise unattended. Code now reads as "absorber is tied to the race," which is the real invariant. (b) Thread `id` (POST id, PUT newId) through `buildStoredEntry` and include it in the smuggle warning's pino bindings. Two clients smuggling simultaneously are distinguishable in the log instead of indistinguishable. (c) `validateSettings` coerces empty-string OAuth fields to absent. The form emits `""` when the user clears an input, and an empty `oauthClientId` on disk would later be misread as "OAuth configured." New route test pins it. (d) `buildStoredEntry` adds a null/object guard before `"settings" in configObj`. The route layer pre-checks this on POST and PUT, but the helper's `unknown` parameter is a weak contract; the guard makes it safe to call from anywhere. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 02801e1 commit dac8069

3 files changed

Lines changed: 70 additions & 14 deletions

File tree

‎clients/web/src/test/integration/mcp/remote/servers-route.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,34 @@ describe("/api/servers routes", () => {
605605
expect(res.status).toBe(400);
606606
});
607607

608+
it("validateSettings coerces empty-string OAuth fields to absent (cleared inputs don't read as 'configured')", async () => {
609+
const res = await fetch(`${h.baseUrl}/api/servers`, {
610+
method: "POST",
611+
headers: { "Content-Type": "application/json" },
612+
body: JSON.stringify({
613+
id: "empty-oauth",
614+
config: { type: "streamable-http", url: "https://x.test/mcp" },
615+
settings: {
616+
headers: [],
617+
metadata: [],
618+
connectionTimeout: 0,
619+
requestTimeout: 0,
620+
oauthClientId: "",
621+
oauthClientSecret: "",
622+
oauthScopes: "",
623+
},
624+
}),
625+
});
626+
expect(res.status).toBe(200);
627+
const stored = readConfig(h.configPath).mcpServers["empty-oauth"] as {
628+
settings?: Record<string, unknown>;
629+
};
630+
expect(stored.settings).toBeDefined();
631+
expect(stored.settings).not.toHaveProperty("oauthClientId");
632+
expect(stored.settings).not.toHaveProperty("oauthClientSecret");
633+
expect(stored.settings).not.toHaveProperty("oauthScopes");
634+
});
635+
608636
it("validateSettings drops unknown keys (explicit pick-and-build, not spread)", async () => {
609637
const res = await fetch(`${h.baseUrl}/api/servers`, {
610638
method: "POST",

‎core/mcp/inspectorClient.ts‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,16 @@ export class InspectorClient extends InspectorClientEventTarget {
603603
// connect() starts clean and the upstream socket isn't left hanging.
604604
const connectTimeoutMs = this.serverSettings?.connectionTimeout ?? 0;
605605
const connectPromise = this.client.connect(this.transport);
606-
// Absorb any late rejection from `connectPromise` — when the timeout
607-
// wins the race and `disconnect()` tears the transport down, real
608-
// transports (SSE / streamable-http) reject the in-flight handshake
609-
// *after* Promise.race has already settled. Without a handler here
610-
// Node emits an unhandledRejection warning (and in test runs vitest
611-
// can surface it as a suite failure).
612-
connectPromise.catch(() => {});
613606
if (connectTimeoutMs > 0) {
607+
// Absorb any late rejection from `connectPromise` — when the timeout
608+
// wins the race and `disconnect()` tears the transport down, real
609+
// transports (SSE / streamable-http) reject the in-flight handshake
610+
// *after* Promise.race has already settled. Without a handler here
611+
// Node emits an unhandledRejection warning (and in test runs vitest
612+
// can surface it as a suite failure). Only needed on the race path;
613+
// the await-only branch below propagates rejection cleanly through
614+
// the outer try/catch.
615+
connectPromise.catch(() => {});
614616
let timer: ReturnType<typeof setTimeout> | undefined;
615617
const timeoutPromise = new Promise<never>((_, reject) => {
616618
timer = setTimeout(

‎core/mcp/remote/node/server.ts‎

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -670,14 +670,26 @@ export function createRemoteApp(
670670
// remains the single write path for the settings node. Log a warning if
671671
// we observe this — it indicates a client bug (settings should travel
672672
// through the body's top-level `settings` field, not nested in config).
673+
//
674+
// `id` is threaded through purely so the warning correlates to a
675+
// specific entry; the route ultimately reaches here via POST or PUT and
676+
// both know the target id at call time.
673677
const buildStoredEntry = (
678+
id: string,
674679
config: unknown,
675680
settings: InspectorServerSettings | undefined,
676681
): StoredMCPServer => {
677-
const configObj = (config as Record<string, unknown>) ?? {};
682+
// `unknown` parameter contract: be honest about the shape rather than
683+
// assuming the route layer's pre-checks. The route handlers do reject
684+
// non-object config before reaching here, but this helper should stay
685+
// safe to call from anywhere.
686+
const configObj: Record<string, unknown> =
687+
config !== null && typeof config === "object"
688+
? (config as Record<string, unknown>)
689+
: {};
678690
if ("settings" in configObj) {
679691
fileLogger?.warn(
680-
{ route: "/api/servers", smuggledKey: "settings" },
692+
{ route: "/api/servers", id, smuggledKey: "settings" },
681693
"Stripping `settings` key from request body's `config` — settings must travel through the top-level `settings` field, not nested inside `config`.",
682694
);
683695
}
@@ -755,19 +767,25 @@ export function createRemoteApp(
755767
// casting the raw object through. Unknown keys silently drop so a
756768
// misconfigured client can't smuggle stowaways onto disk, and consumers
757769
// can rely on the validated shape being exactly InspectorServerSettings.
770+
// Empty-string OAuth fields coerce to absent — the form emits `""` when
771+
// the user clears an input, and an empty `oauthClientId` on disk would
772+
// later be misread as "OAuth configured."
758773
const value: InspectorServerSettings = {
759774
headers: obj.headers as { key: string; value: string }[],
760775
metadata: obj.metadata as { key: string; value: string }[],
761776
connectionTimeout: obj.connectionTimeout as number,
762777
requestTimeout: obj.requestTimeout as number,
763778
};
764-
if (typeof obj.oauthClientId === "string") {
779+
if (typeof obj.oauthClientId === "string" && obj.oauthClientId !== "") {
765780
value.oauthClientId = obj.oauthClientId;
766781
}
767-
if (typeof obj.oauthClientSecret === "string") {
782+
if (
783+
typeof obj.oauthClientSecret === "string" &&
784+
obj.oauthClientSecret !== ""
785+
) {
768786
value.oauthClientSecret = obj.oauthClientSecret;
769787
}
770-
if (typeof obj.oauthScopes === "string") {
788+
if (typeof obj.oauthScopes === "string" && obj.oauthScopes !== "") {
771789
value.oauthScopes = obj.oauthScopes;
772790
}
773791
return { ok: true, value };
@@ -863,7 +881,11 @@ export function createRemoteApp(
863881
if (id in current.mcpServers) {
864882
return c.json({ error: `Server '${id}' already exists` }, 409);
865883
}
866-
current.mcpServers[id] = buildStoredEntry(body.config, postSettings);
884+
current.mcpServers[id] = buildStoredEntry(
885+
id,
886+
body.config,
887+
postSettings,
888+
);
867889
await writeStoreFile(mcpConfigPath, serializeStore(current));
868890
return c.json({ ok: true });
869891
});
@@ -963,7 +985,11 @@ export function createRemoteApp(
963985
const next: MCPConfig = { mcpServers: {} };
964986
for (const [key, val] of Object.entries(current.mcpServers)) {
965987
if (key === originalId) {
966-
next.mcpServers[newId] = buildStoredEntry(nextConfig, nextSettings);
988+
next.mcpServers[newId] = buildStoredEntry(
989+
newId,
990+
nextConfig,
991+
nextSettings,
992+
);
967993
} else {
968994
next.mcpServers[key] = val;
969995
}

0 commit comments

Comments
 (0)