From b5d7270feaabcae27ff6743483946b8ec4a9b5fb Mon Sep 17 00:00:00 2001 From: Bortlesboat Date: Sun, 15 Mar 2026 17:13:07 -0400 Subject: [PATCH] fix: suppress platform string output to stdout on macOS --- main.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/main.py b/main.py index 8fb97c5..adbd69d 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import io import argparse import logging import os @@ -6,6 +7,13 @@ import sys from importlib import metadata, import_module from dotenv import load_dotenv +# Prevent any stray output (e.g. platform identifiers like "darwin" on macOS) +# from corrupting the MCP JSON-RPC handshake on stdout. We capture anything +# written to stdout during module-level initialisation and replay it to stderr +# so that diagnostic information is not lost. +_original_stdout = sys.stdout +sys.stdout = io.StringIO() + # Check for CLI mode early - before loading oauth_config # CLI mode requires OAuth 2.0 since there's no MCP session context _CLI_MODE = "--cli" in sys.argv @@ -117,12 +125,21 @@ def narrow_permissions_to_services( } +def _restore_stdout(): + """Restore the real stdout and replay any captured output to stderr.""" + captured = sys.stdout.getvalue() + sys.stdout = _original_stdout + if captured: + print(captured, end="", file=sys.stderr) + + def main(): """ Main entry point for the Google Workspace MCP server. Uses FastMCP's native streamable-http transport. Supports CLI mode for direct tool invocation without running the server. """ + _restore_stdout() # Check if CLI mode is enabled - suppress startup messages if _CLI_MODE: # Suppress logging output in CLI mode for clean output