Skip to content

Commit 82e2b78

Browse files
Use MCP direct env var mode in Node SDK
1 parent df23ff4 commit 82e2b78

7 files changed

Lines changed: 1310 additions & 8 deletions

File tree

nodejs/src/client.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ import type {
3434
ModelInfo,
3535
ResumeSessionConfig,
3636
SessionConfig,
37+
SessionContext,
3738
SessionEvent,
3839
SessionLifecycleEvent,
3940
SessionLifecycleEventType,
4041
SessionLifecycleHandler,
41-
SessionContext,
4242
SessionListFilter,
4343
SessionMetadata,
4444
Tool,
@@ -529,6 +529,7 @@ export class CopilotClient {
529529
workingDirectory: config.workingDirectory,
530530
streaming: config.streaming,
531531
mcpServers: config.mcpServers,
532+
envValueMode: "direct",
532533
customAgents: config.customAgents,
533534
configDir: config.configDir,
534535
skillDirectories: config.skillDirectories,
@@ -611,6 +612,7 @@ export class CopilotClient {
611612
configDir: config.configDir,
612613
streaming: config.streaming,
613614
mcpServers: config.mcpServers,
615+
envValueMode: config.mcpServers ? "direct" : undefined,
614616
customAgents: config.customAgents,
615617
skillDirectories: config.skillDirectories,
616618
disabledSkills: config.disabledSkills,

nodejs/test/e2e/harness/sdkTestContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const SNAPSHOTS_DIR = resolve(__dirname, "../../../../test/snapshots");
1919

2020
export async function createSdkTestContext({
2121
logLevel,
22-
}: { logLevel?: "error" | "none" | "warning" | "info" | "debug" | "all" } = {}) {
22+
}: { logLevel?: "error" | "none" | "warning" | "info" | "debug" | "all"; cliPath?: string } = {}) {
2323
const homeDir = realpathSync(fs.mkdtempSync(join(os.tmpdir(), "copilot-test-config-")));
2424
const workDir = realpathSync(fs.mkdtempSync(join(os.tmpdir(), "copilot-test-work-")));
2525

@@ -40,6 +40,7 @@ export async function createSdkTestContext({
4040
cwd: workDir,
4141
env,
4242
logLevel: logLevel || "error",
43+
cliPath: process.env.COPILOT_CLI_PATH,
4344
// Use fake token in CI to allow cached responses without real auth
4445
githubToken: process.env.CI === "true" ? "fake-token-for-e2e-tests" : undefined,
4546
});

nodejs/test/e2e/mcp_and_agents.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
*--------------------------------------------------------------------------------------------*/
44

5+
import { dirname, resolve } from "path";
6+
import { fileURLToPath } from "url";
57
import { describe, expect, it } from "vitest";
68
import type { CustomAgentConfig, MCPLocalServerConfig, MCPServerConfig } from "../../src/index.js";
79
import { createSdkTestContext } from "./harness/sdkTestContext.js";
810

11+
const __filename = fileURLToPath(import.meta.url);
12+
const __dirname = dirname(__filename);
13+
const TEST_MCP_SERVER = resolve(__dirname, "../../../test/harness/test-mcp-server.mjs");
14+
915
describe("MCP Servers and Custom Agents", async () => {
1016
const { copilotClient: client } = await createSdkTestContext();
1117

@@ -88,6 +94,31 @@ describe("MCP Servers and Custom Agents", async () => {
8894
expect(session.sessionId).toBeDefined();
8995
await session.destroy();
9096
});
97+
98+
it("should pass literal env values to MCP server subprocess", async () => {
99+
const mcpServers: Record<string, MCPServerConfig> = {
100+
"env-echo": {
101+
type: "local",
102+
command: "node",
103+
args: [TEST_MCP_SERVER],
104+
tools: ["*"],
105+
env: { TEST_SECRET: "hunter2" },
106+
} as MCPLocalServerConfig,
107+
};
108+
109+
const session = await client.createSession({
110+
mcpServers,
111+
});
112+
113+
expect(session.sessionId).toBeDefined();
114+
115+
const message = await session.sendAndWait({
116+
prompt: "Use the env-echo/get_env tool to read the TEST_SECRET environment variable. Reply with just the value, nothing else.",
117+
});
118+
expect(message?.data.content).toContain("hunter2");
119+
120+
await session.destroy();
121+
});
91122
});
92123

93124
describe("Custom Agents", () => {

0 commit comments

Comments
 (0)