The run exited 0 and nothing was saved: what Claude Code and Codex do when an MCP server is slow or fails mid-run
A scheduled agent calls a memory server that answers 500, hangs or drops the connection, and the job is still green. What Claude Code, Codex and Cursor document about startup timeouts, tool-call timeouts and retries, what we measured against a local fault-injection server in 23 runs, and the timeout, the gate and the three lines of instruction that turn a silent miss into a failed job or a recovered write.
A nightly job runs an agent over the day's merged pull requests and asks it to save the decisions to the team's memory server. At 02:10 the server has a bad minute: a deploy, a cold start, a database that answers late. The agent's write comes back with an error. The agent says so, in one polite sentence, to nobody. The job exits 0 and the dashboard is green.
The next morning a teammate's agent searches the memory for yesterday's decision and finds nothing. Nobody connects that to the job, because the job did not fail.
This guide shows what Claude Code, Codex and Cursor document about a server that is slow to start, slow to answer or failing mid-run, what we measured when a local server misbehaved on purpose, and ends with a timeout, a gate script and three lines of instruction you can copy.
What each harness documents
| Claude Code | Codex | Cursor | |
|---|---|---|---|
| The server is slow to start | MCP_TIMEOUT: "Timeout in milliseconds for MCP server startup (default: 30000, or 30 seconds)" | startup_timeout_sec: "Timeout (seconds) for the server to start. Default: 10." Optional servers get mcp_optional_startup_grace_ms while Codex builds the first tool list: "It defaults to 1000 milliseconds" | Not stated on the pages we read |
| The first connection fails | For an HTTP or SSE server, after "a 5xx response, a connection refused, or a timeout, Claude Code retries up to three times. If the connection still fails, Claude Code marks the server as failed" | "Set required = true to make startup fail if this enabled server isn't ready" | "Cursor shows an error message in chat" |
| A tool call takes too long | A per-server timeout in milliseconds, "a hard wall-clock limit per tool call". Without it, MCP_TOOL_TIMEOUT, whose default is "about 28 hours". A call that sends "no response and no progress notification for the idle window" is aborted, and that window "defaults to five minutes" for HTTP servers and 30 minutes for stdio ones. An HTTP server also has a per-request timer of at least 60 seconds that "covers each request through to the server's first response byte" | tool_timeout_sec: "Timeout (seconds) for the server to run a tool. Default: 60." | Not stated on the pages we read |
| A tool call fails | With a headersHelper, on 401 Unauthorized or 403 Forbidden Claude Code "re-runs the helper", "reconnects with the fresh headers, and retries the call once". We found no sentence about retrying a tool call after a 5xx or a 429. The tool-list requests are a separate case: retried "up to three times", and "It doesn't retry authentication errors, 4xx responses, or request timeouts" | Not stated on the page we read | "The tool call is marked as failed". "You can retry the operation or check logs for details" |
| The connection drops mid-session | Claude Code tries to reconnect "up to five times with exponential backoff". In claude -p: "If all five fail, the run continues without the server's tools", and "To reconnect it in claude -p, start the run again" | Not stated on the page we read | "Other MCP servers continue working normally" |
Read the table by its last words. "Marks the server as failed", "the run continues without the server's tools", "The tool call is marked as failed": each one describes a session that goes on. That is the right default for a person at a keyboard, who sees the message and decides. A job has nobody to see it.
What we measured
A lab on one machine, on November 30, 2026: Claude Code 2.1.329 with the model claude-opus-5-5 and codex-cli 0.156.1, against a local MCP server over HTTP on 127.0.0.1 with two tools, memory_store and memory_search. The server stores nothing. It logs every request and misbehaves on demand: a slow handshake, an HTTP error on the tool call, a call it never answers, a closed socket. One run per case, with dummy data.
Every Claude Code row is this command, with the server's fault chosen in the config file:
claude -p 'Use the memory_store tool to save this note: "Deploy freeze starts on Dec 18". Then answer in one line: was the note saved?' \
--mcp-config ../cfg.json --strict-mcp-config \
--allowedTools "mcp__memory__memory_store,mcp__memory__memory_search" \
--output-format stream-json --verbose --max-turns 10The server was connected at the start of every run in this first table. What changed is what it did with the write.
| What the server did with the write | Exit code | Run time | Write requests the server received | What the model was told |
|---|---|---|---|---|
Answered stored: note-0001 | 0 | 5.4 s | 1 | stored: note-0001 |
| Answered after 10 seconds | 0 | 14.9 s | 1 | stored: note-0001 |
| HTTP 500 | 0 | 7.9 s | 1 | Error POSTing to endpoint: {"error":"internal error"} |
| HTTP 500 once, fine from the second call on | 0 | 6.3 s | 1 | the same error. No second call was made |
HTTP 429 with Retry-After: 30 | 0 | 5.5 s | 1 | Error POSTing to endpoint: {"error":"rate limited"} |
| HTTP 401 | 0 | 14.0 s | 6, for 3 attempts by the model | MCP server "memory" session expired |
| Closed the socket without answering | 0 | 8.3 s | 1 | ECONNRESET: The socket connection was closed unexpectedly. |
A tool result with isError: true | 0 | 9.5 s | 1 | write rejected: storage quota exceeded |
| Never answered, default settings | 0 | 308.1 s | 1 | MCP server "memory" tool "memory_store" sent no response or progress for 300s; aborting. |
Never answered, "timeout": 5000 on the server entry | 0 | 18.3 s | 1 | MCP server "memory" tool "memory_store" timed out after 5s |
The model's final answer, word for word, in four of the rows:
HTTP 500
No, the note wasn't saved: `memory_store` failed with a server-side "internal error".
HTTP 500 once, fine from the second call on
No, the note wasn't saved: `memory_store` failed with an "internal error" from the memory server, and I didn't retry because the save might have gone through anyway and a retry could create a duplicate.
Closed the socket without answering
No, I can't confirm it was saved: `memory_store` failed with ECONNRESET, and the search I ran to check failed with the same error, so the note may or may not be in memory and I didn't retry to avoid a duplicate.
Never answered, "timeout": 5000
Unconfirmed: the save call timed out after 5s and the check search timed out too, so I can't tell if it was stored (I didn't retry, to avoid saving it twice).The second table is the handshake. The server answers initialize late and is healthy after that.
The server answers initialize after | Setting | Exit code | Run time | Status in the init event | The note |
|---|---|---|---|---|---|
| 20 s | defaults | 0 | 24.6 s | connected | saved |
| 45 s | defaults | 0 | 28.1 s | failed | not saved: the run had no memory tools |
| 20 s | MCP_TIMEOUT=10000 | 0 | 8.4 s | failed | not saved: the run had no memory tools |
| 45 s | MCP_TIMEOUT=60000 | 0 | 50.5 s | connected | saved |
In the two failed rows the model answered in one turn. For the 45-second server:
The note wasn't saved: the memory server failed to connect (connection timeout), so the `memory_store` tool isn't available in this session. Reconnect the memory server and retry, and I'll save "Deploy freeze starts on Dec 18".Codex, with the same server in ~/.codex/config.toml. Our Codex was not signed in, so a run that got past startup ended about 15 seconds later on an authentication error from the model API. What these rows show is the start of the MCP connection and nothing after it.
The server answers initialize after | Config | What happened at start |
|---|---|---|
| at once | required = true | Connected. The run ended on the model API's error at 15.9 s |
| 15 s | no required | The run ended at 15.8 s, the same as with a fast server: it did not wait. The handshake finished in the background |
| 15 s | required = true | Waited for the handshake, then went on and ended at 29.7 s. It did not fail at the documented 10 seconds |
| 40 s | required = true | Stopped at 30.6 s, before any model call: required MCP servers failed to initialize: memory: timed out handshaking with MCP server after 30s |
| 15 s | required = true, startup_timeout_sec = 5 | Stopped at 5.8 s, before any model call: required MCP servers failed to initialize: memory: timed out handshaking with MCP server after 5s |
Six things the lab shows
1. The exit code was 0 in every Claude Code run where the note was not saved. Ten runs: an HTTP error, a rejected write, a closed socket, a timeout, a server that never connected. The model said the truth in its last sentence each time, and claude -p exited 0 each time. A job that checks only the exit code reports all ten as a success. One exception shows how fragile even that is: with --max-turns 4, the 401 and closed-socket runs spent their turns on retries and ended in error_max_turns with exit 1. That was the turn cap, not the failure, and with ten turns both exited 0.
2. A slow start becomes a run without the tools. With defaults, a server that needed 20 seconds was waited for, and one that needed 45 was not: the run went on with status: "failed" and no memory tools at all. The model cannot retry a tool it does not have. The same server with MCP_TIMEOUT=60000 connected and the note was saved. A memory server behind a container that scales to zero is exactly this case on the first run of the night.
3. A silent server held one call for five minutes. With default settings the write that never got an answer was aborted after 300 seconds, with a message that names the fix. With "timeout": 5000 on the server entry, the same call ended in 5 seconds, and the server's log shows a notifications/cancelled from the client 5.0 seconds after the call. Two notes. The documented 60-second per-request timer did not end the call in our run: the 300-second idle window did. And 300 seconds is per call: four silent calls in one job are twenty minutes of waiting.
4. Nothing retried the write for us. On a 500, a 429 and a closed socket the server received exactly one request. The Retry-After: 30 header on the 429 was not waited for. On the server that fails once and then works, one more attempt would have saved the note, and nothing made it. The 401 was the exception: on each one Claude Code opened a new session with the server and sent the call again once, so each attempt by the model reached the server twice, and the model was told the session had expired.
5. After a timeout or a closed socket, "not saved" really means "unknown". A server can answer 500 before it wrote or after. A timeout is worse: the request may have been processed and the answer lost. In four runs the model said why it did not send the write again: a retry could save the note twice. One of those was the 500 that a second attempt would have fixed. That is a careful default, and it is also how a one-minute outage becomes a missing note. Whether a retry is safe is a property of the write, not of the model, and the model does not know it unless you say so.
6. Codex did not wait for an optional server, and waited longer than documented for a required one. Without required = true, the run went on while the 15-second handshake was still open, as the 1-second grace on its page says. With required = true, a 15-second handshake was waited for and a 40-second one failed "after 30s", where the page gives a default of 10. With startup_timeout_sec = 5 it failed after 5. If the number matters to you, set it and do not rely on the default.
Put a ceiling on a silent call
Add timeout, in milliseconds, to the server entry. It is the one setting that turned a 308-second run into an 18-second one:
{
"mcpServers": {
"memory": {
"type": "http",
"url": "https://memory.example.com/mcp",
"headers": { "Authorization": "Bearer ${TEAM_MEMORY_TOKEN}" },
"timeout": 30000
}
}
}Three things from the documentation before you pick the number. It is a hard limit: "progress notifications from the server don't extend it". Values below 1000 are ignored. And it applies to every tool of that server, so set it above the slowest call you expect to succeed, such as a large search, and not above the longest wait you are willing to pay per call. The lab server answered a write after 10 seconds without any setting and the run was fine. Slow is not the problem; silent is.
The timeout ends the wait. It does not tell you whether the write happened. That is the next three sections.
Decide what a slow start means
In our claude -p runs a server that missed the startup window was gone for the whole run, and the documentation's answer for a failed server there is to "start the run again". So choose on purpose.
- The job is useless without the server. Give the server time (
MCP_TIMEOUT=60000connected to our 45-second server) and fail the job when theinitevent does not sayconnected. The gate below does that. - The job is still useful without it. Keep the default or lower it (with
MCP_TIMEOUT=10000the run gave up on our 20-second server and was over in 8.4 seconds), and write the missing memory into the job's output where a person will see it.
Do not leave it at "whatever happens". With defaults, the difference between a saved note and a run with no memory tools was a handshake of 20 seconds or 45.
Codex: required, and both timeouts
[mcp_servers.memory]
url = "https://memory.example.com/mcp"
bearer_token_env_var = "TEAM_MEMORY_TOKEN"
required = true
startup_timeout_sec = 20
tool_timeout_sec = 30required = true is what turns a server that did not come up into a run that does not start: exit 1 and a line with the server's name and the reason. Without it, our run did not wait for a server that needed 15 seconds. startup_timeout_sec was obeyed when we set it. tool_timeout_sec is documented with a default of 60 seconds; we could not measure it, because a tool call needs a signed-in Codex.
A gate that reads the stream
The exit code will not tell you, so read the run. --output-format stream-json --verbose writes one JSON event per line: the init event carries each server's status, every tool call has an id, and every tool result says whether it was an error. This script fails the job when the server was not connected at the start, or when the last call to one of its tools ended in an error:
#!/bin/bash
# check-memory-run.sh <run.jsonl> [server-name]
# Fails when the server was not connected at start, or when the last call to one of its tools ended in an error.
# Input: the output of `claude -p ... --output-format stream-json --verbose`.
run="$1"; server="${2:-memory}"
jq -s -r --arg s "$server" '
([ .[] | select(.type=="assistant") | .message.content[]? | select(.type=="tool_use") | {(.id): .name} ] | add // {}) as $names
| ([ .[] | select(.type=="system" and .subtype=="init") | .mcp_servers[]? | select(.name==$s and .status=="connected") ] | length) as $up
| [ .[] | select(.type=="user") | .message.content[]? | select(.type=="tool_result")
| {tool: ($names[.tool_use_id] // "?"), failed: (.is_error == true)}
| select(.tool | startswith("mcp__" + $s + "__")) ] as $calls
| ($calls | group_by(.tool) | map(select(last.failed) | first.tool)) as $unrecovered
| ($calls | map(select(.failed)) | length) as $errors
| if $up != 1 then "FAIL: \($s) was not connected when the run started" | halt_error(2)
elif ($unrecovered | length) > 0 then "FAIL: the last call to \($unrecovered | join(", ")) returned an error (\($errors) of \($calls | length) calls to \($s) failed)\n" | halt_error(3)
else "ok: \($s) connected, \($calls | length) calls, \($errors) failed and recovered" end
' "$run"In the job:
claude -p "$(cat prompt.md)" --output-format stream-json --verbose > run.jsonl
./check-memory-run.sh run.jsonl memoryWe ran it over the output of every Claude Code run in the lab, with jq 1.7. Claude Code's own exit code was 0 in all of them.
| Run | What the gate printed | Gate exit |
|---|---|---|
| Note saved (four runs) | ok: memory connected, 1 calls, 0 failed and recovered | 0 |
| HTTP 500, HTTP 500 once, HTTP 429, rejected write, silent call | FAIL: the last call to mcp__memory__memory_store returned an error (1 of 1 calls to memory failed) | 3 |
| Closed socket, 5-second timeout | FAIL: the last call to mcp__memory__memory_search, mcp__memory__memory_store returned an error (2 of 2 calls to memory failed) | 3 |
| HTTP 401 | the same line, with 4 of 4 calls | 3 |
| Server not connected (two runs) | FAIL: memory was not connected when the run started | 2 |
| First write failed, the retry worked (next section) | ok: memory connected, 3 calls, 1 failed and recovered | 0 |
What it does not catch: a run where the model never called the server at all, and a server that answers success and stores nothing. It reads claude -p output only; we did not write the Codex equivalent.
Tell the agent what to do when a write fails
In finding 5 the model would not retry, because it could not know whether a retry was safe. You can tell it. We put three lines in the lab repository's CLAUDE.md:
## When a memory write fails
If a call to `memory_store` returns an error or times out, the note may or may not have been saved.
Search for the note with `memory_search` first. If the search does not find it, call `memory_store` once more.
End your answer with one line: `MEMORY: saved` or `MEMORY: NOT SAVED (<reason>)`.Then we ran the "500 once, fine from the second call on" case again. The server's log shows a write, a search one second later, and a second write four seconds after that. The final answer:
No. The first save failed with an "internal error". A search found no copy of the note, so I saved it again, and that worked (id `note-0001`).
MEMORY: savedOne run, one model, one fault. It shows the instruction can be followed, not that it always will be. That is why the gate stays: the instruction raises the odds that the write lands, and the gate tells you when it still did not. The last line gives a human reader of the job's log something to grep, and the gate does not depend on it.
Codex reads AGENTS.md for instructions like these; we did not run it there.
Make the write safe to repeat
Search-then-retry is a patch over a write that cannot be repeated safely. If you choose or build the memory server, the better fix is in the write itself:
- Write by key when the thing has a name. "The deploy freeze date" is one fact with one current value. A write addressed by key that replaces or supersedes the previous value can be sent twice and leave the same state. A write that appends a new note every time cannot.
- Know which of your server's writes are which. Read its documentation for words like idempotent, upsert or deduplicate. If it does not say, assume a retry creates a second copy and keep the search step.
- Return an id. A write that answers with the stored id gives the agent, and your gate, something to verify with a read.
The same setup with ValorBrain
ValorBrain is a memory server for teams of agents, reached over MCP at one URL. The two files from this guide with it:
{
"mcpServers": {
"valorbrain": {
"type": "http",
"url": "https://mcpbrain.valor.digital/mcp",
"headers": { "Authorization": "Bearer ${VALORBRAIN_MCP_TOKEN}" },
"timeout": 30000
}
}
}[mcp_servers.valorbrain]
url = "https://mcpbrain.valor.digital/mcp"
bearer_token_env_var = "VALORBRAIN_MCP_TOKEN"
required = true
startup_timeout_sec = 20
tool_timeout_sec = 30The gate is ./check-memory-run.sh run.jsonl valorbrain. The numbers are the example's, not a measured ValorBrain latency: pick yours from your own slowest search.
What the product does about the rest of this guide:
- A busy credential check has its own code, and the code says what to do. When a dependency fails for a moment while your token is being checked, the MCP endpoint answers 503
temporarily_unavailablewith aRetry-After, and the error catalog says: "Wait and retry with the same token; do not reconnect or replace the credential." The catalog's first rule is "Match on the code, not the text." By finding 4, the harness will not do that waiting for you: the retry belongs in the agent's instructions or in your job. - A dated fact is written by key.
upsert_keyed_factwrites a snapshot under afact_keyand a date, and a reader gets the latest value for the key. The server keeps one row per key and date, so the same call sent twice updates that row and adds nothing. That makes it the write to use for anything with a name: a date, a limit, an owner, a status. See Correctability. - There is an exact-match search for the check before a retry.
memory_greplooks for an exact string, id, date or key, which is the step the three lines above ask for. The list is in MCP tools. - A repeated
memory_storefolds into the first for 30 minutes. When the same account sendsmemory_storewith the same type, title, text and tags to the same collection within 30 minutes of the first call, the server keeps one document and answersAction: deduplicatedwith the first one's docid. Capitalization and spacing do not count. A retry that repeats the call exactly, soon after a timeout, cannot leave two copies. A retry where the model rewrote the note is a new document, and so is one after 30 minutes: that is where the search step earns its place. - A liveness check with no credential.
curl -sS https://valorbrain-api.valor.digital/healthanswered{"status":"ok","version":"1.1.0",…}in 0.46 seconds from our machine on November 30, 2026. It is the API host's liveness, not proof that your token works or that the MCP endpoint will answer your call, so use it as a cheap first step in a job and not as the gate.
What ValorBrain does not do
- It does not make your harness retry. A
Retry-Afteris a request to the client. In the lab, Claude Code did not wait for one on a tool call. - It does not change your run's exit code. A failed write reaches the model as a tool error, like any server's. The gate is still yours to run.
- It does not merge notes that say the same thing in other words. The 30-minute fold compares the text, not the meaning. A model that rephrases the note on its second try leaves two documents, so keep the search before the retry, and write anything with a name by key.
- It does not set your timeouts.
timeout,MCP_TIMEOUT,startup_timeout_secandtool_timeout_seclive in your config and your environment. - What we ran and what we did not. Every row in the three lab tables, the gate over fifteen Claude Code outputs and the
CLAUDE.mdrerun, on one Linux machine, against a local server with dummy data, one run per case. The model's wording will vary between runs and models; the exit codes, the request counts and the harness's error strings are what we would expect to hold. Our Codex was not signed in: its rows show the start of the connection, and no tool call ran, sotool_timeout_secis documentation only. We did not run Cursor; its column is documentation only. We did not test a stdio server, a connection that drops between calls, or a server that sends progress notifications. We did not run any of this against ValorBrain's production endpoint: nobody should inject faults there, and the product section comes from its documentation, its engine's code for the 30-minute fold, and one call to the public health route.
Checklist
- The server entry has a
timeout, set above your slowest legitimate call. - You decided what a slow start means for each job, and
MCP_TIMEOUTorstartup_timeout_secsays so. - In Codex, a server the job depends on has
required = true. - Unattended runs keep their
stream-jsonoutput, and a gate fails the job when the server was not connected or the last call to it failed. - The instruction file says what to do when a memory write fails: search, retry once, report.
- Anything with a name is written by key, so a retry cannot duplicate it.
- Somebody receives the failed job. A gate that fails into a channel nobody reads is the green dashboard again.
Sources, read on November 30, 2026: Claude Code, MCP (time limits on MCP tool calls, automatic reconnection, headersHelper), Environment variables (MCP_TIMEOUT, MCP_TOOL_TIMEOUT, CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT) and Run Claude Code programmatically; Codex, Model Context Protocol; Cursor, MCP.
Next: Headless and scheduled agent runs for what a run with nobody watching starts with, One MCP config in the repo, no token in git for the server that fails because a variable was never set, and What a memory MCP server costs in context for what the server adds to every session once it does connect.