-
Notifications
You must be signed in to change notification settings - Fork 978
fix(client): Some mcp server need default env(#393, #196) #394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sunrabbit123
wants to merge
5
commits into
modelcontextprotocol:main
Choose a base branch
from
sunrabbit123:fix/1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+71
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd359ed
fix(client): No such file or directory in StdioClientTransport(#393, …
sunrabbit123 9cca201
fix: modify test code about stdio.test
sunrabbit123 47204f2
Merge branch 'main' into fix/1
sunrabbit123 c5dd44c
Merge branch 'main' into fix/1
sunrabbit123 823fc43
Merge branch 'main' into fix/1
sunrabbit123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,21 @@ | ||
import { JSONRPCMessage } from "../types.js"; | ||
import { StdioClientTransport, StdioServerParameters } from "./stdio.js"; | ||
import { StdioClientTransport, StdioServerParameters, DEFAULT_INHERITED_ENV_VARS, getDefaultEnvironment } from "./stdio.js"; | ||
|
||
const serverParameters: StdioServerParameters = { | ||
command: "/usr/bin/tee", | ||
}; | ||
|
||
|
||
let spawnEnv: Record<string, string> | undefined; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
jest.mock('cross-spawn', () => { | ||
const originalSpawn = jest.requireActual('cross-spawn'); | ||
return jest.fn((command, args, options) => { | ||
spawnEnv = options.env; | ||
return originalSpawn(command, args, options); | ||
}); | ||
}); | ||
|
||
test("should start then close cleanly", async () => { | ||
const client = new StdioClientTransport(serverParameters); | ||
client.onerror = (error) => { | ||
|
@@ -59,3 +70,52 @@ test("should read messages", async () => { | |
|
||
await client.close(); | ||
}); | ||
|
||
test("should properly set default environment variables in spawned process", async () => { | ||
const client = new StdioClientTransport(serverParameters); | ||
|
||
await client.start(); | ||
await client.close(); | ||
|
||
// Get the default environment variables | ||
const defaultEnv = getDefaultEnvironment(); | ||
|
||
// Verify that all default environment variables are present | ||
for (const key of DEFAULT_INHERITED_ENV_VARS) { | ||
if (process.env[key] && !process.env[key].startsWith("()")) { | ||
expect(spawnEnv).toHaveProperty(key); | ||
expect(spawnEnv![key]).toBe(process.env[key]); | ||
expect(spawnEnv![key]).toBe(defaultEnv[key]); | ||
} | ||
} | ||
}); | ||
|
||
test("should override default environment variables with custom ones", async () => { | ||
const customEnv = { | ||
HOME: "/custom/home", | ||
PATH: "/custom/path", | ||
USER: "custom_user" | ||
}; | ||
|
||
const client = new StdioClientTransport({ | ||
...serverParameters, | ||
env: customEnv | ||
}); | ||
|
||
await client.start(); | ||
await client.close(); | ||
|
||
// Verify that custom environment variables override default ones | ||
for (const [key, value] of Object.entries(customEnv)) { | ||
expect(spawnEnv).toHaveProperty(key); | ||
expect(spawnEnv![key]).toBe(value); | ||
} | ||
|
||
// Verify that other default environment variables are still present | ||
for (const key of DEFAULT_INHERITED_ENV_VARS) { | ||
if (!(key in customEnv) && process.env[key] && !process.env[key].startsWith("()")) { | ||
expect(spawnEnv).toHaveProperty(key); | ||
expect(spawnEnv![key]).toBe(process.env[key]); | ||
} | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be removed