Skip to main content
Glama

file_read

Read file contents from local storage to access documents, notes, or operational data for AI-assisted analysis and task management.

Instructions

Читать содержимое файла

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesПуть к файлу

Implementation Reference

  • Core handler function for the 'file_read' tool. Sanitizes the file path to prevent directory traversal, reads the file content using Node.js fs.promises.readFile, logs the operation, and returns the content as a string. Handles errors appropriately.
    async readFile(filePath: string): Promise<string> {
      try {
        // Проверяем безопасность пути
        const safePath = this.sanitizePath(filePath);
        
        console.log(`📖 Чтение файла: ${safePath}`);
        
        const content = await fs.readFile(safePath, 'utf-8');
        
        console.log(`✅ Файл прочитан: ${safePath} (${content.length} символов)`);
        
        return content;
      } catch (error) {
        console.error('Ошибка чтения файла:', error);
        throw new Error(`Ошибка чтения файла: ${error}`);
      }
  • Input schema definition for the 'file_read' tool, requiring a 'path' string parameter. Part of the tool list returned by ListToolsRequestSchema handler.
    {
      name: 'file_read',
      description: 'Читать содержимое файла',
      inputSchema: {
        type: 'object',
        properties: {
          path: {
            type: 'string',
            description: 'Путь к файлу',
          },
        },
        required: ['path'],
      },
    },
  • src/server.ts:196-199 (registration)
    Registration of the 'file_read' tool handler in the main CallToolRequestSchema switch statement in the stdio server transport. Delegates execution to FileService.readFile.
    case 'file_read':
      return {
        content: await this.fileService.readFile(args.path as string)
      };
  • Helper method used by readFile to sanitize the file path, preventing path traversal attacks by restricting to the notes directory and removing dangerous characters.
    private sanitizePath(filePath: string): string {
      // Убираем потенциально опасные символы
      const cleanPath = filePath.replace(/[<>:"|?*]/g, '');
      
      // Разрешаем только относительные пути
      if (path.isAbsolute(cleanPath)) {
        throw new Error('Абсолютные пути не разрешены');
      }
      
      // Разрешаем только файлы в notes директории
      const resolvedPath = path.resolve(this.notesDir, cleanPath);
      const notesDirResolved = path.resolve(this.notesDir);
      
      if (!resolvedPath.startsWith(notesDirResolved)) {
        throw new Error('Доступ к файлу вне notes директории запрещен');
      }
      
      return resolvedPath;

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/Galiusbro/MCP'

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