Skip to content

ResponseLimitingMiddleware truncation breaks tools with outputSchema #3717

Description

@kterui9019

What happened?

When ResponseLimitingMiddleware truncates a response from a tool that has an outputSchema (either explicitly set or auto-generated from a return type annotation), the truncated ToolResult has no structured_content. This causes the MCP SDK's output validation to fail with:
Output validation error: outputSchema defined but no structured output returned

Normal-sized responses work fine. The error only occurs when the response exceedsmax_sizeand gets truncated.

I noticed that the original PR (#3072) had a raise_on_structured option that was removed during review as unnecessary complexity. However, the current behavior silently produces an MCP protocol violation for any tool with outputSchema — which includes any tool with a return type annotation. It seems like some form of explicit handling for outputSchema-bearing tools is needed, whether that's raising, preserving structured_content, or something else.

Example Code

import asyncio
from fastmcp import Client, FastMCP
from fastmcp.server.middleware.response_limiting import ResponseLimitingMiddleware
from pydantic import BaseModel

class Answer(BaseModel):
    text: str

mcp = FastMCP()
mcp.add_middleware(ResponseLimitingMiddleware(max_size=1_000))

@mcp.tool()
def big_answer() -> Answer:
    return Answer(text="x" * 2_000)
async def demo():
    async with Client(mcp) as client:
        result = await client.call_tool("big_answer")
        print(f"OK: {result}")
asyncio.run(demo())
WARNING  Tool 'big_answer' response exceeds size limit: 4131 bytes > 1000 bytes, truncating
fastmcp.exceptions.ToolError: Output validation error: outputSchema defined but no structured output returned

Version Information

FastMCP version:  3.1.1
MCP version:      1.26.0
Python version:   3.13.11
Platform:         macOS-26.3-arm64-arm-64bit-Mach-O

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.serverRelated to FastMCP server implementation or server-side functionality.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions