Skip to main content
Glama

fs_read

Read files from remote servers over SSH to access content for automation, analysis, or management tasks.

Instructions

Reads a file from the remote system

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionIdYesSSH session ID
pathYesFile path to read
encodingNoFile encoding (default: utf8)

Implementation Reference

  • The handler function 'readFile' that implements the 'fs_read' tool logic.
    export async function readFile(
      sessionId: string,
      path: string,
      encoding: string = 'utf8'
    ): Promise<string> {
      logger.debug('Reading file', { sessionId, path, encoding });
      
      const session = sessionManager.getSession(sessionId);
      if (!session) {
        throw new Error(`Session ${sessionId} not found or expired`);
      }
      
      try {
        const data = await session.sftp.get(path);
        const result = Buffer.isBuffer(data) ? data.toString(encoding as any) : String(data);
        logger.debug('File read successfully', { sessionId, path, size: result.length });
        return result;
      } catch (error) {
        logger.error('Failed to read file', { sessionId, path, error });
        throw wrapError(
          error,
          ErrorCode.EFS,
          `Failed to read file ${path}. Check if the file exists and is readable.`
        );
      }
    }
  • Zod schema for input validation for the 'fs_read' tool.
    export const FSReadSchema = z.object({
      sessionId: z.string().min(1),
      path: z.string().min(1),
      encoding: z.string().optional().default('utf8')
    });
  • src/mcp.ts:179-190 (registration)
    Registration of the 'fs_read' tool in the MCP server setup.
      name: 'fs_read',
      description: 'Reads a file from the remote system',
      inputSchema: {
        type: 'object',
        properties: {
          sessionId: { type: 'string', description: 'SSH session ID' },
          path: { type: 'string', description: 'File path to read' },
          encoding: { type: 'string', description: 'File encoding (default: utf8)' }
        },
        required: ['sessionId', 'path']
      }
    },
  • The case block in 'setupToolHandlers' that routes the 'fs_read' request to the 'readFile' handler.
    case 'fs_read': {
      const params = FSReadSchema.parse(args);
      const result = await readFile(params.sessionId, params.path, params.encoding);
      logger.info('File read', { sessionId: params.sessionId, path: params.path });
      return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/oaslananka/mcp-ssh-tool'

If you have feedback or need assistance with the MCP directory API, please join our Discord server