Skip to content
mentoor

Topic 5 of 5 · 6 min read

Handling protocol errors, tool failures and timeouts

Goal: Classify an MCP failure from a log and decide whether to retry, show it to the model, ask the user or stop.

MCP has two kinds of failure, and each goes to a different place: a protocol error is for your code, a tool error is for the model.

Check yourself · 1 of 3

A tools/call response is a normal result with isError set to true and the text file not found. What should the client do?

Choose one answer

Why it matters

If every failure is shown to the model, it will try to "fix" a broken connection by calling again. If every failure is treated as an exception, the model never sees that its argument was wrong and cannot correct it. Sorting them correctly keeps both the model and the user informed.

How to do it

  1. Protocol errors. These are JSON-RPC error responses with a numeric code and a message: unknown method, unknown tool, a malformed request (-32602, invalid params), parse failure. The call never ran. Fix the request in code, or if the model chose a tool that does not exist, tell it the message once so it can pick another.
  2. Tool errors. The call reached the tool and failed. The response is a normal result with isError: true and a message in content: "rate limit reached", "file not found", "date must be in the future". The specification puts input validation failures here, because the model can fix them. Give these to the model, so it can change its plan or tell the user.
  3. Version errors. A 2026-07-28 server answers an unsupported version with UnsupportedProtocolVersionError and the list of versions it supports: retry once with a version you share, or stop if there is none. A legacy server answers initialize with the version it will use; if you cannot speak it, disconnect. Either way, report the versions involved.
  4. Broken streams. A dropped connection loses the request in flight. The 2026-07-28 revision removed stream resumption, so the client re-issues the call as a new request with a new id, but only if the call is safe to repeat. A read-only lookup is. Creating an invoice is not, unless you have a way to detect that it already happened. A legacy server may also answer 404 to an expired session id: start a new session with initialize.
  5. Timeouts and cancellation. Set a timeout on every request. When it fires, cancel: on Streamable HTTP, close that request's response stream; on stdio, send notifications/cancelled with the request id. Then ignore any late reply. Long tools can send progress notices, and a client may use them to extend the wait while keeping a maximum.

Before and after

// Before: one catch for everything
try {
  return await client.callTool({ name, arguments: args });
} catch (e) {
  return { isError: true, content: [{ type: "text", text: String(e) }] };
}
// After: tool errors go to the model, the rest are handled in code
try {
  const res = await client.callTool({ name, arguments: args }, undefined,
    { timeout: 30_000 });
  return res; // isError results are passed to the model as-is
} catch (e) {
  if (e instanceof McpError && e.code === ErrorCode.InvalidParams) {
    return toolResultError(`Request rejected: ${e.message}`);
  }
  throw e; // transport, timeout, version: retry or stop in the caller
}

What goes wrong

  • Blind retries. A POST that created an order is sent twice after a broken stream.
  • Hiding tool errors. The user sees "Error", and the model never learns the file was missing.
  • No timeout. One hung server freezes the whole agent turn.

Examples

Reading two logs

Log 1: {"error":{"code":-32602,"message":"Unknown tool: serch"}}. The model misspelled a name. Return the message to the model once so it can correct it.

Log 2: {"result":{"isError":true,"content":[{"type":"text","text":"Rate limit: retry in 20 s"}]}}. The tool ran. Give the text to the model, and it can wait or tell the user.

Retry decision

The stream breaks while calling get_invoice. The client reconnects and repeats the call, because it only reads. The stream breaks while calling send_invoice. The client does not repeat it. It asks get_invoice_status first and tells the user if it cannot tell whether the email went out.

Key points

  • A JSON-RPC error means the request itself failed; an isError result means the tool ran and reported a failure, including invalid arguments.
  • Give isError content to the model so it can adjust, and keep transport faults in client code.
  • A version error is fixed only by a version both sides support: retry once with one from the server's list, or stop.
  • After a broken stream, re-issue the call as a new request only when it is safe to repeat.
  • Set a timeout on every request; when it fires, close the response stream on HTTP or send notifications/cancelled on stdio.

Common mistakes

  • Catching every error and returning it to the model as a tool result.
  • Retrying a failed side-effecting call after a dropped connection without checking whether it ran.
  • Waiting with no timeout on a server that has stopped responding.
  • Retrying an unsupported protocol version in a loop.

Check yourself · 2 of 3

A legacy server (revision 2025-11-25) answers initialize with a protocol version the client does not support. What is the right action?

Choose one answer

Check yourself · 3 of 3

A request to a stdio server times out in the client. What should the client send to the server?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic