Skip to content

fix(api): preserve zero JSON-RPC request id in MCP error responses - #42997

Merged
Stream29 merged 2 commits into
langgenius:mainfrom
TINGyu123644:fix/mcp-error-zero-request-id
Nov 9, 2026
Merged

Stream29 merged 2 commits into
langgenius:mainfrom
TINGyu123644:fix/mcp-error-zero-request-id

Conversation

@TINGyu123644

Copy link
Copy Markdown
Contributor

create_mcp_error_response (api/core/mcp/utils.py) built the error response id with request_id or 1. Under JSON-RPC 2.0 a numeric id of 0 is perfectly legal — only null is reserved for notifications — and the response object MUST echo the same id as the request so the client can correlate it. With or 1, a request carrying id: 0 gets an error response with id: 1, which the client can never match.

This keeps the existing default of 1 for None (notification-style) and preserves a genuine 0.

  • api/core/mcp/utils.py: request_id if request_id is not None else 1
  • api/tests/unit_tests/core/mcp/test_utils.py: regression test for the zero-id case

Verified locally: with the patch, create_mcp_error_response(0, ...) yields id == 0; None still yields 1; string and non-zero numeric ids are unchanged.

`create_mcp_error_response` used `request_id or 1`, which rewrites a legal
JSON-RPC 2.0 numeric id of 0 to 1. Per the JSON-RPC 2.0 spec the response
id MUST echo the request id, so a client that sent id=0 can never match
the error response. Only `None` (notification-style request) should fall
back to the default id of 1.

Adds a regression test for the zero-id case; all existing
`TestCreateMCPErrorResponse` cases still pass.
@github-actions github-actions Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Sep 27, 2026
@ghfind-review ghfind-review Bot added the review: low ghfind author score; see https://ghfind.com label Sep 27, 2026
@crazywoola

Copy link
Copy Markdown
Member

Thank you for your contribution! I’ve approved the pending CI workflows so the automated checks can validate this update. Please keep an eye on the results and address any failures that need changes. We appreciate your help improving Dify.

@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 67.36% 67.35% -0.00%
Strict coverage 66.98% 66.98% -0.00%
Typed symbols 51,316 51,311 -5
Untyped symbols 25,011 25,010 -1
Modules 3562 3562 0

@codecov

codecov Bot commented Sep 28, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.05%. Comparing base (725611b) to head (bf8e07c).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #42997      +/-   ##
==========================================
- Coverage   88.05%   88.05%   -0.01%     
==========================================
  Files        5151     5151              
  Lines      323364   323364              
  Branches    65169    65169              
==========================================
- Hits       284729   284722       -7     
- Misses      33251    33259       +8     
+ Partials     5384     5383       -1     
Flag Coverage Δ
api 87.89% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@TINGyu123644

Copy link
Copy Markdown
Contributor Author

Thanks for approving the CI workflows! All required checks now pass: API unit/integration/E2E/VDB tests, pyrefly type checks, style checks, and codecov/patch are all green. The change keeps the MCP error response from omitting the request_id when it is explicitly zero (0), which was causing downstream clients to fail correlation. Ready for review whenever you have a chance.

@Stream29 Stream29 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@Stream29
Stream29 added this pull request to the merge queue Nov 9, 2026
Merged via the queue into langgenius:main with commit 62d63c6 Nov 9, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: low ghfind author score; see https://ghfind.com size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants