Skip to content

Commit 79912b2

Browse files
fix(server): keep MCP server secrets out of Claude's process arguments
The SDK passes mcpServers to the CLI as an inline --mcp-config argument, which any local user can read. A sensitive env value or header from Settings → Tools now goes into the child's environment and --mcp-config carries a ${VAR} reference, the same as T3's own token since #17408. Checked against claude 2.1.292: a stdio server received the expanded value. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8e7f082 commit 79912b2

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,19 +667,20 @@ describe("ClaudeAdapterV2 MCP query overrides", () => {
667667
mcpSession,
668668
readOnlySandbox: false,
669669
});
670+
// The secret reaches the child's environment, never `--mcp-config`.
670671
assert.deepEqual(overrides, {
671672
allowedTools: [ClaudeAdapterV2.CLAUDE_T3_MCP_TOOL_WILDCARD, "mcp__supabase__*"],
672673
mcpServers: {
673674
supabase: {
674675
type: "stdio",
675676
command: "npx",
676677
args: ["-y", "@supabase/mcp-server-supabase"],
677-
env: { SUPABASE_ACCESS_TOKEN: "token" },
678+
env: { SUPABASE_ACCESS_TOKEN: "${T3_MCP_SECRET_0}" },
678679
},
679680
...T3_MCP_SERVERS,
680681
},
681682
sdkSettings: { skillOverrides: { "grill-me": "off" } },
682-
mcpEnvironment: T3_MCP_ENVIRONMENT,
683+
mcpEnvironment: { T3_MCP_SECRET_0: "token", ...T3_MCP_ENVIRONMENT },
683684
});
684685
// Read-only sandboxes keep the user's servers behind the permission prompt.
685686
assert.deepEqual(

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666
type ProviderRequestKind,
6767
type ProviderUserInputAnswers,
6868
type ProviderThreadId,
69+
type McpServerVariable,
6970
type ResolvedMcpServer,
7071
type ThreadId,
7172
} from "@t3tools/contracts";
@@ -999,12 +1000,13 @@ export function claudeMcpQueryOverrides(input: {
9991000
const userAllowedTools = input.readOnlySandbox
10001001
? []
10011002
: tools.servers.map((server) => `mcp__${server.name}__*`);
1003+
const userServers = claudeUserMcpServers(tools.servers);
10021004
return {
10031005
allowedTools: Array.from(
10041006
new Set([...(input.allowedTools ?? []), ...mcpAllowedTools, ...userAllowedTools]),
10051007
),
10061008
mcpServers: {
1007-
...claudeUserMcpServers(tools.servers),
1009+
...userServers.mcpServers,
10081010
"t3-code": {
10091011
type: "http",
10101012
url: session.endpoint,
@@ -1026,30 +1028,52 @@ export function claudeMcpQueryOverrides(input: {
10261028
),
10271029
},
10281030
}),
1029-
mcpEnvironment: { [CLAUDE_T3_MCP_AUTHORIZATION_ENV]: session.authorizationHeader },
1031+
mcpEnvironment: {
1032+
...userServers.environment,
1033+
[CLAUDE_T3_MCP_AUTHORIZATION_ENV]: session.authorizationHeader,
1034+
},
10301035
};
10311036
}
10321037

1033-
function claudeUserMcpServers(
1034-
servers: ReadonlyArray<ResolvedMcpServer>,
1035-
): NonNullable<ClaudeQueryOptions["mcpServers"]> {
1036-
return Object.fromEntries(
1038+
/**
1039+
* The user's servers for `--mcp-config`. Like T3's own token, a sensitive
1040+
* env value or header becomes a `${VAR}` reference there, and its value goes
1041+
* into the child's environment.
1042+
*/
1043+
function claudeUserMcpServers(servers: ReadonlyArray<ResolvedMcpServer>): {
1044+
readonly mcpServers: NonNullable<ClaudeQueryOptions["mcpServers"]>;
1045+
readonly environment: Readonly<Record<string, string>>;
1046+
} {
1047+
const environment: Record<string, string> = {};
1048+
const values = (server: string, variables: ReadonlyArray<McpServerVariable>) => {
1049+
const record = mcpServerVariableRecord(variables);
1050+
for (const variable of variables) {
1051+
if (!variable.sensitive || !Object.hasOwn(record, variable.name)) continue;
1052+
// Indexed, so two names that read the same once uppercased stay apart.
1053+
const reference = `T3_MCP_SECRET_${Object.keys(environment).length}`;
1054+
environment[reference] = record[variable.name]!;
1055+
record[variable.name] = `\${${reference}}`;
1056+
}
1057+
return record;
1058+
};
1059+
const mcpServers = Object.fromEntries(
10371060
servers.map((server) => [
10381061
server.name,
10391062
server.transport.type === "stdio"
10401063
? {
10411064
type: "stdio" as const,
10421065
command: server.transport.command,
10431066
args: [...server.transport.args],
1044-
env: mcpServerVariableRecord(server.transport.env),
1067+
env: values(server.name, server.transport.env),
10451068
}
10461069
: {
10471070
type: "http" as const,
10481071
url: server.transport.url,
1049-
headers: mcpServerVariableRecord(server.transport.headers),
1072+
headers: values(server.name, server.transport.headers),
10501073
},
10511074
]),
10521075
);
1076+
return { mcpServers, environment };
10531077
}
10541078

10551079
function providerSession(input: {

0 commit comments

Comments
 (0)