Add an optional size limit for tool results
Problem Statement
One thing that's easy to run into with MCP tools is accidentally returning a very large result.
A search tool, log query, database query, or API wrapper can return far more data than expected. From the MCP server's point of view that's still a valid CallToolResult, but once it reaches an AI client it may consume a large part of the model's context window or make the next model call unnecessarily expensive.
Right now, applications can handle this themselves in each tool or with custom middleware, but it would be useful to have an optional guard at the server level.
Proposed Solution
Add an opt-in size limit for tool results before they are sent back to the client.
I think a byte limit would make more sense than a token limit here, since the MCP server doesn't know which model or tokenizer the client will use.
If a result exceeds the configured limit, I'd prefer returning a small IsError result explaining that the response was too large rather than silently truncating it. Silent truncation could leave the model with a partial result that still looks valid.
The limit could be disabled by default so existing behavior doesn't change.
Example Usage
The exact API isn't important, but something like this would be convenient:
s := server.NewMCPServer(
"my-server",
"1.0.0",
server.WithToolResultSizeLimit(128<<10), // 128 KiB
)Or this could be provided as a built-in ToolHandlerMiddleware instead if that fits the existing API better.
The main goal is to have one reusable place to enforce the limit across all tools.
Alternatives/Workarounds Considered
A server can already implement this with custom tool middleware or add size checks inside every handler.
For tools that naturally support pagination, limiting the result at the tool level is still the better approach.
The problem is mostly the fallback case: when a tool unexpectedly produces a huge response, having a server-wide safety net would keep that response from being passed straight through to the AI client.
Source: mark3labs/mcp-go