ssh_read_output
Retrieve output from an active SSH session in real-time, using the session ID and optional timeout. Clear the buffer post-read for streamlined command execution and output management.
Instructions
Read output from an interactive shell session
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| clearBuffer | No | Clear the output buffer after reading | |
| sessionId | Yes | Interactive session ID | |
| timeout | No | Timeout in milliseconds to wait for output |
Input Schema (JSON Schema)
{
"properties": {
"clearBuffer": {
"default": true,
"description": "Clear the output buffer after reading",
"type": "boolean"
},
"sessionId": {
"description": "Interactive session ID",
"type": "string"
},
"timeout": {
"default": 5000,
"description": "Timeout in milliseconds to wait for output",
"type": "number"
}
},
"required": [
"sessionId"
],
"type": "object"
}
Implementation Reference
- src/index.ts:1004-1053 (handler)The handler function that executes the 'ssh_read_output' tool. It retrieves the interactive shell session by sessionId, waits for output (using the session's buffer or new data event) with a configurable timeout, optionally clears the buffer, and returns the output as text content.private async handleReadOutput(args: unknown) { const params = ReadOutputSchema.parse(args); const session = shellSessions.get(params.sessionId); if (!session) { throw new McpError( ErrorCode.InvalidParams, `Session ID '${params.sessionId}' not found` ); } try { // Wait for output with timeout const output = await new Promise<string>((resolve, reject) => { const timeout = setTimeout(() => { resolve(session.buffer); }, params.timeout); if (session.buffer) { clearTimeout(timeout); resolve(session.buffer); } else { session.emitter.once('data', () => { clearTimeout(timeout); resolve(session.buffer); }); } }); const result = output; if (params.clearBuffer) { session.buffer = ''; } return { content: [ { type: 'text', text: `Output from session '${params.sessionId}':\n${result}`, }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to read output: ${error instanceof Error ? error.message : String(error)}` ); } }
- src/index.ts:113-117 (schema)Zod schema defining the input parameters for the 'ssh_read_output' tool: sessionId (required), timeout (default 5000ms), clearBuffer (default true). Used for validation in the handler.const ReadOutputSchema = z.object({ sessionId: z.string().describe('Interactive session ID'), timeout: z.number().default(5000).describe('Timeout in milliseconds to wait for output'), clearBuffer: z.boolean().default(true).describe('Clear the output buffer after reading') });
- src/index.ts:348-358 (registration)Tool registration in the ListTools response, defining name, description, and inputSchema matching the ReadOutputSchema.name: 'ssh_read_output', description: 'Read output from an interactive shell session', inputSchema: { type: 'object', properties: { sessionId: { type: 'string', description: 'Interactive session ID' }, timeout: { type: 'number', default: 5000, description: 'Timeout in milliseconds to wait for output' }, clearBuffer: { type: 'boolean', default: true, description: 'Clear the output buffer after reading' } }, required: ['sessionId'] },
- src/index.ts:501-502 (registration)Dispatch case in the CallToolRequest handler switch statement that routes calls to the handleReadOutput function.case 'ssh_read_output': return await this.handleReadOutput(args);