Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions extensions/cli/src/services/MCPService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe("MCPService", () => {

afterEach(async () => {
vi.clearAllMocks();
vi.unstubAllEnvs();
await mcpService.cleanup();
});

Expand Down Expand Up @@ -218,6 +219,39 @@ describe("MCPService", () => {
).resolves.not.toThrow();
});

it("should not forward ambient environment variables to stdio servers", async () => {
vi.stubEnv("CONTINUE_MCP_TEST_SECRET", "must-not-be-inherited");
const { StdioClientTransport } = await import(
"@modelcontextprotocol/sdk/client/stdio.js"
);
const stdioAssistant: AssistantConfig = {
name: "stdio-assistant",
version: "1.0.0",
mcpServers: [
{
name: "stdio-server",
command: "node",
env: { EXPLICIT_MCP_VALUE: "allowed" },
},
],
} as AssistantConfig;

await mcpService.initialize(stdioAssistant);

expect(StdioClientTransport).toHaveBeenCalledWith(
expect.objectContaining({
env: { EXPLICIT_MCP_VALUE: "allowed" },
}),
);
expect(StdioClientTransport).not.toHaveBeenCalledWith(
expect.objectContaining({
env: expect.objectContaining({
CONTINUE_MCP_TEST_SECRET: "must-not-be-inherited",
}),
}),
);
});

it("should create HttpsAgent when verifySsl is false for SSE transport", async () => {
const { SSEClientTransport } = await import(
"@modelcontextprotocol/sdk/client/sse.js"
Expand Down
15 changes: 5 additions & 10 deletions extensions/cli/src/services/mcpTransports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,14 @@ export function constructStdioTransport(
serverConfig: StdioMcpServer,
connection: MCPConnectionInfo,
): StdioClientTransport {
const env: Record<string, string> = serverConfig.env || {};
if (process.env) {
for (const [key, value] of Object.entries(process.env)) {
if (!(key in env) && !!value) {
env[key] = value;
}
}
}

const transport = new StdioClientTransport({
command: serverConfig.command,
args: serverConfig.args || [],
env,
// The MCP SDK adds its platform-safe default environment (for example
// PATH and HOME). Only add variables the user explicitly configured;
// forwarding process.env would expose unrelated credentials to every
// stdio MCP server.
env: serverConfig.env,
cwd: serverConfig.cwd,
stderr: "pipe",
});
Expand Down
Loading