mcp-use
Version:
A utility library for integrating Model Context Protocol (MCP) with LangChain, Zod, and related tools. Provides helpers for schema conversion, event streaming, and SDK usage.
44 lines (43 loc) • 1.31 kB
JavaScript
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { logger } from '../logging.js';
import { ConnectionManager } from './base.js';
export class SseConnectionManager extends ConnectionManager {
url;
opts;
_transport = null;
/**
* Create an SSE connection manager.
*
* @param url The SSE endpoint URL.
* @param opts Optional transport options (auth, headers, etc.).
*/
constructor(url, opts) {
super();
this.url = typeof url === 'string' ? new URL(url) : url;
this.opts = opts;
}
/**
* Spawn a new `SSEClientTransport` and start the connection.
*/
async establishConnection() {
this._transport = new SSEClientTransport(this.url, this.opts);
logger.debug(`${this.constructor.name} connected successfully`);
return this._transport;
}
/**
* Close the underlying transport and clean up resources.
*/
async closeConnection(_connection) {
if (this._transport) {
try {
await this._transport.close();
}
catch (e) {
logger.warn(`Error closing SSE transport: ${e}`);
}
finally {
this._transport = null;
}
}
}
}