Skip to content
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

MCP Client Environment Variables in StdioServerParameters #99

Open
AshuJoshi opened this issue Dec 11, 2024 · 2 comments
Open

MCP Client Environment Variables in StdioServerParameters #99

AshuJoshi opened this issue Dec 11, 2024 · 2 comments
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@AshuJoshi
Copy link

Describe the bug
Passing environment variables to the MCP Server - I am trying to test the example Weather MCP Server with both the MCP Inspector and a simple Python MCP Client, and when I try to pass the Open Weather API key using the 'env' field - I cannot it to work.

To Reproduce
Steps to reproduce the behavior:

  1. Use StdioServerParameters to configure connect with MCP server in Python Client
  2. Specify the Open Weather Key in the parameters
  3. When running the Python client - you get an error
    [Errno 2] No such file or directory: 'uv'

Here is the code snippet from the Python Client:

server_params = StdioServerParameters(
        command="uv",
        args=[
            "--directory",
            "<PATH-TO-SERVER>/weather_service",
            "run",
            "weather-service"
            ],
        # env=None
        env={
            "OPENWEATHER_API_KEY" : API_KEY
        }
    )

    # Initialize MCP client with server parameters
    async with MCPClient(server_params) as mcp_client:

If I do not pass any environment by either commenting the env field or remove it all together, then the Client is able to connect with the Server.
Are there any examples on using StdioServerParameters in Python Clients? I could not find any examples (other than env=None) in example MCP Client code.

@christopherhwood
Copy link

christopherhwood commented Dec 12, 2024

You need to make sure to include the necessary default env vars for it to find the uv program. When you set env to None then it uses these default env vars:

# Environment variables to inherit by default
DEFAULT_INHERITED_ENV_VARS = (
[
"APPDATA",
"HOMEDRIVE",
"HOMEPATH",
"LOCALAPPDATA",
"PATH",
"PROCESSOR_ARCHITECTURE",
"SYSTEMDRIVE",
"SYSTEMROOT",
"TEMP",
"USERNAME",
"USERPROFILE",
]
if sys.platform == "win32"
else ["HOME", "LOGNAME", "PATH", "SHELL", "TERM", "USER"]
)
def get_default_environment() -> dict[str, str]:
"""
Returns a default environment object including only environment variables deemed
safe to inherit.
"""
env: dict[str, str] = {}
for key in DEFAULT_INHERITED_ENV_VARS:
value = os.environ.get(key)
if value is None:
continue
if value.startswith("()"):
# Skip functions, which are a security risk
continue
env[key] = value
return env

As a fast workaround, if you copy over the above code for the get_default_environment and use that as the base env dict, then update with your new env vars and pass that through to the mcp then you should see it working, something like this:

async def connect_to_server(self, server_config: ServerConfig):
    env = get_default_environment()
    env.update(server_config.env) if server_config.env else None

    server_params = StdioServerParameters(
        command=server_config.command,
        args=server_config.args,
        env=env
    )
    # rest of code here...

@dsp-ant dsp-ant added documentation Improvements or additions to documentation good first issue Good for newcomers labels Jan 7, 2025
@kzmszk
Copy link

kzmszk commented Jan 10, 2025

I believe it would be much better to apply the above code into the sdk.

https://github.com/modelcontextprotocol/python-sdk/blob/main/src/mcp/client/stdio.py#L100-L103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants