create_connection
Open and track SSH sessions to remote machines using machine_id, enabling reuse for subsequent operations. Integrates with SSH MCP Server for secure command execution.
Instructions
Open an SSH session to the given machine and track it in global state so subsequent tool calls can reuse it.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| machine_id | Yes | ID from get_available_connections | |
| title | Yes | Purpose of this session (displayed in UIs) |
Implementation Reference
- src/index.ts:317-369 (handler)Handler for create_connection tool: destructures machine_id and title, validates, finds machine config, creates SSH2 client, generates UUID connection_id, connects using SSH config, on ready executes pwd to get current path, stores connection info in global connections Map, resolves with connection details.// create_connection if (name === "create_connection") { const { machine_id, title } = args as { machine_id: string; title: string }; if (!machine_id || !title) throw new Error("Both machine_id and title are required."); const machine = findMachine(machine_id); if (!machine) throw new Error(`Unknown machine_id '${machine_id}'.`); const client = new SSHClient(); const connection_id = crypto.randomUUID(); return new Promise((resolve, reject) => { client .on("ready", async () => { try { const { stdout } = await wrapExec(client, "pwd"); const connInfo = { connection_id, machine_id, title, currentPath: stdout.trim(), client, }; connections.set(connection_id, connInfo); resolve({ content: [ { type: "text", text: JSON.stringify( { connection_id, machine_id, title, currentPath: connInfo.currentPath, }, null, 2 ), }, ], }); } catch (e) { client.end(); reject(e); } }) .on("error", (err) => reject(err)) .connect(buildSshConfig(machine)); }); }
- src/index.ts:148-161 (registration)Registration of the create_connection tool in the ListTools response, including name, description, and input schema.{ name: "create_connection", description: "Open an SSH session to the given machine and track it in global state so subsequent tool calls can reuse it.", inputSchema: { type: "object", required: ["machine_id", "title"], properties: { machine_id: { type: "string", description: "ID from get_available_connections" }, title: { type: "string", description: "Purpose of this session (displayed in UIs)" }, }, additionalProperties: false, }, },
- src/index.ts:75-77 (helper)Helper function to find machine configuration by machine_id from availableMachines array.function findMachine(machine_id) { return availableMachines.find((m) => m.machine_id === machine_id); }
- src/index.ts:79-90 (helper)Helper function to build SSH connection config from machine spec, handling password or private key.function buildSshConfig(m) { const cfg: any = { host: m.ssh.host, port: m.ssh.port, username: m.ssh.username, }; if (m.ssh.password) cfg.password = m.ssh.password; if (m.ssh.keyPath) { // Lazy‑load fs so we don’t pay the I/O cost unless needed cfg.privateKey = readFileSync(m.ssh.keyPath, "utf8"); } return cfg;
- src/index.ts:93-111 (helper)Helper function to execute a command over SSH client and return promise with stdout, stderr, exitCode.function wrapExec(client, command): any { return new Promise((resolve, reject) => { let stdout = ""; let stderr = ""; client.exec(command, (err, stream) => { if (err) return reject(err); stream .on("close", (code) => { resolve({ stdout, stderr, exitCode: code }); }) .on("data", (data) => { stdout += data.toString(); }) .stderr.on("data", (data) => { stderr += data.toString(); }); }); }); }