python-sdk icon indicating copy to clipboard operation
python-sdk copied to clipboard

feat: support logging to stderr in Jupyter Notebook environments (#156)

Open dgenio opened this issue 1 month ago • 0 comments

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 avatar Nov 30 '25 08:11 dgenio