connectWebSocket
Establish WebSocket connection to receive live cryptocurrency market data and trading updates from Bitget exchange.
Instructions
Connect to WebSocket for real-time data
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- src/server.ts:469-491 (handler)MCP tool handler for 'connectWebSocket' that connects the WebSocket client instance and returns success or error message.case 'connectWebSocket': { try { await this.wsClient.connect(); return { content: [ { type: 'text', text: 'WebSocket connected successfully', }, ], } as CallToolResult; } catch (error: any) { return { content: [ { type: 'text', text: `Failed to connect WebSocket: ${error.message}`, }, ], isError: true, } as CallToolResult; } }
- src/server.ts:239-247 (registration)Registration of the 'connectWebSocket' tool in the ListTools response, defining name, description, and input schema.{ name: 'connectWebSocket', description: 'Connect to WebSocket for real-time data', inputSchema: { type: 'object', properties: {}, required: [] }, },
- src/api/websocket-client.ts:41-79 (helper)Core WebSocket connection implementation in BitgetWebSocketClient.connect(), handling connection, timeout, and event setup.async connect(): Promise<void> { if (this.isConnected || this.isConnecting) { return; } this.isConnecting = true; logger.info('Connecting to Bitget WebSocket', { url: this.config.url }); try { this.ws = new WebSocket(this.config.url); this.setupEventHandlers(); return new Promise((resolve, reject) => { const timeout = setTimeout(() => { reject(new Error('WebSocket connection timeout')); }, 10000); this.ws!.once('open', () => { clearTimeout(timeout); this.isConnected = true; this.isConnecting = false; this.reconnectCount = 0; logger.info('WebSocket connected successfully'); this.startPing(); this.resubscribeAll(); resolve(); }); this.ws!.once('error', (error) => { clearTimeout(timeout); this.isConnecting = false; reject(error); }); }); } catch (error) { this.isConnecting = false; throw error; } }
- src/api/websocket-client.ts:307-314 (helper)Factory function used to create and initialize the BitgetWebSocketClient instance in the server.export function createBitgetWebSocketClient(config: BitgetConfig): BitgetWebSocketClient { return new BitgetWebSocketClient({ url: config.wsUrl, pingInterval: 30000, reconnectInterval: 5000, maxReconnects: 10 }); }