Skip to content

Commit 38b04ff

Browse files
authored
fix: separate console streams for proper stdout/stderr routing (#255)
Create separate Console instances for regular CLI output (stdout) and error handling (stderr). This ensures: - Logo, help, and normal UI go to stdout (console) - Error tracebacks and error messages go to stderr (err_console)
1 parent 6d3728f commit 38b04ff

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/mcpm/cli.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from rich.console import Console
99
from rich.traceback import Traceback
10+
from rich.traceback import install as install_rich_traceback
1011

1112
from mcpm.clients.client_config import ClientConfigManager
1213
from mcpm.commands import (
@@ -31,12 +32,17 @@
3132
from mcpm.utils.logging_config import setup_logging
3233
from mcpm.utils.rich_click_config import click, get_header_text
3334

34-
console = Console()
35+
console = Console() # stdout for regular CLI output
36+
err_console = Console(stderr=True) # stderr for errors/tracebacks
3537
client_config_manager = ClientConfigManager()
3638

3739
# Setup Rich logging early - this runs when the module is imported
3840
setup_logging()
3941

42+
# Install Rich's global exception handler to use stderr instead of stdout
43+
# This prevents Rich/rich-gradient from routing tracebacks to stdout
44+
install_rich_traceback(console=err_console, show_locals=True)
45+
4046
# Custom context settings to handle main command help specially
4147
CONTEXT_SETTINGS: Dict[str, Any] = dict(help_option_names=[])
4248

@@ -53,9 +59,9 @@ def wrapper(*args, **kwargs):
5359
try:
5460
return func(*args, **kwargs)
5561
except Exception:
56-
console.print(Traceback(show_locals=True))
57-
console.print("[bold red]An unexpected error occurred.[/bold red]")
58-
console.print(
62+
err_console.print(Traceback(show_locals=True))
63+
err_console.print("[bold red]An unexpected error occurred.[/bold red]")
64+
err_console.print(
5965
"Please report this issue on our GitHub repository: "
6066
"[link=https://github.com/pathintegral-institute/mcpm.sh/issues]https://github.com/pathintegral-institute/mcpm.sh/issues[/link]"
6167
)

0 commit comments

Comments
 (0)