ssh_connect_with_credential
Establish SSH connections using stored credentials for secure remote server access. Simplify authentication by leveraging saved credential IDs and unique connection identifiers.
Instructions
Connect to SSH server using saved credentials
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionId | Yes | Unique identifier for this connection | |
| credentialId | Yes | Stored credential ID to use |
Implementation Reference
- src/index.ts:1178-1247 (handler)Handler function that implements the ssh_connect_with_credential tool. It retrieves stored credentials, establishes an SSH connection using NodeSSH, manages connection pooling, and updates credential usage timestamp.private async handleConnectWithCredential(args: unknown) { const params = ConnectWithCredentialSchema.parse(args); if (connectionPool.has(params.connectionId)) { throw new McpError( ErrorCode.InvalidParams, `Connection ID '${params.connectionId}' already exists` ); } const credential = credentialStore.get(params.credentialId); if (!credential) { throw new McpError( ErrorCode.InvalidParams, `Credential ID '${params.credentialId}' not found` ); } const ssh = new NodeSSH(); try { const connectConfig: any = { host: credential.host, port: credential.port, username: credential.username, }; if (credential.privateKeyPath) { const privateKey = await fs.readFile(credential.privateKeyPath, 'utf8'); connectConfig.privateKey = privateKey; if (credential.passphrase) { connectConfig.passphrase = credential.passphrase; } } else if (credential.password) { connectConfig.password = credential.password; } else { throw new McpError( ErrorCode.InvalidParams, 'Credential has neither password nor private key' ); } await ssh.connect(connectConfig); connectionPool.set(params.connectionId, ssh); // Initialize connection context connectionContexts.set(params.connectionId, { ssh, currentWorkingDirectory: undefined, defaultWorkingDirectory: undefined }); // Update last used timestamp credential.lastUsed = new Date().toISOString(); credentialStore.set(params.credentialId, credential); return { content: [ { type: 'text', text: `Successfully connected to ${credential.host}:${credential.port} as ${credential.username} using saved credential '${params.credentialId}' (Connection ID: ${params.connectionId})`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `SSH connection failed: ${error instanceof Error ? error.message : String(error)}` ); }
- src/index.ts:143-146 (schema)Zod schema for input validation of the ssh_connect_with_credential tool parameters.const ConnectWithCredentialSchema = z.object({ credentialId: z.string().describe('Stored credential ID to use'), connectionId: z.string().describe('Unique identifier for this connection') });
- src/index.ts:408-418 (registration)Tool registration in the list of available tools returned by ListToolsRequestSchema, including name, description, and input schema.{ name: 'ssh_connect_with_credential', description: 'Connect to SSH server using saved credentials', inputSchema: { type: 'object', properties: { credentialId: { type: 'string', description: 'Stored credential ID to use' }, connectionId: { type: 'string', description: 'Unique identifier for this connection' } }, required: ['credentialId', 'connectionId'] },
- src/index.ts:511-512 (registration)Dispatcher case in CallToolRequestSchema handler that routes calls to the specific handler function.case 'ssh_connect_with_credential': return await this.handleConnectWithCredential(args);