Skip to content

Commit f5a122c

Browse files
Add logging to track process creation paths on Windows
Added comprehensive logging to help debug CI/local differences: - Log which platform path is taken (Windows vs Unix/Linux) - Log which Windows process type is created (anyio vs FallbackProcess) - Log any errors that cause fallbacks to different process creation methods - Include command names in logs for easier debugging This will help determine if CI and local environments are using different process creation mechanisms, which could explain test behavior differences.
1 parent e27f89b commit f5a122c

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/mcp/client/stdio/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import sys
34
from contextlib import asynccontextmanager
@@ -18,6 +19,8 @@
1819
get_windows_executable_command,
1920
)
2021

22+
logger = logging.getLogger(__name__)
23+
2124
# Environment variables to inherit by default
2225
DEFAULT_INHERITED_ENV_VARS = (
2326
[
@@ -240,8 +243,10 @@ async def _create_platform_compatible_process(
240243
Returns a process handle.
241244
"""
242245
if sys.platform == "win32":
246+
logger.info(f"Creating Windows process for command: {command}")
243247
process = await create_windows_process(command, args, env, errlog, cwd)
244248
else:
249+
logger.info(f"Creating Unix/Linux process using anyio for command: {command}")
245250
process = await anyio.open_process([command, *args], env=env, stderr=errlog, cwd=cwd)
246251

247252
return process

src/mcp/client/stdio/win32.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Windows-specific functionality for stdio client operations.
33
"""
44

5+
import logging
56
import shutil
67
import subprocess
78
import sys
@@ -13,6 +14,8 @@
1314
from anyio.abc import Process
1415
from anyio.streams.file import FileReadStream, FileWriteStream
1516

17+
logger = logging.getLogger(__name__)
18+
1619

1720
def get_windows_executable_command(command: str) -> str:
1821
"""
@@ -144,17 +147,27 @@ async def create_windows_process(
144147
if hasattr(subprocess, "CREATE_NO_WINDOW")
145148
else 0,
146149
)
150+
logger.info(f"Created Windows process using anyio.open_process for command: {command}")
147151
return process
148-
except NotImplementedError:
152+
except NotImplementedError as e:
153+
logger.info(
154+
f"anyio.open_process not available (NotImplementedError), "
155+
f"falling back to FallbackProcess for command: {command}. Error: {e}"
156+
)
149157
return _create_windows_fallback_process(command, args, env, errlog, cwd)
150-
except Exception:
158+
except Exception as e:
151159
# If anyio failed for other reasons, try without creation flags
160+
logger.info(
161+
f"anyio.open_process failed with creation flags, "
162+
f"retrying without flags for command: {command}. Error: {e}"
163+
)
152164
process = await anyio.open_process(
153165
[command, *args],
154166
env=env,
155167
stderr=errlog,
156168
cwd=cwd,
157169
)
170+
logger.info(f"Created Windows process using anyio.open_process (without flags) for command: {command}")
158171
return process
159172

160173

@@ -192,9 +205,13 @@ def _create_windows_fallback_process(
192205
bufsize=0, # Unbuffered output
193206
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
194207
)
208+
logger.info(f"Created FallbackProcess with creation flags for command: {command}")
195209
return FallbackProcess(popen_obj)
196-
except Exception:
210+
except Exception as e:
197211
# If creationflags failed, try without them
212+
logger.info(
213+
f"FallbackProcess with creation flags failed, " f"retrying without flags for command: {command}. Error: {e}"
214+
)
198215
popen_obj = subprocess.Popen(
199216
[command, *args],
200217
stdin=subprocess.PIPE,
@@ -204,4 +221,5 @@ def _create_windows_fallback_process(
204221
cwd=cwd,
205222
bufsize=0,
206223
)
224+
logger.info(f"Created FallbackProcess without creation flags for command: {command}")
207225
return FallbackProcess(popen_obj)

0 commit comments

Comments
 (0)