Skip to content

Pass through only backend-sent structuredContent - #7309

Merged
ChrisJBurns merged 2 commits into
stacklok:mainfrom
nilsberberich:fix/vmcp-structured-content-passthrough
Nov 26, 2026
Merged

ChrisJBurns merged 2 commits into
stacklok:mainfrom
nilsberberich:fix/vmcp-structured-content-passthrough

Conversation

@nilsberberich

@nilsberberich nilsberberich commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When a backend tool result has no structuredContent, vMCP builds one from the content array (conversion.ContentArrayToMap) and returns it to the client on plain pass-through calls. That map was meant for composite workflow templates, but clients that read structuredContent first get a shape the backend never produced: {"text": "..."} for a text result, and the full base64 payload of blob resources and images copied into structuredContent. With a backend that returns files as embedded resources, the client stores the file, but the base64 also lands in the model context through structuredContent (see #7174 for the file case and the OSV example in the comments).

  • So that clients only see what the backend produced, the backend client (toolResultFromMCP, shared by the Legacy and Modern backend paths) and the session connector (mcpSession.CallTool) now keep structuredContent only when the backend sent an object. Nothing is synthesized from content; a non-object value is still dropped, now with a Debug line on both paths.
  • Composite workflows do not need the synthesized map in the result, so they are unchanged: the workflow engine already builds step output from content when structuredContent is nil (tool steps and forEach), and the .content template context is built from content anyway.
  • The three Legacy result builders (coreToolHandler, DefaultHandlerFactory.CreateToolHandler, adaptToolsForFactory) assigned a possibly nil map[string]any to the any-typed mcp.CallToolResult.StructuredContent. Once the map is usually nil, that typed nil would be encoded as "structuredContent": null. A small helper, conversion.ToMCPStructuredContent, returns an untyped nil instead; code mode's toMCPResult uses it in place of its own guard. The Modern envelope already omitted the member.
  • Because comments described the synthesized map as part of the result contract, they are updated (vmcp.ToolCallResult.StructuredContent, ContentArrayToMap, code mode), and docs/arch/10-virtual-mcp-architecture.md gets a short section on tool call results so clients can see what to expect.

Fixes #7174

Type of change

  • Bug fix
  • New feature
  • Refactoring (no behavior change)
  • Dependency update
  • Documentation
  • Other (describe):

Test plan

  • Unit tests (task test)
  • E2E tests (task test-e2e)
  • Linting (task lint-fix)
  • Manual testing (describe below)

Unit tests added or extended:

  • pkg/vmcp/client: toolResultFromMCP with a blob resource, an image and a single text block without structuredContent (result has none, content unchanged), a backend object (passed through unchanged) and a non-object value (dropped).
  • pkg/vmcp/session/internal/backend: the same cases through mcpSession.CallTool against the package's fake backend (it can now return a configured tools/call result).
  • pkg/vmcp/server: Legacy tools/call result marshalled to JSON: the member is absent without structuredContent and forwarded unchanged with an object. DefaultHandlerFactory.CreateToolHandler leaves the field untyped nil. conversion.ToMCPStructuredContent has its own table test.
  • pkg/vmcp/composer: a tool step and a forEach inner step whose backend result has only content still expose output.text.
  • pkg/vmcp/codemode: an inner result with only an image or only a blob resource reaches the script as None, and the payload does not reach the script result.

Without the production change, the new client, session, Legacy wire and handler factory tests fail; the composer test and the code mode test pass before and after: the first guards the workflow engine's existing fallback, the second pins the script-side contract for results without text content. task lint-fix, task lint and task license-check report no issues, and task test passes locally.

Manual test: the change backported onto v0.69.0 (in that release toolResultFromMCP has no logger parameter, so its Debug lines use slog) and deployed as the vMCP image of a test gateway, with @softeria/ms-365-mcp-server (MS365_MCP_BLOB_OUTPUT=true) as a backend and Claude Code 2.1.332 as the client. In that deployment a filtering proxy sits in front of the backend; its workaround from the issue (adding a small structuredContent to file results) was switched off for the test, so the backend's result reached vMCP without structuredContent:

  • download-bytes for a small .xlsx: the client receives a short text element with name, content type and size, plus the resource; Claude Code stores the file and the model sees only [Resource from … at file:///….xlsx] Binary content (…, 1.7KB) saved to … (about 370 characters). No base64 in the tool result and no overflow file. On v0.66.0 without the workaround, the same tool put the full base64 into the result (92,448 characters for a 69 KB .docx).
  • A text-only tool without structuredContent: the client received the plain text result.
  • A backend that sends its own structuredContent: received once and unchanged.

Changes

File Change
pkg/vmcp/client/client.go Stop synthesizing structuredContent in toolResultFromMCP
pkg/vmcp/session/internal/backend/mcp_session.go Same for mcpSession.CallTool; Debug line for a dropped non-object
pkg/vmcp/conversion/content.go Add ToMCPStructuredContent; ContentArrayToMap doc limited to workflows
pkg/vmcp/server/serve_handlers.go, server/adapter/handler_factory.go, server/sessionmanager/factory.go Use ToMCPStructuredContent (no "structuredContent": null)
pkg/vmcp/codemode/decorator.go Use the helper instead of the local guard, comment updated
pkg/vmcp/types.go ToolCallResult.StructuredContent doc
docs/arch/10-virtual-mcp-architecture.md New section "Tool call results: structuredContent passthrough"
tests see Test plan

Does this introduce a user-facing change?

Yes. Clients calling backend tools through vMCP:

Backend result Before After
single text block, no structuredContent structuredContent: {"text": "<the text>"} no structuredContent
embedded blob resource (file), no structuredContent structuredContent: {"resource": "<full base64>", …} no structuredContent
image, no structuredContent structuredContent: {"image_0": "<base64>"} no structuredContent
text plus embedded text resource with the same text (e.g. a help tool) text three times in the client: resource, text element, and {"resource": …, "text": …} twice, as the backend sent it
backend sends a structuredContent that is not an object (e.g. an array) dropped, replaced by the map built from content dropped, no structuredContent
backend sends a structuredContent object unchanged unchanged

Clients that read structuredContent before content now get the backend's own output. Clients that relied on the synthesized keys (text, text_1, image_0, resource) no longer get them.

Code mode: an inner tool call whose backend sends no structuredContent now goes through the script engine's content fallback. A JSON text result reaches the script as the parsed value, and a plain (non-JSON) text result as the string of the first text item, instead of {"text": "<string>"}. This matches what the existing code mode tests already expect. An inner result with no text content item (for example only an image or a blob resource) reaches the script as None instead of {"image_0": "<base64>"} or {"resource": "<blob>"}; binary payloads are not passed into scripts. Scripts that read r["text"] from such results, or text_1/image_0/resource, need to change.

Composite tools: no change; step output and the .content template context are still built from content when the backend sends no structuredContent.

Implementation plan

Approved implementation plan
  • Cause: ContentArrayToMap is applied in two backend producers, toolResultFromMCP (pkg/vmcp/client, Legacy and Modern backend paths) and mcpSession.CallTool (pkg/vmcp/session/internal/backend). The workflow engine does not need it there: it falls back to ContentArrayToMap(result.Content) itself for tool steps and forEach.
  • Change:
    • Remove the fallback in both producers; StructuredContent is the backend's object or nil.
    • Legacy result builders (serve_handlers.go, adapter/handler_factory.go, sessionmanager/factory.go): set structuredContent only when present, otherwise the typed nil map is encoded as null. The Modern envelope and code mode already guard against this.
    • Update comments (types.go, ContentArrayToMap, code mode).
  • Visible to clients: no synthesized structuredContent (blob, image, text as {"text": ...}). Code mode scripts get the parsed JSON for JSON text results instead of {"text": "<string>"}. Described as before/after in the PR.
  • Tests: backend client (blob, image, text without structuredContent; own object unchanged; non-object dropped), session connector the same, wire result on the Legacy path (member absent, no null) and on the Modern path if not yet covered, composite tool step and forEach still expose output.text, code mode. Adapt or remove tests that pin the old behavior. Locally task lint, task test, task license-check.
  • Docs: section "Tool call results" in docs/arch/10-virtual-mcp-architecture.md.
  • Before the PR: build the change into a test gateway image and test the original file download case.

Special notes for reviewers

  • Legacy and Modern differ for an empty object: the Modern envelope omits structuredContent when the map is empty (len(...) > 0), the Legacy builders forward {}. That was already the case before; I left it alone to keep this PR to the bug, and the new doc section states it. Happy to align it here or in a follow-up if you prefer.
  • adaptToolsForFactory (sessionmanager/factory.go) gets the same helper but no dedicated test: its handlers are only invoked through the optimizer, whose call_tool result is converted back with a type assertion and then leaves through the guarded Legacy or Modern builder.
  • ContentArrayToMap itself is unchanged and still used by the workflow engine and the template expander.
  • Compared with the plan, the Modern path got no new test: the existing newModernCallToolResult test already checks that the member is omitted without structuredContent. For code mode, the existing tests already exercise a text-only inner result parsed as JSON; a test for the non-text case was added after review. No existing test pinned the synthesized map.

🤖 Generated with Claude Code

When a backend tool result had no structuredContent, vMCP built one
from the content array (ContentArrayToMap) and returned it to clients
on plain pass-through calls. Clients that read structuredContent first
got {"text": "..."} instead of the tool's output, and blob resources
and images had their base64 payload copied into structuredContent.

The backend client and the session connector now keep structuredContent
only when the backend sent an object. Composite workflows are
unaffected: the workflow engine already builds step output from content
when structuredContent is nil. The Legacy result builders now omit the
member instead of encoding a typed nil map as "structuredContent": null.

Fixes stacklok#7174

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Nils Berberich <[email protected]>

@ChrisJBurns ChrisJBurns left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the inline comments.

Comment thread pkg/vmcp/codemode/decorator.go Resolved
Without a synthesized structuredContent, an inner tool result that has
no text content item (for example only an image or a blob resource)
reaches code mode scripts as None. State this in the toMCPResult
comment and the architecture doc, and pin it with a decorator test so
the behavior stays deliberate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Nils Berberich <[email protected]>
@codecov

codecov Bot commented Nov 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.16%. Comparing base (0facc72) to head (a28f93b).
⚠️ Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
pkg/vmcp/server/sessionmanager/factory.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7309      +/-   ##
==========================================
+ Coverage   82.09%   82.16%   +0.07%     
==========================================
  Files         927      932       +5     
  Lines       93959    94526     +567     
==========================================
+ Hits        77132    77664     +532     
- Misses      16822    16857      +35     
  Partials        5        5              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisJBurns

Copy link
Copy Markdown
Collaborator

/retest

@ChrisJBurns
ChrisJBurns merged commit e7becbf into stacklok:main Nov 26, 2026
75 of 77 checks passed
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.

vMCP: synthesized structuredContent copies base64 blob of embedded resources to clients

2 participants