Skip to content

fix(server): apply tool_response_limit to structuredContent - #1937

Merged
Dumbris merged 2 commits into
mainfrom
claude/pass-through-behavior-review-e542ee
Nov 19, 2026
Merged

Dumbris merged 2 commits into
mainfrom
claude/pass-through-behavior-review-e542ee

Conversation

@Dumbris

@Dumbris Dumbris commented Nov 19, 2026

Copy link
Copy Markdown
Member

Summary

tool_response_limit truncated only the text blocks in content, so an upstream that returned a large structuredContent (the report: a Vikunja MCP server) delivered the full payload to the client. This was a gap rather than a decision. Spec 056 and #1861 only promise that output validation never mutates structuredContent; neither addresses the response limit.

Now, when the JSON encoding of structuredContent is longer than the limit:

  • it is withheld from structuredContent, and RawStructuredContent is cleared as well;
  • unless a text block already carries semantically equal JSON (the MCP spec says servers SHOULD send one), it is appended as a short label block plus a JSON text block;
  • the result is rewritten in place, before output sanitisation. The JSON block is then an ordinary text block: it is redacted or blocked by sanitisation, seen by TOON detection and the sensitive-data detector, truncated, and cached behind a read_cache handle.

A structuredContent within the limit is forwarded unchanged (Spec 056 SC-002 holds).

Kept consistent:

  • Output-schema validation judges the upstream's structured value (outputValidationView), so strict mode with missing_structured_content=block does not refuse a large response.
  • Spec 069 response_bytes keeps measuring what the upstream returned (upstreamSizeView): the structured value and its raw bytes are restored and the appended blocks dropped.
  • Direct surface: a tool whose published listing carries an outputSchema keeps its structuredContent, because MCP clients reject a result without it. The handler decides from directCatalogEntry.OutputSchemaListed, which is set at render time from the registered mcp.Tool. Deferred listings never publish an outputSchema, so their structured results are bounded too. The direct surface has no read_cache, so its JSON block is cut plainly.

No new config option. Withholding by default is what the issue asked for. A pass-through knob can follow if someone needs the old behaviour.

closes #1932

Testing

  • internal/server/content_forward_structured_test.go: withholding and caching (the banner names the stored key), duplicate text detection, no write into the caller's backing array, no-op cases, redaction of the replacement (proved through the cached copy), validation view with a real validator plus a control, response_bytes parity (including raw bytes), direct render flag in full and deferred modes.
  • go test -race -tags server over ./internal/server/... and ./internal/truncate/... with the CI skip regex: pass.
  • ./scripts/test-api-e2e.sh: 77/77.
  • golangci-lint v2 (.github/.golangci.yml), bare and --build-tags server: 0 new issues.
  • Cross-model review: opencode github-copilot/gpt-6.1-sol, 3 rounds. Round 1 found the replacement bypassing sanitisation/detection and deferred listings being exempted. Round 2 found response_bytes measuring the withheld form. Round 3: clean.

tool_response_limit truncated only TextContent blocks, so an upstream
returning a large structuredContent delivered the full payload to the
client. An oversize structured result is now withheld and, unless a text
block already carries the same JSON, turned into text blocks before output
sanitisation, so it is redacted, scanned, truncated and cached for
read_cache like any other block. Results within the limit are unchanged.

Output-schema validation still judges the upstream value, Spec 069
response_bytes still measures it, and on the direct surface a tool whose
published listing carries an outputSchema keeps its structuredContent.

Closes #1932
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

The docs site does not publish docs/configuration.md, so the link from
output-schema-validation broke the Docusaurus build. Point it at
configuration/config-file.md and describe the structuredContent bound there.
@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: claude/pass-through-behavior-review-e542ee

