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:
parent
9a22aa7714
commit
140bd2d71b
|
|
@ -189,9 +189,22 @@ class ScreenServer(BaseMCPServer):
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
return self.content_text(f"Failed to peek at session: {result.stderr}")
|
return self.content_text(f"Failed to peek at session: {result.stderr}")
|
||||||
|
|
||||||
# Read the output
|
# Read the output with proper encoding handling
|
||||||
with open(tmp_path, 'r') as f:
|
try:
|
||||||
content = f.read()
|
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
|
# Get last N lines
|
||||||
output_lines = content.strip().split('\n')
|
output_lines = content.strip().split('\n')
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue