Skip to content

Commit 87377cf

Browse files
fix(mcp): Ensure that stdio MCP server execution has the GEMINI_CLI=1 env variable populated. (#18832)
1 parent 6c17731 commit 87377cf

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

packages/core/src/services/shellExecutionService.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ const { Terminal } = pkg;
3232

3333
const MAX_CHILD_PROCESS_BUFFER_SIZE = 16 * 1024 * 1024; // 16MB
3434

35+
/**
36+
* An environment variable that is set for shell executions. This can be used
37+
* by downstream executables and scripts to identify that they were executed
38+
* from within Gemini CLI.
39+
*/
40+
export const GEMINI_CLI_IDENTIFICATION_ENV_VAR = 'GEMINI_CLI';
41+
42+
/**
43+
* The value of {@link GEMINI_CLI_IDENTIFICATION_ENV_VAR}
44+
*/
45+
export const GEMINI_CLI_IDENTIFICATION_ENV_VAR_VALUE = '1';
46+
3547
// We want to allow shell outputs that are close to the context window in size.
3648
// 300,000 lines is roughly equivalent to a large context window, ensuring
3749
// we capture significant output from long-running commands.
@@ -302,7 +314,8 @@ export class ShellExecutionService {
302314
detached: !isWindows,
303315
env: {
304316
...sanitizeEnvironment(process.env, sanitizationConfig),
305-
GEMINI_CLI: '1',
317+
[GEMINI_CLI_IDENTIFICATION_ENV_VAR]:
318+
GEMINI_CLI_IDENTIFICATION_ENV_VAR_VALUE,
306319
TERM: 'xterm-256color',
307320
PAGER: 'cat',
308321
GIT_PAGER: 'cat',

packages/core/src/tools/mcp-client.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,28 @@ describe('mcp-client', () => {
16391639
});
16401640
});
16411641

1642+
it('sets an env variable GEMINI_CLI=1 for stdio MCP servers', async () => {
1643+
const mockedTransport = vi
1644+
.spyOn(SdkClientStdioLib, 'StdioClientTransport')
1645+
.mockReturnValue({} as SdkClientStdioLib.StdioClientTransport);
1646+
1647+
await createTransport(
1648+
'test-server',
1649+
{
1650+
command: 'test-command',
1651+
args: ['--foo', 'bar'],
1652+
env: {},
1653+
cwd: 'test/cwd',
1654+
},
1655+
false,
1656+
EMPTY_CONFIG,
1657+
);
1658+
1659+
const callArgs = mockedTransport.mock.calls[0][0];
1660+
expect(callArgs.env).toBeDefined();
1661+
expect(callArgs.env!['GEMINI_CLI']).toBe('1');
1662+
});
1663+
16421664
it('should exclude extension settings with undefined values from environment', async () => {
16431665
const mockedTransport = vi
16441666
.spyOn(SdkClientStdioLib, 'StdioClientTransport')

packages/core/src/tools/mcp-client.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ import {
6767
sanitizeEnvironment,
6868
type EnvironmentSanitizationConfig,
6969
} from '../services/environmentSanitization.js';
70+
import {
71+
GEMINI_CLI_IDENTIFICATION_ENV_VAR,
72+
GEMINI_CLI_IDENTIFICATION_ENV_VAR_VALUE,
73+
} from '../services/shellExecutionService.js';
7074

7175
export const MCP_DEFAULT_TIMEOUT_MSEC = 10 * 60 * 1000; // default to 10 minutes
7276

@@ -1897,10 +1901,11 @@ export async function createTransport(
18971901
let transport: Transport = new StdioClientTransport({
18981902
command: mcpServerConfig.command,
18991903
args: mcpServerConfig.args || [],
1900-
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
19011904
env: {
19021905
...sanitizeEnvironment(process.env, sanitizationConfig),
19031906
...(mcpServerConfig.env || {}),
1907+
[GEMINI_CLI_IDENTIFICATION_ENV_VAR]:
1908+
GEMINI_CLI_IDENTIFICATION_ENV_VAR_VALUE,
19041909
} as Record<string, string>,
19051910
cwd: mcpServerConfig.cwd,
19061911
stderr: 'pipe',

0 commit comments

Comments
 (0)