Available Artifacts

  • archive-darwin-amd64 (33 MB)
  • archive-darwin-arm64 (30 MB)
  • archive-linux-amd64 (20 MB)
  • archive-linux-arm64 (18 MB)
  • archive-windows-amd64 (33 MB)
  • archive-windows-arm64 (29 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (29 MB)
  • installer-dmg-darwin-arm64 (26 MB)
  • smart-mcp-proxymcpproxy-goUWARBM.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 44514382038 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@Dumbris
Dumbris merged commit 201d373 into main Nov 19, 2026
54 checks passed
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 80.24691% with 16 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/server/content_forward_structured.go 83.33% 7 Missing and 4 partials ⚠️
internal/server/mcp.go 50.00% 2 Missing and 1 partial ⚠️
internal/truncate/truncator.go 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

Dumbris added a commit that referenced this pull request Nov 24, 2026
## Summary

Since 0.75.0 (#1937) `tool_response_limit` also bounds
`structuredContent`. That is right for agents, but a local script that
parses the JSON itself now gets cut answers, and `read_cache` cannot
rebuild them (it pages one record array and leaves out sibling fields
such as `@odata.nextLink`).

This adds the override the issue asks for:
`profiles[].tool_response_limit`.

- unset: the profile inherits the global limit
- `0`: no limit, text blocks and `structuredContent` arrive whole
- a positive number: that many characters (may be larger or smaller than
the global limit)

A script gets its own read-only profile with whole answers; chats keep
the default bound.

```json
{ "name": "script", "servers": ["workiq", "tickets"], "max_tier": "read", "tool_response_limit": 0 }
```

closes #2027

## What changed

- **Config** (`internal/config/profiles.go`): new `ToolResponseLimit
*int` (a pointer, so an explicit `0` survives `omitempty`),
`EffectiveToolResponseLimit`, and a negative value is refused by the one
profile validator (field `tool_response_limit`). It is a delivery
setting, not policy: it does not make a profile non-legacy.
- **Serving path** (`internal/server/profile_response_limit.go`): the
truncator is resolved per call from the effective profile
(`truncatorForProfile`), and used by `call_tool_read|write|destructive`,
the legacy `call_tool`, the direct tools, `retrieve_tools`, `read_cache`
and the TOON encoder. A profile that sets no limit, or a name that no
longer exists, keeps the live global truncator.
- **Sanitisation is not weakened.** Output sanitisation reads text
blocks only, and under the global limit an oversize structured result
reached it as text. While sanitisation is set to block, redact or strip,
the global limit still decides when a structured result becomes a text
block; the profile's limit then delivers that text whole
(`structuredWithholdLimit`). With sanitisation in its default mode the
result stays in `structuredContent`.
- **Admin surfaces**: REST `ProfileView.tool_response_limit`, the
profile change diff, the MCP `profiles` tool schema, CLI `profile
create|update --tool-response-limit <n|inherit>` /
`--clear-tool-response-limit` / `profile try --set
tool_response_limit=`, `profile show`.
- **Editors**: the Web UI and macOS profile editors gain an Inherit / No
limit / Custom control. Without it, saving a profile there would have
dropped a limit set elsewhere, because both build the PUT body from
their own draft.
- **Docs**: `docs/features/profiles.md` "Response limit per profile",
config reference, CLI and REST references, swagger, changelog.

## Decisions made without asking

- The override may widen as well as narrow. The limit is a
context-budget setting, not an access rule, and only an operator can set
it. Any caller that can select the profile gets its limit, which the
docs say.
- `mcpproxy call tool-read` has no profile argument and is unchanged; a
script uses `/mcp/p/<profile>` or a credential bound to the profile.

## Testing

- New Go tests: config validation and JSON round trip,
`handleCallToolVariant` under URL, session and pinned profiles against a
real in-process upstream (fails without the change), the sanitisation
case, direct-mode limit resolution, REST view and diff, CLI flags.
- New vitest spec for the editor control; new XCTest cases for the Swift
payload round trip.
- Live check with the built binary and a fixture returning 128,357
characters of `structuredContent` (300 records plus `@odata.nextLink`):

| Endpoint | Result |
|---|---|
| `/mcp` (no profile, limit 20000) | structuredContent withheld, text
cut at 19,811 |
| `/mcp/p/chat` (inherits) | same as above |
| `/mcp/p/script` (`tool_response_limit: 0`) | structuredContent whole:
300 records, `@odata.nextLink` present |

A live `profile update chat --tool-response-limit 60000` took effect on
the next call without a restart.

## Known, not changed here

Direct mode reads the global limit from the construction-time config, so
a hot-reloaded global `tool_response_limit` does not reach it. That
predates this change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tool_response_limit does not truncate structuredContent

2 participants