Skip to content

Conversation

@BabyChrist666
Copy link

Summary

Fixes #156

In Jupyter notebook environments, passing sys.stderr directly to subprocess doesn't work as expected - the stderr output is lost and users have no visibility into server errors or crashes. This makes debugging extremely difficult as the notebook cell just hangs with no feedback.

Changes

  • Added _is_jupyter_environment() function to detect Jupyter/IPython notebook environments
  • In Jupyter mode, stderr is captured as a pipe and read asynchronously
  • Stderr output is printed with red ANSI color for visibility in notebooks
  • In non-Jupyter environments, behavior remains completely unchanged (direct stderr passthrough)

How It Works

  1. On client initialization, we detect if running in Jupyter/IPython
  2. If in Jupyter, we set capture_stderr=True which pipes stderr instead of redirecting
  3. An async stderr_reader task reads the pipe and prints to the notebook output
  4. Red ANSI coloring makes errors stand out in the notebook

Technical Details

  • Detection checks for IPKernelApp in config or ZMQInteractiveShell class name
  • Falls back gracefully if IPython is not installed
  • Windows compatibility maintained via FallbackProcess stderr wrapping

Test plan

  • Added TestJupyterStderrSupport test class with 4 tests
  • Test Jupyter detection returns False in normal Python
  • Test graceful handling when IPython not installed
  • Test stderr capture doesn't block the client
  • Test continuous stderr output is handled properly
  • All existing stdio tests pass

🤖 Generated with Claude Code

Fixes modelcontextprotocol#156

In Jupyter notebook environments, passing sys.stderr directly to subprocess
doesn't work as expected - the stderr output is lost and users have no
visibility into server errors or crashes.

This change:
- Adds _is_jupyter_environment() to detect when running in Jupyter/IPython
- In Jupyter mode, captures stderr as a pipe and reads it asynchronously
- Prints stderr output with red ANSI color for visibility in notebooks
- In non-Jupyter environments, behavior remains unchanged (direct stderr)

The fix ensures that MCP server error messages and crash logs are visible
in Jupyter notebooks, making debugging much easier.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support logging to stderr in Jupyter Notebook Environments.

1 participant