Skip to content

Conversation

@mattzcarey
Copy link
Contributor

  • use TransportSendOptions as per the MCP SDK
  • send() method to prioritise relatedRequestId for routing
  • some tests

@changeset-bot
Copy link

changeset-bot bot commented Nov 17, 2025

🦋 Changeset detected

Latest commit: b468829

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 17, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/agents@654

commit: b468829

@claude
Copy link

claude bot commented Nov 17, 2025

Claude Code Review

PR Summary: Implements relatedRequestId routing for bidirectional MCP streams, enabling server-to-client requests (like elicitations) to flow through the same POST stream as the originating client request.

Issues

1. Invalid JSON Schema constraint for number type

Location: examples/mcp-elicitation/src/index.ts:74

The schema uses minLength: 1 for a number field, but minLength only applies to strings/arrays. Should use minimum: 1 instead.

Impact: Schema validation may not enforce the constraint as intended, potentially allowing zero or negative values.

2. Test coupling via whitebox testing

Location: packages/agents/src/tests/mcp/transports/worker-transport.test.ts:1047-1089

The new tests use type assertions to directly manipulate private fields (streamMapping, requestToStreamMapping). While this enables thorough testing of internal routing logic, it creates tight coupling that will break if these fields are renamed/refactored.

Recommendation: Consider adding test-only public methods in future refactoring. For now, acceptable given the tests are well-documented.

Observations

Routing logic correctness

The send() method at worker-transport.ts:748-754 correctly implements priority:

  1. For responses/errors: always use message.id
  2. For server-to-client requests: use relatedRequestId if provided
  3. Otherwise: use standalone GET stream

This ensures responses always return on the correct stream, while server-initiated requests can be routed through the same stream as a related client request.

Optional chaining fix

worker-transport.ts:363 now correctly uses optional chaining for both conditions, fixing a potential null reference issue.

Testing

Excellent coverage with 340 lines of comprehensive tests:

  • ✅ Routing with/without relatedRequestId
  • ✅ Multiple concurrent streams
  • ✅ Response routing priority
  • ✅ Error cases (missing/closed streams)
  • ✅ Edge cases (concurrent requests, priority overrides)

Verdict

One fix required: Issue #1 - use minimum: 1 instead of minLength: 1 for the amount field

Otherwise, the implementation is well-designed and thoroughly tested. The core routing logic is sound and properly handles complex stream routing scenarios.

threepointone pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 17, 2025
Add comprehensive documentation for the relatedRequestId routing feature
that enables proper server-to-client request routing in Streamable HTTP
transport. This feature is essential for bidirectional communication
patterns like elicitation where the server needs to send requests back
to the client while maintaining proper request-response pairing.

Related: cloudflare/agents#654

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@threepointone
Copy link
Contributor

Documentation sync PR created: cloudflare/cloudflare-docs#26562

This PR documents the relatedRequestId routing feature for bidirectional MCP streams, including:

  • How relatedRequestId routing works in Streamable HTTP transport
  • When to use relatedRequestId (elicitation, progress updates, callbacks)
  • Implementation details and code examples
  • Clarification that this is specific to Streamable HTTP transport

The documentation has been added to the transport.mdx page in the Model Context Protocol section.

threepointone pushed a commit to cloudflare/cloudflare-docs that referenced this pull request Nov 17, 2025
Add documentation for bidirectional streaming with relatedRequestId in MCP transport. This explains how server-to-client requests (like elicitation) are routed through the same stream as the originating client request.

Related to cloudflare/agents#654
@threepointone
Copy link
Contributor

📚 Documentation Update

I've synced the documentation for this PR to the cloudflare-docs repository.

Documentation PR: cloudflare/cloudflare-docs#26562

What was documented:

  • Added a new section on Bidirectional streaming with relatedRequestId to the MCP Transport documentation
  • Explained how server-to-client requests (like elicitation) are routed through the same stream as the originating client request
  • Documented the message routing priority: responses → relatedRequestId → fallback to GET stream
  • Provided a practical elicitation example showing automatic relatedRequestId handling
  • Included advanced usage documentation for custom transport implementations using TransportSendOptions

The documentation helps developers understand when and how to use relatedRequestId for proper bidirectional communication in MCP servers.

@threepointone
Copy link
Contributor

Documentation has been synced to cloudflare-docs.

📚 Documentation PR: cloudflare/cloudflare-docs#26562

The following documentation updates have been made:

  • Added comprehensive guide on using relatedRequestId for bidirectional MCP communication
  • Documented how server-to-client requests (elicitation) are routed through POST streams
  • Included complete code example showing elicitInput() with relatedRequestId parameter
  • Explained technical details of request-to-stream mapping and hibernation support

Please review the documentation PR to ensure it accurately reflects the changes in this PR.

@mattzcarey mattzcarey force-pushed the fix-related-request-id branch from c365f76 to df0bc0d Compare November 18, 2025 12:06
"agents": patch
---

Use relatedRequestId in TransportOptions to send the response down a POST stream if supported
Copy link
Contributor

Choose a reason for hiding this comment

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

reference MCP here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doing this now

@threepointone
Copy link
Contributor

📚 Documentation Updated

The documentation for this change has been synced to the cloudflare-docs repository:

Documentation PR: cloudflare/cloudflare-docs#26562

What was documented

Added a new section on Bidirectional stream routing in the MCP transport documentation that covers:

  • How the transport layer routes server-to-client requests through the correct HTTP stream using relatedRequestId
  • The routing priority (responses → related requests → standalone stream)
  • Practical example showing how to use relatedRequestId with elicitInput in tool handlers

This ensures developers understand how to properly implement elicitation and other bidirectional communication patterns when building MCP servers with the Agents SDK.

- Added support for TransportSendOptions to route server-to-client requests through the same stream as the originating client request
- Updated send() method to prioritize relatedRequestId for routing, with message.id override for responses/errors
- Added comprehensive test coverage for stream routing scenarios including multiple streams, error cases, and response handling
@mattzcarey mattzcarey force-pushed the fix-related-request-id branch from c4a0dd3 to b468829 Compare November 19, 2025 12:48
@mattzcarey mattzcarey merged commit a315e86 into main Nov 19, 2025
5 checks passed
@mattzcarey mattzcarey deleted the fix-related-request-id branch November 19, 2025 12:58
@threepointone threepointone mentioned this pull request Nov 19, 2025
naji247 pushed a commit to MCPCat/agents that referenced this pull request Nov 20, 2025
* feat: implement relatedRequestId routing for bidirectional MCP streams

- Added support for TransportSendOptions to route server-to-client requests through the same stream as the originating client request
- Updated send() method to prioritize relatedRequestId for routing, with message.id override for responses/errors
- Added comprehensive test coverage for stream routing scenarios including multiple streams, error cases, and response handling

* changeset

* fix: review suggestions

* add related id to ellicitation call

* fix: optional chaining

* update changeset
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