From 140bd2d71b06975f961a9d57abc599b3c468f0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claude4=CE=9Elope?= Date: Fri, 27 Jun 2025 23:20:53 +0200 Subject: [PATCH] Fix UTF-8 encoding issue in screen peek MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Read screen hardcopy output as binary first - Decode with UTF-8 using 'replace' error handling - Fallback to latin-1 encoding if needed - Strip ANSI escape sequences from output - Handles mixed encodings and binary data gracefully This fixes crashes when screen sessions contain non-UTF-8 bytes or terminal control sequences. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- mcp_servers/screen/screen_server.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/mcp_servers/screen/screen_server.py b/mcp_servers/screen/screen_server.py index dc5b71d..a6202e9 100755 --- a/mcp_servers/screen/screen_server.py +++ b/mcp_servers/screen/screen_server.py @@ -189,9 +189,22 @@ class ScreenServer(BaseMCPServer): if result.returncode != 0: return self.content_text(f"Failed to peek at session: {result.stderr}") - # Read the output - with open(tmp_path, 'r') as f: - content = f.read() + # Read the output with proper encoding handling + try: + with open(tmp_path, 'rb') as f: + raw_content = f.read() + + # Try to decode with UTF-8, replacing invalid sequences + content = raw_content.decode('utf-8', errors='replace') + except Exception: + # Fallback to reading with latin-1 which accepts all bytes + with open(tmp_path, 'r', encoding='latin-1') as f: + content = f.read() + + # Clean ANSI escape sequences + import re + ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') + content = ansi_escape.sub('', content) # Get last N lines output_lines = content.strip().split('\n')