Skip to content

PATH lost and npx not found when proxying MCP servers because get_default_environment() is not used when user provides env in StdioServerParameters #185

Closed
@zhjch05

Description

@zhjch05

Describe the bug
In https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/stdio.py#L102

process = await anyio.open_process(
      [server.command, *server.args],
      env=server.env if server.env is not None else get_default_environment(),
      stderr=sys.stderr,
)

I wonder in the case where user specifies the env input, why get_default_environment()'s env vars are not retained? Not sure if this is a bug or left as it is intentionally.
get_default_environment() returns crucial env vars like PATH and without it I current run into a issue where I have a proxy MCP server under uv and it tries to connect to a npx based MCP server but when env vars are specified in server params then the PATH is no longer injected, resulting in npx not found error in my use case.
Can we change it to following? Not sure if this has side effects or not, if it works happy to create a PR for that. So default envs are always provided but will be overwritten by user if they want.

process = await anyio.open_process(
      [server.command, *server.args],
      env={
          **get_default_environment(),
          **(server.env or {})
      },
      stderr=sys.stderr,
)

To Reproduce
In a uvx isolated python environment, create a python client to connect to npx based pkg like @modelcontextprotocol/server-slack with env like SLACK_TEAM_ID configured, then it will raise an npx not found error.

Expected behavior
It should execute the npx based MCP server correctly.

Screenshots

Image

Desktop (please complete the following information):

  • OS: [MacOS]
  • Browser [N/A]
  • Version [14.0 (23A344)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions