Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-crabs-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"agents": patch
---

When handling MCP server requests use relatedRequestId in TransportOptions to send the response down a POST stream if supported (streamable-http)
35 changes: 19 additions & 16 deletions examples/mcp-elicitation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,31 @@ export class MyAgent extends Agent<Env, State> {
confirm: z.boolean().describe("Do you want to increase the counter?")
}
},
async ({ confirm }) => {
async ({ confirm }, extra) => {
if (!confirm) {
return {
content: [{ type: "text", text: "Counter increase cancelled." }]
};
}
try {
const basicInfo = await this.server.server.elicitInput({
message: "By how much do you want to increase the counter?",
requestedSchema: {
type: "object",
properties: {
amount: {
type: "number",
title: "Amount",
description: "The amount to increase the counter by",
minLength: 1
}
},
required: ["amount"]
}
});
const basicInfo = await this.server.server.elicitInput(
{
message: "By how much do you want to increase the counter?",
requestedSchema: {
type: "object",
properties: {
amount: {
type: "number",
title: "Amount",
description: "The amount to increase the counter by",
minLength: 1
}
},
required: ["amount"]
}
},
{ relatedRequestId: extra.requestId }
);

if (basicInfo.action !== "accept" || !basicInfo.content) {
return {
Expand Down
16 changes: 12 additions & 4 deletions packages/agents/src/mcp/worker-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
* Based on @hono/mcp transport implementation (https://github.com/honojs/middleware/tree/main/packages/mcp)
*/

import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
import type {
Transport,
TransportSendOptions
} from "@modelcontextprotocol/sdk/shared/transport.js";
import type {
JSONRPCMessage,
RequestId,
Expand Down Expand Up @@ -357,7 +360,7 @@ export class WorkerTransport implements Transport {
const acceptHeader = request.headers.get("Accept");
if (
!acceptHeader?.includes("application/json") ||
!acceptHeader.includes("text/event-stream")
!acceptHeader?.includes("text/event-stream")
) {
return new Response(
JSON.stringify({
Expand Down Expand Up @@ -738,9 +741,14 @@ export class WorkerTransport implements Transport {
this.onclose?.();
}

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

// Then override with message.id for responses/errors
if (isJSONRPCResponse(message) || isJSONRPCError(message)) {
requestId = message.id;
}
Expand Down
Loading
Loading