Skip to content

Commit c22887f

Browse files
mattzcareynaji247
authored andcommitted
RelatedRequestId routing for MCP streams (cloudflare#654)
* 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
1 parent 76553dd commit c22887f

File tree

6 files changed

+422
-26
lines changed

6 files changed

+422
-26
lines changed

.changeset/spotty-crabs-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"agents": patch
3+
---
4+
5+
When handling MCP server requests use relatedRequestId in TransportOptions to send the response down a POST stream if supported (streamable-http)

examples/mcp-elicitation/src/index.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,31 @@ export class MyAgent extends Agent<Env, State> {
5454
confirm: z.boolean().describe("Do you want to increase the counter?")
5555
}
5656
},
57-
async ({ confirm }) => {
57+
async ({ confirm }, extra) => {
5858
if (!confirm) {
5959
return {
6060
content: [{ type: "text", text: "Counter increase cancelled." }]
6161
};
6262
}
6363
try {
64-
const basicInfo = await this.server.server.elicitInput({
65-
message: "By how much do you want to increase the counter?",
66-
requestedSchema: {
67-
type: "object",
68-
properties: {
69-
amount: {
70-
type: "number",
71-
title: "Amount",
72-
description: "The amount to increase the counter by",
73-
minLength: 1
74-
}
75-
},
76-
required: ["amount"]
77-
}
78-
});
64+
const basicInfo = await this.server.server.elicitInput(
65+
{
66+
message: "By how much do you want to increase the counter?",
67+
requestedSchema: {
68+
type: "object",
69+
properties: {
70+
amount: {
71+
type: "number",
72+
title: "Amount",
73+
description: "The amount to increase the counter by",
74+
minLength: 1
75+
}
76+
},
77+
required: ["amount"]
78+
}
79+
},
80+
{ relatedRequestId: extra.requestId }
81+
);
7982

8083
if (basicInfo.action !== "accept" || !basicInfo.content) {
8184
return {

packages/agents/src/mcp/worker-transport.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
* Based on @hono/mcp transport implementation (https://github.com/honojs/middleware/tree/main/packages/mcp)
33
*/
44

5-
import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
5+
import type {
6+
Transport,
7+
TransportSendOptions
8+
} from "@modelcontextprotocol/sdk/shared/transport.js";
69
import type {
710
JSONRPCMessage,
811
RequestId,
@@ -357,7 +360,7 @@ export class WorkerTransport implements Transport {
357360
const acceptHeader = request.headers.get("Accept");
358361
if (
359362
!acceptHeader?.includes("application/json") ||
360-
!acceptHeader.includes("text/event-stream")
363+
!acceptHeader?.includes("text/event-stream")
361364
) {
362365
return new Response(
363366
JSON.stringify({
@@ -738,9 +741,14 @@ export class WorkerTransport implements Transport {
738741
this.onclose?.();
739742
}
740743

741-
async send(message: JSONRPCMessage): Promise<void> {
742-
let requestId: RequestId | undefined;
744+
async send(
745+
message: JSONRPCMessage,
746+
options?: TransportSendOptions
747+
): Promise<void> {
748+
// Check relatedRequestId FIRST to route server-to-client requests through the same stream as the originating client request
749+
let requestId: RequestId | undefined = options?.relatedRequestId;
743750

751+
// Then override with message.id for responses/errors
744752
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
745753
requestId = message.id;
746754
}

0 commit comments

Comments
 (0)