Skip to content

Commit c6ab731

Browse files
committed
refactor: simplify McpSSETransport initialization by using getCurrentAgent util
1 parent c091fac commit c6ab731

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

packages/agents/src/mcp/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export abstract class McpAgent<
8080
}
8181

8282
/** Get the unique WebSocket. SSE transport only. */
83-
private getWebSocket() {
83+
getWebSocket() {
8484
const websockets = Array.from(this.getConnections());
8585
if (websockets.length === 0) {
8686
return null;
@@ -110,10 +110,7 @@ export abstract class McpAgent<
110110
private initTransport() {
111111
switch (this.getTransportType()) {
112112
case "sse": {
113-
return new McpSSETransport({
114-
getWebSocket: () => this.getWebSocket(),
115-
sessionId: this.getSessionId()
116-
});
113+
return new McpSSETransport();
117114
}
118115
case "streamable-http": {
119116
return new StreamableHTTPServerTransport({});

packages/agents/src/mcp/transport.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ import type { McpAgent } from ".";
1515
import { MessageType } from "../ai-types";
1616
import { MCP_HTTP_METHOD_HEADER, MCP_MESSAGE_HEADER } from "./utils";
1717

18-
export interface McpSSETransportOptions {
19-
getWebSocket: () => WebSocket | null;
20-
sessionId?: string;
21-
}
22-
2318
export class McpSSETransport implements Transport {
24-
sessionId?: string;
19+
sessionId: string;
2520
// Set by the server in `server.connect(transport)`
2621
onclose?: () => void;
2722
onerror?: (error: Error) => void;
2823
onmessage?: (message: JSONRPCMessage, extra?: MessageExtraInfo) => void;
2924

3025
private _getWebSocket: () => WebSocket | null;
3126
private _started = false;
32-
constructor(options: McpSSETransportOptions) {
33-
this._getWebSocket = options.getWebSocket;
34-
this.sessionId = options.sessionId;
27+
constructor() {
28+
const { agent } = getCurrentAgent<McpAgent>();
29+
if (!agent)
30+
throw new Error("McpAgent was not found in Transport constructor");
31+
32+
this.sessionId = agent.getSessionId();
33+
this._getWebSocket = () => agent.getWebSocket();
3534
}
3635

3736
async start() {

0 commit comments

Comments
 (0)