diff options
| author | Shreya Keshive <[email protected]> | 2025-06-13 20:18:06 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-13 16:18:06 -0400 |
| commit | 1fa41af918f7253d4ceb9d78ab25c1415614508f (patch) | |
| tree | 047be270432cba07db44cd6062bce9d51ce36d3d /packages/core/src/tools/websocket-client-transport.ts | |
| parent | 491e367f7c36ede7025831a960a0440a4e08b579 (diff) | |
Support MCP StreamableHTTPClientTransport (#1014)
Diffstat (limited to 'packages/core/src/tools/websocket-client-transport.ts')
| -rw-r--r-- | packages/core/src/tools/websocket-client-transport.ts | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/packages/core/src/tools/websocket-client-transport.ts b/packages/core/src/tools/websocket-client-transport.ts deleted file mode 100644 index ff754c0a..00000000 --- a/packages/core/src/tools/websocket-client-transport.ts +++ /dev/null @@ -1,97 +0,0 @@ -/** - * @license - * Copyright 2025 Google LLC - * SPDX-License-Identifier: Apache-2.0 - */ - -import WebSocket from 'ws'; -import { - Transport, - TransportSendOptions, -} from '@modelcontextprotocol/sdk/shared/transport.js'; -import { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js'; -import { AuthInfo } from '@modelcontextprotocol/sdk/server/auth/types.js'; - -export class WebSocketClientTransport implements Transport { - private socket: WebSocket | null = null; - onclose?: () => void; - onerror?: (error: Error) => void; - onmessage?: ( - message: JSONRPCMessage, - extra?: { authInfo?: AuthInfo }, - ) => void; - - constructor(private readonly url: URL) {} - - async start(): Promise<void> { - return new Promise((resolve, reject) => { - const handshakeTimeoutDuration = 10000; - let connectionTimeout: NodeJS.Timeout | null = null; - - try { - this.socket = new WebSocket(this.url.toString(), { - handshakeTimeout: handshakeTimeoutDuration, - }); - - connectionTimeout = setTimeout(() => { - this.socket?.close(); - reject( - new Error( - `WebSocket connection timed out after ${handshakeTimeoutDuration}ms`, - ), - ); - }, handshakeTimeoutDuration); - - this.socket.on('open', () => { - clearTimeout(connectionTimeout!); - resolve(); - }); - - this.socket.on('message', (data) => { - try { - const parsedMessage: JSONRPCMessage = JSON.parse(data.toString()); - this.onmessage?.(parsedMessage, { authInfo: undefined }); // Auth unsupported currently - } catch (error: unknown) { - this.onerror?.( - error instanceof Error ? error : new Error(String(error)), - ); - } - }); - - this.socket.on('error', (error) => { - clearTimeout(connectionTimeout!); - this.onerror?.(error); - reject(error); - }); - - this.socket.on('close', () => { - clearTimeout(connectionTimeout!); - this.onclose?.(); - this.socket = null; - }); - } catch (error: unknown) { - clearTimeout(connectionTimeout!); - reject(error instanceof Error ? error : new Error(String(error))); - } - }); - } - - async close(): Promise<void> { - if (this.socket) { - this.socket.close(); - this.socket = null; - } - } - - async send( - message: JSONRPCMessage, - _options?: TransportSendOptions, - ): Promise<void> { - if (!this.socket || this.socket.readyState !== WebSocket.OPEN) { - throw new Error( - 'WebSocket is not connected or not open. Cannot send message.', - ); - } - this.socket.send(JSON.stringify(message)); - } -} |
