@robosystems/mcp 0.3.7 → 0.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +64 -76
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -452,93 +452,81 @@ class RoboSystemsMCPClient {
452
452
  }
453
453
 
454
454
  async handleSSEResponse(response, toolName) {
455
- return new Promise((resolve, reject) => {
456
- const events = []
457
- const operationId = response.headers.get('x-operation-id') || `sse-${Date.now()}`
458
-
459
- const eventSource = this.connectionPool.getConnection(operationId, response.url, this.headers)
455
+ // Read SSE events directly from the POST response body instead of opening
456
+ // a new EventSource. EventSource always uses GET, which 405s on this
457
+ // POST-only endpoint. The SSE stream is already in the response body.
458
+ const events = []
459
+ const reader = response.body.getReader()
460
+ const decoder = new TextDecoder()
461
+ let buffer = ''
460
462
 
461
- const timeout = setTimeout(() => {
462
- this.connectionPool.closeConnection(operationId)
463
- reject(new Error('SSE timeout after 5 minutes'))
464
- }, 300000)
463
+ let timeoutId
464
+ const timeout = new Promise((_, reject) => {
465
+ timeoutId = setTimeout(() => reject(new Error('SSE timeout after 5 minutes')), 300000)
466
+ })
465
467
 
466
- eventSource.onmessage = (event) => {
467
- try {
468
- const data = JSON.parse(event.data)
469
- events.push({ event: event.type || 'message', data })
470
- } catch (e) {
471
- console.error('Failed to parse SSE event:', e)
472
- }
473
- }
468
+ const readStream = async () => {
469
+ try {
470
+ while (true) {
471
+ const { done, value } = await reader.read()
472
+ if (done) break
473
+
474
+ buffer += decoder.decode(value, { stream: true })
475
+ const lines = buffer.split(/\r?\n/)
476
+ buffer = lines.pop() || ''
477
+
478
+ for (const line of lines) {
479
+ if (line.startsWith('data:')) {
480
+ const dataStr = line.slice(5).trim()
481
+ if (!dataStr || dataStr === '[DONE]') continue
482
+
483
+ let data
484
+ try {
485
+ data = JSON.parse(dataStr)
486
+ } catch (e) {
487
+ console.error('Failed to parse SSE event:', e)
488
+ continue
489
+ }
474
490
 
475
- eventSource.addEventListener('complete', () => {
476
- clearTimeout(timeout)
477
- const result = this.aggregateStreamedResults(events, toolName)
478
- resolve(result)
479
- })
491
+ const eventType = data.event || 'message'
480
492
 
481
- // Listen for the actual server event types
482
- eventSource.addEventListener('operation_completed', () => {
483
- clearTimeout(timeout)
484
- this.connectionPool.closeConnection(operationId)
485
- const result = this.aggregateStreamedResults(events, toolName)
486
- resolve(result)
487
- })
493
+ if (eventType === 'operation_error') {
494
+ throw new Error(data.data?.error || data.data?.message || 'Operation failed')
495
+ }
488
496
 
489
- eventSource.addEventListener('operation_error', (event) => {
490
- clearTimeout(timeout)
491
- this.connectionPool.closeConnection(operationId)
492
- try {
493
- const data = JSON.parse(event.data)
494
- reject(new Error(data.error || data.message || 'Operation failed'))
495
- } catch (_e) {
496
- reject(new Error('Operation failed'))
497
- }
498
- })
497
+ if (eventType === 'operation_progress' || eventType === 'progress') {
498
+ const progress = data.data?.percentage ? `${data.data.percentage}% - ` : ''
499
+ console.error(`Progress: ${progress}${data.data?.message || 'Processing...'}`)
500
+ continue
501
+ }
499
502
 
500
- eventSource.addEventListener('error', () => {
501
- clearTimeout(timeout)
502
- this.connectionPool.closeConnection(operationId)
503
+ events.push({ event: eventType, data: data.data ?? data })
503
504
 
504
- if (events.length > 0) {
505
- const result = this.aggregateStreamedResults(events, toolName)
506
- resolve(result)
507
- } else {
508
- reject(new Error('SSE connection failed'))
505
+ if (eventType === 'complete' || eventType === 'operation_completed') {
506
+ return
507
+ }
508
+ } else if (line.startsWith('event:')) {
509
+ // event: lines are handled via the data: payload's event field
510
+ }
511
+ }
509
512
  }
510
- })
513
+ } finally {
514
+ reader.cancel().catch(() => {})
515
+ }
516
+ }
511
517
 
512
- // Handle specific event types
513
- eventSource.addEventListener('query_chunk', (event) => {
514
- try {
515
- const data = JSON.parse(event.data)
516
- events.push({ event: 'query_chunk', data })
517
- } catch (e) {
518
- console.error('Failed to parse query chunk:', e)
519
- }
520
- })
518
+ try {
519
+ await Promise.race([readStream(), timeout])
520
+ } finally {
521
+ clearTimeout(timeoutId)
522
+ reader.cancel().catch(() => {})
523
+ }
521
524
 
522
- eventSource.addEventListener('progress', (event) => {
523
- try {
524
- const data = JSON.parse(event.data)
525
- console.error(`Progress: ${data.message || 'Processing...'}`)
526
- } catch (_e) {
527
- // Ignore progress parsing errors
528
- }
529
- })
525
+ if (events.length === 0) {
526
+ throw new Error('SSE connection failed')
527
+ }
530
528
 
531
- // Also listen for operation_progress events
532
- eventSource.addEventListener('operation_progress', (event) => {
533
- try {
534
- const data = JSON.parse(event.data)
535
- const progress = data.percentage ? `${data.percentage}% - ` : ''
536
- console.error(`Progress: ${progress}${data.message || 'Processing...'}`)
537
- } catch (_e) {
538
- // Ignore progress parsing errors
539
- }
540
- })
541
- })
529
+ return this.aggregateStreamedResults(events, toolName)
542
530
  }
543
531
 
544
532
  async handleNDJSONResponse(response) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@robosystems/mcp",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "MCP client adapter for connecting AI agents to RoboSystems Financial Knowledge Graph API endpoints with workspace support",
5
5
  "type": "module",
6
6
  "main": "index.js",