@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.
- package/index.js +64 -76
- 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
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
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
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
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
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
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
|
-
|
|
476
|
-
clearTimeout(timeout)
|
|
477
|
-
const result = this.aggregateStreamedResults(events, toolName)
|
|
478
|
-
resolve(result)
|
|
479
|
-
})
|
|
491
|
+
const eventType = data.event || 'message'
|
|
480
492
|
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
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
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
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
|
-
|
|
501
|
-
clearTimeout(timeout)
|
|
502
|
-
this.connectionPool.closeConnection(operationId)
|
|
503
|
+
events.push({ event: eventType, data: data.data ?? data })
|
|
503
504
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
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
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
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
|
-
|
|
523
|
-
|
|
524
|
-
|
|
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
|
-
|
|
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