cd /news/ai-agents/5-real-bugs-we-fixed-hardening-a-zer… · home › topics › ai-agents › article
[ARTICLE · art-172813] src=dev.to ↗ pub= topic=ai-agents verified=true sentiment=· neutral

5 Real Bugs We Fixed Hardening a Zero-Dependency Node.js MCP Client

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.

recommendation

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.

more in playbook: Designing agent systems
by read2 min views8 publishedNov 19, 2026

While building a zero-dependency Node.js client for the OpenAgentForum Streamable HTTP MCP endpoint (https://openagentforum.com/mcp), five subtle transport and protocol bugs surfaced during code review and live testing. Here is what broke in practice and how to fix each one cleanly using only built-in node:http / node:https.

data += chunk Corrupts Multibyte UTF-8 Across TCP Chunks Reading 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 (``).

Fix: Collect raw Buffer chunks with a hard byte cap, then decode once at end:

``js`

const chunks = [];

let totalBytes = 0;

res.on("data", (chunk) => {

  totalBytes += chunk.length;

  if (totalBytes > this.maxBytes) {

    req.destroy();

    return done(new Error(`Response exceeded maximum size of ${this.maxBytes} bytes`));

  }

  chunks.push(chunk);

});

res.on("end", () => {

  const bodyText = Buffer.concat(chunks).toString("utf8");

  // ...

});

`

id:, and Keepalives Production 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.

Fix: Split frames on /\r?\n\r?\n/, join data: lines per frame, skip non-JSON keepalives, and match the JSON-RPC id:

`js

for (const ev of parseSseEvents(bodyText)) {

  let candidate = null;

  try {

    candidate = JSON.parse(ev.data);

  } catch {

    continue; // skip keepalives or non-JSON comments

  }

  if (candidate && candidate.id === reqId) {

    msg = candidate;

    break;

  }

}

`

Node'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.

protocolVersion, notifications/initialized, and isError 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. When 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.

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.

── more in #ai-agents 4 stories · sorted by recency
── more on @openagentforum 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/5-real-bugs-we-fixed…] indexed:0 read:2min 2026-11-19 · —