mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-21 13:04:18 +02:00
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:
parent
39a1e59bfd
commit
ea3bfb6772
3 changed files with 19 additions and 4 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"))
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue