avancement planning
This commit is contained in:
+14
-17
@@ -1,7 +1,7 @@
|
||||
import process from "node:process";
|
||||
import { ReadBuffer, serializeMessage } from "../shared/stdio.js";
|
||||
import process from 'node:process';
|
||||
import { ReadBuffer, serializeMessage } from '../shared/stdio.js';
|
||||
/**
|
||||
* Server transport for stdio: this communicates with a MCP client by reading from the current process' stdin and writing to stdout.
|
||||
* Server transport for stdio: this communicates with an MCP client by reading from the current process' stdin and writing to stdout.
|
||||
*
|
||||
* This transport is only available in Node.js environments.
|
||||
*/
|
||||
@@ -17,8 +17,7 @@ export class StdioServerTransport {
|
||||
this.processReadBuffer();
|
||||
};
|
||||
this._onerror = (error) => {
|
||||
var _a;
|
||||
(_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
||||
this.onerror?.(error);
|
||||
};
|
||||
}
|
||||
/**
|
||||
@@ -26,32 +25,30 @@ export class StdioServerTransport {
|
||||
*/
|
||||
async start() {
|
||||
if (this._started) {
|
||||
throw new Error("StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.");
|
||||
throw new Error('StdioServerTransport already started! If using Server class, note that connect() calls start() automatically.');
|
||||
}
|
||||
this._started = true;
|
||||
this._stdin.on("data", this._ondata);
|
||||
this._stdin.on("error", this._onerror);
|
||||
this._stdin.on('data', this._ondata);
|
||||
this._stdin.on('error', this._onerror);
|
||||
}
|
||||
processReadBuffer() {
|
||||
var _a, _b;
|
||||
while (true) {
|
||||
try {
|
||||
const message = this._readBuffer.readMessage();
|
||||
if (message === null) {
|
||||
break;
|
||||
}
|
||||
(_a = this.onmessage) === null || _a === void 0 ? void 0 : _a.call(this, message);
|
||||
this.onmessage?.(message);
|
||||
}
|
||||
catch (error) {
|
||||
(_b = this.onerror) === null || _b === void 0 ? void 0 : _b.call(this, error);
|
||||
this.onerror?.(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
async close() {
|
||||
var _a;
|
||||
// Remove our event listeners first
|
||||
this._stdin.off("data", this._ondata);
|
||||
this._stdin.off("error", this._onerror);
|
||||
this._stdin.off('data', this._ondata);
|
||||
this._stdin.off('error', this._onerror);
|
||||
// Check if we were the only data listener
|
||||
const remainingDataListeners = this._stdin.listenerCount('data');
|
||||
if (remainingDataListeners === 0) {
|
||||
@@ -61,16 +58,16 @@ export class StdioServerTransport {
|
||||
}
|
||||
// Clear the buffer and notify closure
|
||||
this._readBuffer.clear();
|
||||
(_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this);
|
||||
this.onclose?.();
|
||||
}
|
||||
send(message) {
|
||||
return new Promise((resolve) => {
|
||||
return new Promise(resolve => {
|
||||
const json = serializeMessage(message);
|
||||
if (this._stdout.write(json)) {
|
||||
resolve();
|
||||
}
|
||||
else {
|
||||
this._stdout.once("drain", resolve);
|
||||
this._stdout.once('drain', resolve);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user