@agentpaid/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.
76 lines (75 loc) • 3.09 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StdioConnector = void 0;
const node_process_1 = __importDefault(require("node:process"));
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
const logging_js_1 = require("../logging.js");
const stdio_js_1 = require("../task_managers/stdio.js");
const base_js_1 = require("./base.js");
class StdioConnector extends base_js_1.BaseConnector {
command;
args;
env;
errlog;
clientInfo;
constructor({ command = 'npx', args = [], env, errlog = node_process_1.default.stderr, ...rest } = {}) {
super(rest);
this.command = command;
this.args = args;
this.env = env;
this.errlog = errlog;
this.clientInfo = rest.clientInfo ?? { name: 'stdio-connector', version: '1.0.0' };
}
/** Establish connection to the MCP implementation. */
async connect() {
if (this.connected) {
logging_js_1.logger.debug('Already connected to MCP implementation');
return;
}
logging_js_1.logger.debug(`Connecting to MCP implementation via stdio: ${this.command}`);
try {
// 1. Build server parameters for the transport
// Merge env with process.env, filtering out undefined values
let mergedEnv;
if (this.env) {
mergedEnv = {};
// First add process.env values (excluding undefined)
for (const [key, value] of Object.entries(node_process_1.default.env)) {
if (value !== undefined) {
mergedEnv[key] = value;
}
}
// Then override with provided env
Object.assign(mergedEnv, this.env);
}
const serverParams = {
command: this.command,
args: this.args,
env: mergedEnv,
};
// 2. Start the connection manager -> returns a live transport
this.connectionManager = new stdio_js_1.StdioConnectionManager(serverParams, this.errlog);
const transport = await this.connectionManager.start();
// 3. Create & connect the MCP client
this.client = new index_js_1.Client(this.clientInfo, this.opts.clientOptions);
await this.client.connect(transport);
this.connected = true;
logging_js_1.logger.debug(`Successfully connected to MCP implementation: ${this.command}`);
}
catch (err) {
logging_js_1.logger.error(`Failed to connect to MCP implementation: ${err}`);
await this.cleanupResources();
throw err;
}
}
get publicIdentifier() {
return {
'type': 'stdio',
'command&args': `${this.command} ${this.args.join(' ')}`,
};
}
}
exports.StdioConnector = StdioConnector;