Skip to content

Commit a910d78

Browse files
committed
refactor: replace buildMessageExtraInfo with direct MessageExtraInfo construction in onSSEMcpMessage
1 parent c6ab731 commit a910d78

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

packages/agents/src/mcp/index.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -88,24 +88,6 @@ export abstract class McpAgent<
8888
return websockets[0];
8989
}
9090

91-
/** Build MessageExtraInfo from extracted headers and auth info */
92-
private buildMessageExtraInfo(extraInfo?: {
93-
headers: Record<string, string>;
94-
}): MessageExtraInfo | undefined {
95-
if (!extraInfo) return;
96-
97-
const headers = { ...extraInfo.headers };
98-
99-
// Remove internal headers that are not part of the original request
100-
delete headers[MCP_HTTP_METHOD_HEADER];
101-
delete headers[MCP_MESSAGE_HEADER];
102-
delete headers["upgrade"];
103-
104-
return {
105-
requestInfo: { headers }
106-
};
107-
}
108-
10991
/** Returns a new transport matching the type of the Agent. */
11092
private initTransport() {
11193
switch (this.getTransportType()) {
@@ -210,7 +192,7 @@ export abstract class McpAgent<
210192
async onSSEMcpMessage(
211193
_sessionId: string,
212194
messageBody: unknown,
213-
extraInfo?: { headers: Record<string, string> }
195+
extraInfo?: MessageExtraInfo
214196
): Promise<Error | null> {
215197
// Since we address the DO via both the protocol and the session id,
216198
// this should never happen, but let's enforce it just in case
@@ -232,10 +214,7 @@ export abstract class McpAgent<
232214
return null; // Message was handled by elicitation system
233215
}
234216

235-
// Build extra info with auth and request headers, matching StreamableHTTP behavior
236-
const extra = this.buildMessageExtraInfo(extraInfo);
237-
238-
this._transport?.onmessage?.(parsedMessage, extra);
217+
this._transport?.onmessage?.(parsedMessage, extraInfo);
239218
return null;
240219
} catch (error) {
241220
console.error("Error forwarding message to SSE:", error);

packages/agents/src/mcp/utils.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
JSONRPCMessageSchema,
33
type JSONRPCMessage,
4+
type MessageExtraInfo,
45
InitializeRequestSchema,
56
isJSONRPCResponse,
67
isJSONRPCNotification
@@ -682,9 +683,24 @@ export const createLegacySseHandler = (
682683
});
683684

684685
const messageBody = await request.json();
685-
const error = await agent.onSSEMcpMessage(sessionId, messageBody, {
686-
headers: Object.fromEntries(request.headers.entries())
687-
});
686+
687+
// Build MessageExtraInfo with filtered headers
688+
const headers = Object.fromEntries(request.headers.entries());
689+
690+
// Remove internal headers that are not part of the original request
691+
delete headers[MCP_HTTP_METHOD_HEADER];
692+
delete headers[MCP_MESSAGE_HEADER];
693+
delete headers["upgrade"];
694+
695+
const extraInfo: MessageExtraInfo = {
696+
requestInfo: { headers }
697+
};
698+
699+
const error = await agent.onSSEMcpMessage(
700+
sessionId,
701+
messageBody,
702+
extraInfo
703+
);
688704

689705
if (error) {
690706
return new Response(error.message, {

0 commit comments

Comments
 (0)