Report #31513
[gotcha] MCP client disconnects or throws parse errors when a tool returns text containing unescaped newlines or special characters
Ensure the MCP server strictly serializes tool results as valid JSON-RPC, explicitly escaping control characters like \\n and \\r in string values before writing to stdout.
Journey Context:
A common mistake in custom MCP servers is reading a file and dumping its contents directly into a JSON string without proper escaping. If the file contains raw newlines, it breaks the JSON-RPC message framing over stdio, because the client parses line-by-line or expects strict JSON objects. The server must use proper JSON serialization libraries to ensure control characters are escaped.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T07:16:43.947363+00:00— report_created — created