Skip to content

Conversation

@dgenio
Copy link

@dgenio dgenio commented Nov 30, 2025

Summary

Fixes #156 - Adds support for displaying stderr output in Jupyter Notebook environments.

Changes

  • Added Jupyter/IPython detection using IPython.get_ipython()
  • Implemented async stderr reader that reads stderr asynchronously (similar to stdout)
  • Added IPython display integration for colored stderr output in Jupyter notebooks
  • Updated process creation to pipe stderr instead of redirecting (enables async reading)
  • Updated Windows process creation and fallback to support piped stderr
  • Updated FallbackProcess to wrap stderr in async-compatible streams

Design Decisions

  • Backward compatibility: errlog parameter is kept but behavior adapts based on environment
  • Error handling: Non-critical errors are logged but don't stop execution
  • Performance: Matches stdout_reader behavior (line-by-line buffering)
  • Platform support: Works on both Unix and Windows (including fallback path)

Testing

  • Added comprehensive tests for stderr capture, Jupyter detection, and display integration
  • All existing tests pass
  • Linting and type checking pass

Behavior

  • In Jupyter: stderr is displayed using IPython's display system with red HTML formatting
  • Outside Jupyter: stderr is printed to the provided errlog stream (defaults to sys.stderr)
  • Fallback: If IPython display fails, falls back to regular print

This enables proper debugging of MCP servers in Jupyter notebook environments, addressing the issue where server crashes or command errors were invisible.

dgenio and others added 9 commits November 30, 2025 07:11
- Add _is_jupyter_notebook() to detect Jupyter/IPython environments
- Add _print_stderr() to handle stderr output with IPython display support
- Mock builtins.__import__ instead of patching IPython.display directly

- Handle 'from IPython.display import HTML, display' import pattern

- Tests now work without IPython installed in test environment
- Add test for _is_jupyter_notebook with valid IPython instances

- Add test for stderr_reader exception handling

- Improve test coverage to reach 100%
- Store original __import__ before patching to avoid recursion

- Fix test_stderr_reader_no_stderr to not print to stdout

- Apply ruff formatting fixes
@dgenio dgenio closed this Nov 30, 2025
@dgenio dgenio deleted the feature/156-jupyter-stderr-support branch November 30, 2025 08:14
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