Fix UTF-8 encoding issue in screen peek

- 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 <noreply@anthropic.com>
This commit is contained in:
Claude4Ξlope 2025-06-27 23:20:53 +02:00
parent 9a22aa7714
commit 140bd2d71b
1 changed files with 16 additions and 3 deletions

View File

@ -189,10 +189,23 @@ 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:
# 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')
if len(output_lines) > lines: