Skip to content

fix(sdk): unwrap {result: ...} transport carriers before MCP citation detection - #4555

Merged
YehongPan merged 2 commits into
developfrom
fix/mcp-result-carrier-citation
Dec 4, 2026
Merged

YehongPan merged 2 commits into
developfrom
fix/mcp-result-carrier-citation

Conversation

@TiTi-iTiT

@TiTi-iTiT TiTi-iTiT commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Data-platform RAG MCP tools (e.g. datacenter_tools__2, verified on tianzhou-beta conversations/13734) return their business JSON inside a single-key {"result": "<JSON string>"} transport carrier (double-encoded).
  • _extract_retrieval_items only recognized top-level lists and text objects, so the carrier escaped detection entirely: no search_content sources were reported to the frontend, and the model invented its own reference keys ([[a1]](1)), which React-Markdown rendered as plain links instead of citation badges — the "Source details are loading" dead end.
  • Add _unwrap_result_carrier(): unwrap a sole-key {"result": ...} carrier (JSON-string or object value, up to two nested levels) before retrieval detection, mirroring the jiuwen adapter's carrier-decoding constraint ("never arbitrary nested business JSON").
  • After unwrapping, the existing chain applies unchanged: tool_sign="q" sources on the SEARCH_CONTENT channel, reference_mark: [[q1]] in the model payload, and the frontend renders resolvable citation badges (title from document_name).

Compatibility

  • The wrapper is only wired for source == "mcp" tools; built-in search tools, local/langchain/builtin sources and the jiuwen core are untouched.
  • Only an object whose sole key is result is treated as a carrier; a business payload that merely carries a result field among others is never misinterpreted.
  • Non-JSON text, scalars, arrays, multi-key dicts, plain-text carrier values and non-retrieval lists inside carriers all keep the previous pass-through behavior (the model payload stays byte-identical).
  • Worst-case misdetection of a retrieval-shaped list inside a carrier costs one extra citation badge plus an INFO log naming the tool; annotation is non-destructive (unmapped fields preserved verbatim, failures fall back to the original output).
  • Unwrapping depth is capped at two levels; no recursion risk.

Test evidence

  • pytest test/sdk/core/agents/test_mcp_citation.py — 54 passed (49 existing + 5 new regressions: JSON-string carrier replaying the tianzhou-beta payload, nested carrier, plain-text carrier pass-through, multi-key payload pass-through, non-retrieval list inside carrier pass-through).
  • pytest test/sdk/core/agents/test_nexent_agent.py — 248 passed (wiring regression).
  • Copyright header check passed for both changed files.

Notes

  • Takes effect once the image carrying this fix is deployed to tianzhou-beta; new datacenter queries then display citation badges.
  • Historical conversations lack persisted source data from the old behavior and cannot be retro-rendered (out of scope for this fix).
image

…on detection

Data-platform RAG MCP tools deliver their business JSON inside a
single-key {"result": "<JSON string>"} transport carrier. The retrieval
detection only scanned top-level lists and text objects, so the carrier
escaped annotation entirely: no SEARCH_CONTENT sources reached the
frontend and the model invented its own reference keys, leaving citation
markers stuck on "Source details are loading".

Unwrap the carrier before retrieval detection (up to two nested levels,
sole "result" key only, mirroring the jiuwen adapter's carrier decoding
constraint of never touching arbitrary nested business JSON). All other
payload shapes keep the previous pass-through behavior, so no other MCP
tool result changes. Five regression cases cover the payload captured
on tianzhou-beta, nested carriers, plain-text carriers, multi-key
payloads and non-retrieval lists inside carriers.
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Exercise the scalar-value carrier branch ({"result": 42}) so the new
carrier unwrapping reaches 100% line coverage, closing the codecov
patch gap reported on PR #4555 (88.23% against the 90% target).
@YehongPan
YehongPan merged commit 8aaee32 into develop Dec 4, 2026
11 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.

2 participants