Skip to content

Instantly share code, notes, and snippets.

@romanilyin
Last active November 28, 2025 14:51
Show Gist options
  • Select an option

  • Save romanilyin/78bcf3669f6f37385ac4e720b17141d9 to your computer and use it in GitHub Desktop.

Select an option

Save romanilyin/78bcf3669f6f37385ac4e720b17141d9 to your computer and use it in GitHub Desktop.
Issue with Google Antigravity + MCP "invalid trailing data at the end of stream"

Issue with Google Antigravity + MCP "invalid trailing data at the end of stream"

Root Cause

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].

Why it works in Cursor

Cursor uses softer MCP protocol validation and automatically normalizes line break characters, whereas Antigravity applies a stricter check for compliance with the MCP specification.

Solutions

1. Python Solution (for Python-based MCP servers)

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)

2. Node.js Solution (for Node-based MCP servers)

// 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' });

3. mcp_config.json Configuration

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.

4. In MCP Server Code: Always Write to stderr for Logging

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 stdout

5. Update Antigravity

Make sure you are using the latest version of Antigravity, as Google may release fixes for this issue.

Recommended Checklist

  1. ✅ Use absolute paths in mcp_config.json
  2. ✅ For Python: set binary mode and PYTHONUNBUFFERED=1
  3. ✅ All logging only to stderr
  4. ✅ Send JSON-RPC messages with a single LF (\n), no CR
  5. ✅ Ensure the server does not output any welcome messages on startup
  6. ✅ Use .flush() after every message in stdout
  7. ✅ Update Antigravity to the latest version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment