Update client transports
This commit is contained in:
parent
87ae7ffc5a
commit
c39f8084d7
3 changed files with 36 additions and 14 deletions
|
|
@ -11,14 +11,19 @@ export class SSEClientTransport implements Transport {
|
|||
private _eventSource?: EventSource;
|
||||
private _endpoint?: URL;
|
||||
private _abortController?: AbortController;
|
||||
private _url: URL;
|
||||
|
||||
onclose?: () => void;
|
||||
onerror?: (error: Error) => void;
|
||||
onmessage?: (message: JSONRPCMessage) => void;
|
||||
|
||||
connect(url: URL): Promise<void> {
|
||||
constructor(url: URL) {
|
||||
this._url = url;
|
||||
}
|
||||
|
||||
start(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._eventSource = new EventSource(url.href);
|
||||
this._eventSource = new EventSource(this._url.href);
|
||||
this._abortController = new AbortController();
|
||||
|
||||
this._eventSource.onerror = (event) => {
|
||||
|
|
@ -35,8 +40,8 @@ export class SSEClientTransport implements Transport {
|
|||
const messageEvent = event as MessageEvent;
|
||||
|
||||
try {
|
||||
this._endpoint = new URL(messageEvent.data, url);
|
||||
if (this._endpoint.origin !== url.origin) {
|
||||
this._endpoint = new URL(messageEvent.data, this._url);
|
||||
if (this._endpoint.origin !== this._url.origin) {
|
||||
throw new Error(
|
||||
`Endpoint origin does not match connection origin: ${this._endpoint.origin}`,
|
||||
);
|
||||
|
|
|
|||
|
|
@ -31,22 +31,34 @@ export class StdioClientTransport implements Transport {
|
|||
private _process?: ChildProcess;
|
||||
private _abortController: AbortController = new AbortController();
|
||||
private _readBuffer: ReadBuffer = new ReadBuffer();
|
||||
private _serverParams: StdioServerParameters;
|
||||
|
||||
onclose?: () => void;
|
||||
onerror?: (error: Error) => void;
|
||||
onmessage?: (message: JSONRPCMessage) => void;
|
||||
|
||||
constructor(server: StdioServerParameters) {
|
||||
this._serverParams = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawns the server process and prepare to communicate with it.
|
||||
* Starts the server process and prepares to communicate with it.
|
||||
*/
|
||||
spawn(server: StdioServerParameters): Promise<void> {
|
||||
async start(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._process = spawn(server.command, server.args ?? [], {
|
||||
// The parent process may have sensitive secrets in its env, so don't inherit it automatically.
|
||||
env: server.env === undefined ? {} : { ...server.env },
|
||||
stdio: ["pipe", "pipe", "inherit"],
|
||||
signal: this._abortController.signal,
|
||||
});
|
||||
this._process = spawn(
|
||||
this._serverParams.command,
|
||||
this._serverParams.args ?? [],
|
||||
{
|
||||
// The parent process may have sensitive secrets in its env, so don't inherit it automatically.
|
||||
env:
|
||||
this._serverParams.env === undefined
|
||||
? {}
|
||||
: { ...this._serverParams.env },
|
||||
stdio: ["pipe", "pipe", "inherit"],
|
||||
signal: this._abortController.signal,
|
||||
},
|
||||
);
|
||||
|
||||
this._process.on("error", (error) => {
|
||||
if (error.name === "AbortError") {
|
||||
|
|
|
|||
|
|
@ -8,14 +8,19 @@ const SUBPROTOCOL = "mcp";
|
|||
*/
|
||||
export class WebSocketClientTransport implements Transport {
|
||||
private _socket?: WebSocket;
|
||||
private _url: URL;
|
||||
|
||||
onclose?: () => void;
|
||||
onerror?: (error: Error) => void;
|
||||
onmessage?: (message: JSONRPCMessage) => void;
|
||||
|
||||
connect(url: URL): Promise<void> {
|
||||
constructor(url: URL) {
|
||||
this._url = url;
|
||||
}
|
||||
|
||||
start(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this._socket = new WebSocket(url, SUBPROTOCOL);
|
||||
this._socket = new WebSocket(this._url, SUBPROTOCOL);
|
||||
|
||||
this._socket.onerror = (event) => {
|
||||
const error =
|
||||
|
|
|
|||
Loading…
Reference in a new issue