The error "invalid trailing data at the end of stream" during MCP initialization in Google Antigravity (unlike in Cursor and other tools) is related to differences in handling line breaks on Windows.
The problem is as follows:
- On Windows, standard line break characters use CRLF (\r\n) instead of LF (\n).
- When sending JSON-RPC messages via stdin/stdout, Google Antigravity validates the protocol format more strictly.
- The \r (carriage return) character from the \r\n pair is interpreted as "trailing data" — a legal end of the message, but unexpectedly followed by extra characters[3].
Cursor uses softer MCP protocol validation and automatically normalizes line break characters, whereas Antigravity applies a stricter check for compliance with the MCP specification.
Open stdout in binary mode to control line break characters:
import sys
import os
import json
# Windows: set binary mode for stdout
if sys.platform == "win32":
import msvcrt
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Function to send JSON-RPC message
def send_message(message):
# Use only LF (\n), without CRLF (\r\n)
json_str = json.dumps(message)
sys.stdout.write(json_str + '\n')
sys.stdout.flush()
# Function to read incoming messages
def read_message():
# Read until LF, ignoring CR if present
line = sys.stdin.readline()
if line.endswith('\r\n'):
line = line[:-2] + '\n' # Replace CRLF with LF
elif line.endswith('\n'):
line = line # Keep LF as is
return json.loads(line.strip())Alternative approach with more explicit control:
import sys
import os
# Windows: use fdopen for explicit control
if sys.platform == "win32":
import io
sys.stdout = io.open(sys.stdout.fileno(), 'w', encoding='utf-8',
newline='', closefd=False)// Ensure only LF is used
process.stdout.write(JSON.stringify(message) + '\n');
process.stdout.flush = () => {}; // flush is called automatically
// OR use streams explicitly
const { createWriteStream } = require('fs');
const stdout = createWriteStream(null, { fd: 1, newline: 'lf' });Ensure the config uses absolute paths and correct launch parameters:
{
"mcpServers": {
"your-mcp-server": {
"command": "C:\\full\\path\\to\\python.exe",
"args": [
"C:\\full\\path\\to\\server.py"
],
"env": {
"PYTHONUNBUFFERED": "1"
}
}
}
}Critical: add PYTHONUNBUFFERED=1 for Python servers to disable stdout buffering.
For the MCP protocol:
- stdout — JSON-RPC messages only, nothing else
- stderr — all logging, debugging, errors
import sys
# ✅ CORRECT
sys.stderr.write(f"Debug: processing request\n")
sys.stderr.flush()
# ❌ INCORRECT
print(f"Debug: processing request") # This will pollute stdoutMake sure you are using the latest version of Antigravity, as Google may release fixes for this issue.
- ✅ Use absolute paths in
mcp_config.json - ✅ For Python: set binary mode and PYTHONUNBUFFERED=1
- ✅ All logging only to stderr
- ✅ Send JSON-RPC messages with a single LF (
\n), no CR - ✅ Ensure the server does not output any welcome messages on startup
- ✅ Use
.flush()after every message in stdout - ✅ Update Antigravity to the latest version