Rate limits and 429s in an MCP server: what clients do and what to return
The spec requires servers to rate limit tool calls but not how. Where to return HTTP 429, where to return a tool error the model can read, what Claude Code retries, and a tested limiter.
Rate limits and 429s in an MCP server: what clients do and what to return
An MCP server in front of a metered API will hit a limit. The spec tells you to have one. It does not tell you what a rate-limited call should look like on the wire, and the clients differ in what they do with a 429. This guide separates the two places a limit can show up and recommends what to return in each.
What the spec requires
The security section of the 2026-07-28 tools page (the 2025-11-25 page is the same) says servers MUST rate limit tool invocations, and clients SHOULD implement timeouts for tool calls. OpenAI's Apps SDK server guide says to "apply timeouts and rate limits to expensive or externally visible tools" and notes that shared limits can go in server instructions because they "apply across tools". Neither names a status code, an error code or a retry field. That part is up to you.
The spec does define two kinds of error for tool calls. Protocol errors are JSON-RPC errors for things like unknown tools and malformed requests, which models are "less likely to be able to fix". Tool execution errors are results with isError: true, and the spec says they carry "actionable feedback that language models can use to self-correct and retry", and that clients SHOULD pass them to the model. A rate limit is a tool execution error by that definition: the request was valid and the model can act on it by waiting.
Two layers, two answers
Layer 1: the transport, before MCP runs. Your CDN, gateway or WAF can reject a request with HTTP 429 and a Retry-After header. MDN documents the header: it takes either an HTTP date or a number of seconds, and on a 429 it "indicates how long to wait before making a new request" (MDN). MDN also says support for it is still inconsistent.
Layer 2: your handler, after MCP parsed the call. Return a normal result with isError: true and say what to do, with the wait in a machine-readable field.
Which layer a given limit belongs to matters because of what clients document:
- Claude Code's MCP docs say that on a first connection to an HTTP server it retries up to three times on transient errors, "such as a 5xx response, a connection refused, or a timeout", and that discovery requests (
tools/list,prompts/list,resources/list) are retried up to three times but not on "authentication errors, 4xx responses, or request timeouts". The page never mentions 429. Since 429 is a 4xx, the stated rule means a rate-limitedtools/listis not retried. That is our reading of the rule, not a sentence on the page. - We found no documented behavior for a
429ontools/callin Claude, ChatGPT or the OpenAI API. Treat it as unknown.
So do not count on any client to wait and retry a 429 for you. If the call reaches your handler, return the tool error. Keep HTTP 429 for abuse you want to shed before it touches your code, and do not apply strict limits to initialize, tools/list or the OAuth endpoints. A client that gets a 4xx on tools/list may not load your tools at all, which is worse than a slow tool.
What a good tool error looks like
Notion's hosted MCP server is a documented example. Its rate limit page says it retries once when the wait is 2 seconds or less, and otherwise "returns the rate limit as a tool error right away instead of waiting it out". The error's structuredContent.error has code: "rate_limited", with retry_after_seconds when known and a rate_limit_reason, and the text content repeats the same JSON for clients that read only text.
Copy those ideas: a stable code, a wait in seconds, and the same data in text, because the model reads content and may never see structuredContent. Write the text as an instruction: "Wait 12 seconds, then call lookup_order again."
A tested limiter
This token bucket is keyed by user. We ran the same logic as plain JavaScript through the TypeScript SDK 1.32 with an in-memory client: the first two calls succeeded and the third returned isError: true with a one-second wait.
const buckets = new Map<string, { tokens: number; at: number }>();
// Returns 0 if allowed, else whole seconds until a token is free.
function take(key: string, capacity = 2, perSec = 1): number {
const now = Date.now();
const b = buckets.get(key) ?? { tokens: capacity, at: now };
b.tokens = Math.min(capacity, b.tokens + ((now - b.at) / 1000) * perSec);
b.at = now;
const ok = b.tokens >= 1;
if (ok) b.tokens -= 1;
buckets.set(key, b);
return ok ? 0 : Math.ceil((1 - b.tokens) / perSec);
}
server.registerTool('lookup_order',
{ description: 'Look up one order by id.', inputSchema: { order_id: z.string() } },
async ({ order_id }) => {
const wait = take(userId); // from the verified access token, never from the URL
if (wait > 0) {
const error = { code: 'rate_limited', retry_after_seconds: wait };
return {
isError: true,
structuredContent: { error },
content: [{ type: 'text', text: `Rate limited. Wait ${wait} seconds, then call lookup_order again. ${JSON.stringify(error)}` }],
};
}
return { content: [{ type: 'text', text: await getOrder(order_id) }] };
});
The in-memory map is per process. On serverless or several instances, keep the counters in a shared store, as with stateless sessions.
Key the limit by user, not by IP
Anthropic documents that its outbound traffic to your server comes from 160.79.104.0/21 (authentication docs). If you limit by source IP, many Claude users share the same few addresses and one heavy user can lock out the rest. Key by the authenticated user or token. For a server with no auth, the spec's own advice is that servers SHOULD implement authentication for all connections, so an unauthenticated limit can only be crude.
Also separate cheap reads from expensive tools. A per-tool budget stops a model that loops on one expensive call, and tells the model which tool to avoid.
Retries must be safe
A client that times out may call again, and the model may call again after your error. Make write tools idempotent with a key so a second identical call returns the first result. The timeouts guide shows the pattern. Do not rate limit by counting retries of the same idempotent call as new work.
If your limit comes from an upstream API, pass its wait through. Return the upstream Retry-After in retry_after_seconds, and add a small random delay if you retry yourself so many calls do not wake together.
Checklist
- Limits keyed by verified user or token, not IP
- Rate-limited tool calls return
isError: true, a code, and a wait in seconds - The text content repeats the structured error
-
initialize,tools/listand OAuth endpoints are not rate limited like tools - Any HTTP 429 carries
Retry-After - Write tools are idempotent
- Limits are shared across instances
Monitoring
Shipyard's monitoring runs the read-only fixtures you mark safe and flags a fixture that returns isError or breaks its latency budget, so a limit that starts biting your own checks shows up as an alert.
Check your server
Run the free scan at /scan. It will show whether initialize and tools/list answer from outside your network without a limit getting in the way.
Check your server against these rules
Scan runs the checks from this guide against a live URL and lists each failing rule with its fix.