Skip to content

fix(api): keep MCP tool list output as JSON text - #46305

Open
mayank609 wants to merge 1 commit into
langgenius:mainfrom
mayank609:fix/mcp-tool-json-list-text
Open

mayank609 wants to merge 1 commit into
langgenius:mainfrom
mayank609:fix/mcp-tool-json-list-text

Conversation

@mayank609

@mayank609 mayank609 commented Nov 13, 2026 •

Copy link
Copy Markdown

Fixes #46303

Summary

When an MCP tool returns a JSON array that contains anything other than objects (strings, numbers, booleans, null, nested arrays), MCPTool._process_json_list falls back to a single text message. It was building that text with str(json_list), so ["ok", true, null] became ['ok', True, None]. That's Python syntax, which breaks anything downstream that expects the JSON the server actually returned.

The fix serializes the list back with json.dumps(..., ensure_ascii=False). Lists made only of objects are untouched; they still produce one JSON message per item.

I added an assertion to the existing helper test in tests/unit_tests/core/tools/mcp_tool/test_tool.py. It feeds ["a", true, null, 1.5, "é"] through _process_text_content and checks that the text comes back as the same JSON. It fails on main (['a', True, None, 1.5, 'é']) and passes with the change. The whole file passes (50 tests), and ruff check / ruff format --check are clean.

Screenshots

N/A (backend only)

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

@github-actions github-actions Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Nov 13, 2026
@mayank609
mayank609 force-pushed the fix/mcp-tool-json-list-text branch from 98cf7b0 to 834edbb Compare November 14, 2026 04:58
@github-actions github-actions Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. web This relates to changes on the web. and removed size:S This PR changes 10-29 lines, ignoring generated files. size:XXL This PR changes 1000+ lines, ignoring generated files. labels Nov 14, 2026
@mayank609
mayank609 force-pushed the fix/mcp-tool-json-list-text branch from 834edbb to 25a7293 Compare November 14, 2026 06:17
@mayank609
mayank609 requested a review from a team November 14, 2026 06:17
@github-actions github-actions Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Nov 14, 2026
@mayank609
mayank609 force-pushed the fix/mcp-tool-json-list-text branch 2 times, most recently from e942881 to 5076af0 Compare November 14, 2026 08:43

GYJ99 commented Nov 15, 2026

Copy link
Copy Markdown

Hi @mayank609 — thanks for working on #46303. I checked the current PR diff and noticed it contains 242 changed files, including unrelated CI workflows, API files and Web files, although the intended fix is scoped to MCP JSON-list text output and its regression tests. GitHub also currently reports this PR as not mergeable.

Could you rebase the fix onto the latest langgenius/dify:main (or recreate a clean topic branch from upstream main and cherry-pick only the fix/test commit), so the diff contains only the relevant MCP tool and test changes? The related #46304 currently also shows 242 changed files and may need the same cleanup. That should make both fixes much easier for maintainers to review and run through CI.

I inspected the remote diff and PR metadata only; I have not run the tests locally. Thanks!

@mayank609
mayank609 force-pushed the fix/mcp-tool-json-list-text branch 5 times, most recently from 97445ac to 56eb5d4 Compare November 18, 2026 15:10
@github-actions github-actions Bot removed the web This relates to changes on the web. label Nov 18, 2026
@github-actions github-actions Bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Nov 18, 2026
@mayank609

Copy link
Copy Markdown
Author

@GYJ99 thanks for flagging that. The branch was based on an older main, so the diff picked up unrelated changes. I rebased it onto the latest main and it now touches only the two files for the fix (mcp_tool/tool.py and its test). It shows as mergeable again.

@mayank609
mayank609 force-pushed the fix/mcp-tool-json-list-text branch from 56eb5d4 to 623a622 Compare November 18, 2026 15:12
@github-actions github-actions Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. size:S This PR changes 10-29 lines, ignoring generated files. web This relates to changes on the web. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Nov 18, 2026
@mayank609
mayank609 force-pushed the fix/mcp-tool-json-list-text branch from 623a622 to dbd7f63 Compare November 20, 2026 11:48
@github-actions github-actions Bot removed the size:S This PR changes 10-29 lines, ignoring generated files. label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files. web This relates to changes on the web.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP tool returns Python repr instead of JSON for arrays with non-object items

2 participants