Rightshold™ Sign up

Answers · MCP security

How do I cap or truncate MCP tool output so it doesn't flood the context window?

Last updated

TL;DR: Fix it in two places. In the server, make large results small by design: give every tool that can return a lot a limit or page-size argument with a sensible default, a cursor for the next page, filters and a concise/detailed response format, and when you do cut a result, say so in the result and tell the agent how to get the rest. Return a resource_link to a big file instead of its contents. In the client, set a ceiling: Claude Code warns when an MCP tool output passes 10,000 tokens, limits it to 25,000 tokens by default (change it with MAX_MCP_OUTPUT_TOKENS), and saves successful text results over 50,000 characters to a file instead of putting them in the conversation. The MCP specification itself sets no size limit on tool results, and its built-in pagination covers list operations such as tools/list, not tools/call results.

What should I check, step by step?

1. Find the tools that can return unbounded data: search, list, query, log, file-read and fetch tools are the usual ones.

  1. Give each one a limit (or page_size) argument with a small default and a hard maximum the server enforces, so a model that asks for 'everything' still gets one page.
  2. Return a cursor or offset for the next page and say in the result how many items remain. MCP's own cursor pagination applies to list operations, so for tools/call you add these as ordinary tool arguments.
  3. Add filters and range selection (date range, fields, line range) so the agent can ask for exactly what it needs instead of fetching everything and reading it.
  4. Offer a response_format argument (for example concise or detailed) and default to concise: names and IDs the next call needs, not every field.
  5. If you truncate, mark it in the result itself, for example '[truncated: showing 50 of 4,812 rows; call again with cursor=... or add a filter]'. A silent cut looks like a complete answer.
  6. For large files, return a resource_link (URI, name, mimeType) and let the client fetch it only if needed, rather than embedding the whole file in the result.
  7. Keep error results short and actionable: Claude Code trims error text longer than about 11,000 characters to its first and last 5,000.
  8. In Claude Code, set MAX_MCP_OUTPUT_TOKENS if 25,000 tokens is too much or too little for your work; only raise a single tool's text ceiling with _meta["anthropic/maxResultSizeChars"] when its large output is genuinely needed.
  9. Remember that big tool output is also untrusted input: the more third-party text a tool pours into the context, the more room there is for injected instructions.

What Claude Code does with large MCP results

Claude Code's MCP docs list the current limits: a warning above 10,000 tokens of tool output, a default maximum of 25,000 tokens that MAX_MCP_OUTPUT_TOKENS changes (the warning threshold is fixed), and, for a successful result without images that goes over the limit, saving it to a file in the session's tool-results directory and putting the file path in the conversation so Claude can read it when needed.

What the MCP specification says

A tool result carries a content array (text, image, audio, resource links and embedded resources) and optionally structuredContent; a tool that returns structuredContent SHOULD also return the serialized JSON in a text block, which doubles the size of a large structured result.

Why truncation alone isn't enough

Anthropic's engineering post on writing tools for agents recommends some combination of pagination, range selection, filtering and truncation with sensible default parameter values for any response that could use a lot of context, and, if you truncate, steering the agent with helpful instructions, for example to make many small targeted searches instead of one broad one.

How do I ask Rightshold about this (free, no key)?

Add the MCP server https://www.rightshold.com/mcp to Claude, Cursor, ChatGPT, Grok or VS Code, then ask: “Before I connect this MCP server to my agent, what tools does it expose, and have their definitions changed since they were recorded?”

One-click setup

{
  "tool": "precall_check",
  "arguments": {
    "url": "<the MCP server's https URL>",
    "tool": "<optional tool name>"
  }
}

Rightshold does not run your tools, measure their output or set limits in your MCP client; output size is controlled by the server's design and the client's settings.

What are the sources?

Related

Not legal advice. JSON · Markdown · All answers

Get a free API key in one call

No card and no email needed. 100 free credits; the license and policy checks stay free without a key.

curl -s -X POST https://www.rightshold.com/v1/accounts/signup -H "Content-Type: application/json" -d '{"accept_terms":true}'
export RIGHTSHOLD_API_KEY=rs_...   # the api_key from the reply (shown once)
curl -s https://www.rightshold.com/v1/licenses/check -H "Authorization: Bearer $RIGHTSHOLD_API_KEY" \
  -H "Content-Type: application/json" -d '{"license":"MIT","intended_use":{"commercial_use":true}}'