feat: Use dynamic terminal width detection for Rich console output

Use shutil.get_terminal_size() with fallback to 100 characters instead of auto-detecting narrow widths. This provides better traceback readability while being more adaptive than fixed width settings.

Co-authored-by: William Easton <strawgate@users.noreply.github.com>
This commit is contained in:
marvin-context-protocol[bot] 2025-09-19 01:12:08 +00:00
commit ea3bfb6772
3 changed files with 19 additions and 4 deletions

View file

@ -26,12 +26,12 @@ from fastmcp.utilities.inspect import (
format_info,
inspect_fastmcp,
)
from fastmcp.utilities.logging import get_logger
from fastmcp.utilities.logging import get_logger, get_terminal_width
from fastmcp.utilities.mcp_server_config import MCPServerConfig
from fastmcp.utilities.mcp_server_config.v1.environments.uv import UVEnvironment
logger = get_logger("cli")
console = Console()
console = Console(width=get_terminal_width())
app = cyclopts.App(
name="fastmcp",

View file

@ -231,5 +231,7 @@ def log_server_banner(
expand=False,
)
console = Console(stderr=True)
from fastmcp.utilities.logging import get_terminal_width
console = Console(stderr=True, width=get_terminal_width())
console.print(Group("\n", panel, "\n"))

View file

@ -1,12 +1,25 @@
"""Logging utilities for FastMCP."""
import logging
import shutil
from typing import Any, Literal
from rich.console import Console
from rich.logging import RichHandler
def get_terminal_width() -> int:
"""Get terminal width using shutil.get_terminal_size() with fallback to 100.
Returns:
Terminal width in characters, defaulting to 100 if detection fails.
"""
try:
return shutil.get_terminal_size().columns
except (OSError, ValueError):
return 100
def get_logger(name: str) -> logging.Logger:
"""Get a logger nested under FastMCP namespace.
@ -39,7 +52,7 @@ def configure_logging(
# Only configure the FastMCP logger namespace
handler = RichHandler(
console=Console(stderr=True),
console=Console(stderr=True, width=get_terminal_width()),
rich_tracebacks=enable_rich_tracebacks,
**rich_kwargs,
)