Skip to content

Commit bb26197

Browse files
authored
delegate to mcp sdk for env variables for stdio connection (#310)
The MCP SDK added support for environment variable inheritance in 0.6.x. See this commit for reference: modelcontextprotocol/python-sdk@5508697
1 parent 3b514f0 commit bb26197

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

langchain_mcp_adapters/sessions.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
from __future__ import annotations
88

9-
import os
109
from contextlib import asynccontextmanager
1110
from datetime import timedelta
1211
from typing import TYPE_CHECKING, Any, Literal, Protocol
@@ -69,7 +68,17 @@ class StdioConnection(TypedDict):
6968
"""Command line arguments to pass to the executable."""
7069

7170
env: NotRequired[dict[str, str] | None]
72-
"""The environment to use when spawning the process."""
71+
"""The environment to use when spawning the process.
72+
73+
If not specified or set to None, a subset of the default environment
74+
variables from the current process will be used.
75+
76+
Please refer to the MCP SDK documentation for details on which
77+
environment variables are included by default. The behavior
78+
varies by operating system.
79+
80+
https://github.com/modelcontextprotocol/python-sdk/blob/c47c767ff437ee88a19e6b9001e2472cb6f7d5ed/src/mcp/client/stdio/__init__.py#L51
81+
"""
7382

7483
cwd: NotRequired[str | Path | None]
7584
"""The working directory to use when spawning the process."""
@@ -196,6 +205,8 @@ async def _create_stdio_session( # noqa: PLR0913
196205
command: Command to execute.
197206
args: Arguments for the command.
198207
env: Environment variables for the command.
208+
If not specified, inherits a subset of the current environment.
209+
The details are implemented in the MCP sdk.
199210
cwd: Working directory for the command.
200211
encoding: Character encoding.
201212
encoding_error_handler: How to handle encoding errors.
@@ -204,13 +215,6 @@ async def _create_stdio_session( # noqa: PLR0913
204215
Yields:
205216
An initialized ClientSession.
206217
"""
207-
# NOTE: execution commands (e.g., `uvx` / `npx`) require PATH envvar to be set.
208-
# To address this, we automatically inject existing PATH envvar into the `env`,
209-
# if it's not already set.
210-
env = env or {}
211-
if "PATH" not in env:
212-
env["PATH"] = os.environ.get("PATH", "")
213-
214218
server_params = StdioServerParameters(
215219
command=command,
216220
args=args,

0 commit comments

Comments
 (0)