Skip to content

fix: complete ShenyuMcpResponseDecorator future only after all chunks are received (#6640) - #7040

Open
wy471x wants to merge 9 commits into
apache:masterfrom
wy471x:fix_ShenyuMcpResponseDecorator-completes-future-on-first-chunk
Open

wy471x wants to merge 9 commits into
apache:masterfrom
wy471x:fix_ShenyuMcpResponseDecorator-completes-future-on-first-chunk

Conversation

@wy471x

@wy471x wy471x commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #6640

Make sure that:

  • You have read the contribution guidelines.
  • You submit test cases (unit or integration tests) that back your changes.
  • Your local test passed ./mvnw clean install -Dmaven.javadoc.skip=true.

Summary

ShenyuMcpResponseDecorator.writeWith completed the response future inside doOnNext, so the very first DataBuffer determined the captured result. Any response delivered in more than one chunk was therefore truncated to the first chunk.

This PR defers future completion until the whole response body stream has completed (with setComplete() kept as the empty/no-body fallback), so tool-call results now contain the full accumulated body.

Changes:

  1. ShenyuMcpResponseDecorator.writeWith (ShenyuMcpResponseDecorator.java:57) — doOnNext now only appends each chunk to the shared body; a doOnComplete(() -> completeFuture()) was added so the future is completed only after all chunks have been accumulated.
  2. Added private ShenyuMcpResponseDecorator.completeFuture() (ShenyuMcpResponseDecorator.java:87) — extracts the synchronized, duplicate-completion-safe logic shared by the stream-completion path and setComplete().
  3. ShenyuMcpResponseDecorator.setComplete() (ShenyuMcpResponseDecorator.java:80) — now delegates to completeFuture() before calling the delegate, preserving the no-write/empty-response fallback.

Test Cases:

  • ShenyuMcpResponseDecoratorTest.testWriteWithCompletesFutureWithAllChunks — feeds two DataBuffers (part-1, + part-2) through writeWith and asserts the future receives the full part-1,part-2 body, not just the first chunk.
  • ShenyuMcpResponseDecoratorTest.testSetCompleteCompletesFutureWithAccumulatedBody — verifies the setComplete() fallback still completes the future.

Verification

  • ./mvnw -o -pl shenyu-plugin/shenyu-plugin-mcp-server verify on JDK 21 passed: checkstyle, RAT, and all module tests (including the new ShenyuMcpResponseDecoratorTest).

close #6640

Copilot AI 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.

🟡 Changes recommended

One or more issues must be addressed before approval.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Fixes MCP response truncation by completing the response future only after all body chunks arrive.

Changes:

  • Defers future completion until stream completion.
  • Centralizes completion logic for writeWith and setComplete.
  • Adds multi-chunk and empty-response tests.
File summaries
File Description
shenyu-plugin/shenyu-plugin-mcp-server/src/test/java/org/apache/shenyu/plugin/mcp/server/response/ShenyuMcpResponseDecoratorTest.java Updated as part of this pull request.
shenyu-plugin/shenyu-plugin-mcp-server/src/main/java/org/apache/shenyu/plugin/mcp/server/response/ShenyuMcpResponseDecorator.java Updated as part of this pull request.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

}
}
}));
}).doOnComplete(() -> completeFuture()));

decorator.setComplete().block();

assertEquals("", future.getNow(""));

@Aias00 Aias00 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.

Approved as PMC (Aias00). Coherent, well-scoped fix with regression tests; green CI, mergeable. Reviewed the diff.

This branch has not been deployed

No deployments
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.

[BUG] ShenyuMcpResponseDecorator completes future on first chunk (truncates multi-chunk bodies)

3 participants