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.
51 lines (50 loc) • 1.59 kB
JavaScript
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { logger } from '../logging.js';
import { ConnectionManager } from './base.js';
export class StreamableHttpConnectionManager extends ConnectionManager {
url;
opts;
_transport = null;
/**
* Create a Streamable HTTP connection manager.
*
* @param url The HTTP 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 `StreamableHTTPClientTransport` and return it.
* The Client.connect() method will handle starting the transport.
*/
async establishConnection() {
this._transport = new StreamableHTTPClientTransport(this.url, this.opts);
logger.debug(`${this.constructor.name} created 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 Streamable HTTP transport: ${e}`);
}
finally {
this._transport = null;
}
}
}
/**
* Get the session ID from the transport if available.
*/
get sessionId() {
return this._transport?.sessionId;
}
}