{"slug": "5-real-bugs-we-fixed-hardening-a-zero-dependency-node-js-mcp-client", "title": "5 Real Bugs We Fixed Hardening a Zero-Dependency Node.js MCP Client", "summary": "A developer building a zero-dependency Node.js client for the OpenAgentForum Streamable HTTP MCP endpoint documented five transport and protocol bugs found during code review and live testing, including UTF-8 corruption from concatenating string chunks, brittle SSE parsing that breaks on CRLF separators and keepalive events, and socket-inactivity timeouts that fail against slow-drip servers. The fixes collect raw Buffers with a byte cap and decode once, split SSE frames on blank-line boundaries while matching JSON-RPC ids, and pair Node's timeout with an explicit destroy deadline, alongside checks for protocolVersion, notifications/initialized, and result.isError.", "body_md": "While building a zero-dependency Node.js client for the [OpenAgentForum](https://openagentforum.com) Streamable HTTP MCP endpoint (`https://openagentforum.com/mcp`), five subtle transport and protocol bugs surfaced during code review and live testing.\n\nHere is what broke in practice and how to fix each one cleanly using only built-in `node:http` / `node:https`.\n\n`data += chunk` Corrupts Multibyte UTF-8 Across TCP Chunks\nReading an `http.IncomingMessage` with `let data = \"\"; res.on(\"data\", chunk => { data += chunk; })` calls `Buffer.prototype.toString()` on each chunk independently. If a 4-byte emoji or Cyrillic character is split across two TCP packets, both halves turn into `U+FFFD` (``).\n\n**Fix:** Collect raw `Buffer` chunks with a hard byte cap, then decode once at `end`:\n\n``js`\n\nconst chunks = [];\n\nlet totalBytes = 0;\n\nres.on(\"data\", (chunk) => {\n\n  totalBytes += chunk.length;\n\n  if (totalBytes > this.maxBytes) {\n\n    req.destroy();\n\n    return done(new Error(`Response exceeded maximum size of ${this.maxBytes} bytes`));\n\n  }\n\n  chunks.push(chunk);\n\n});\n\nres.on(\"end\", () => {\n\n  const bodyText = Buffer.concat(chunks).toString(\"utf8\");\n\n  // ...\n\n});\n\n`` ` ``\n\n`id:`, and Keepalives\nProduction Streamable HTTP MCP servers respond with `content-type: text/event-stream`. A naive `/^event:\\s*message\\s*\\ndata:\\s*/` regex breaks on `\\r\\n\\r\\n` frame separators, `id:` lines, multi-line `data:` blocks, or non-JSON keepalive events.\n\n**Fix:** Split frames on `/\\r?\\n\\r?\\n/`, join `data:` lines per frame, skip non-JSON keepalives, and match the JSON-RPC `id`:\n\n`` `js ``\n\nfor (const ev of parseSseEvents(bodyText)) {\n\n  let candidate = null;\n\n  try {\n\n    candidate = JSON.parse(ev.data);\n\n  } catch {\n\n    continue; // skip keepalives or non-JSON comments\n\n  }\n\n  if (candidate && candidate.id === reqId) {\n\n    msg = candidate;\n\n    break;\n\n  }\n\n}\n\n`\n\nNode's `timeout` option on `http.request` only fires on socket inactivity. A slow-drip server sending 1 byte per second keeps the socket alive indefinitely. Always pair it with an explicit `setTimeout(() => req.destroy(), this.timeoutMs)` deadline cleared on completion.\n\n`protocolVersion`, `notifications/initialized`, and `isError`\n`result.protocolVersion` matches your expected version (e.g. `2025-11-25`) and `result.capabilities.tools` is present.`202 Accepted` empty bodies.`result.isError === true`) instead of returning them as silent successes.\nWhen connecting to untrusted public MCP servers, pin the allowed origin/loopback fixture and enforce an explicit tool allowlist before `tools/call`, and never echo raw upstream response bodies in thrown error messages.\n\n*If you want your AI coding agent to automatically audit and harden MCP clients/servers or write deterministic loopback regression tests, check out my skills on [Agensi](https://www.agensi.io/creators/nmorestd).*", "url": "https://wpnews.pro/news/5-real-bugs-we-fixed-hardening-a-zero-dependency-node-js-mcp-client", "canonical_source": "https://dev.to/nmorestd/5-real-bugs-we-fixed-hardening-a-zero-dependency-nodejs-mcp-client-1k4c", "published_at": "2026-11-19 20:10:25+00:00", "updated_at": "2026-11-19 20:13:21.419893+00:00", "lang": "en", "topics": ["ai-agents", "agent-protocols", "developer-tools"], "entities": ["OpenAgentForum", "Node.js", "Agensi"], "also_reported_by": [], "takeaway": "Concatenating HTTP chunks as strings corrupts multibyte UTF-8 split across TCP packets, so collect raw Buffers with a byte cap and decode once at end, and split SSE frames on /\\r?\\n\\r?\\n/ while matching the JSON-RPC id instead of regexing a single data line.", "recommendation": {"advice": "When writing an MCP client, buffer raw chunks with a size cap, parse SSE frames on blank-line boundaries, and add an explicit destroy deadline alongside the socket timeout to defeat slow-drip servers.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/5-real-bugs-we-fixed-hardening-a-zero-dependency-node-js-mcp-client", "markdown": "https://wpnews.pro/news/5-real-bugs-we-fixed-hardening-a-zero-dependency-node-js-mcp-client.md", "text": "https://wpnews.pro/news/5-real-bugs-we-fixed-hardening-a-zero-dependency-node-js-mcp-client.txt", "jsonld": "https://wpnews.pro/news/5-real-bugs-we-fixed-hardening-a-zero-dependency-node-js-mcp-client.jsonld"}}