Skip to main content
Glama
MrGNSS

Desktop Commander MCP

read_file

Read file contents from the file system, handling various text encodings and providing error details for unreadable files within allowed directories.

Instructions

Read the complete contents of a file from the file system. Handles various text encodings and provides detailed error messages if the file cannot be read. Only works within allowed directories.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • Core handler function that validates the file path for security and reads the file contents using Node.js fs.promises.readFile.
    export async function readFile(filePath: string): Promise<string> {
        const validPath = await validatePath(filePath);
        return fs.readFile(validPath, "utf-8");
    }
  • Zod schema defining the input for read_file tool: requires a 'path' string parameter.
    export const ReadFileArgsSchema = z.object({
      path: z.string(),
    });
  • src/server.ts:126-131 (registration)
    Tool registration in the server's ListTools response, specifying name, description, and input schema.
    name: "read_file",
    description:
      "Read the complete contents of a file from the file system. " +
      "Handles various text encodings and provides detailed error messages " +
      "if the file cannot be read. Only works within allowed directories.",
    inputSchema: zodToJsonSchema(ReadFileArgsSchema),
  • Server dispatch handler that parses arguments, calls the readFile function, and formats the response for MCP protocol.
    case "read_file": {
      const parsed = ReadFileArgsSchema.parse(args);
      const content = await readFile(parsed.path);
      return {
        content: [{ type: "text", text: content }],
      };
    }
  • Security helper function that validates paths are within allowed directories, handles symlinks and home expansion.
    export async function validatePath(requestedPath: string): Promise<string> {
        const expandedPath = expandHome(requestedPath);
        const absolute = path.isAbsolute(expandedPath)
            ? path.resolve(expandedPath)
            : path.resolve(process.cwd(), expandedPath);
            
        const normalizedRequested = normalizePath(absolute);
    
        // Check if path is within allowed directories
        const isAllowed = allowedDirectories.some(dir => normalizedRequested.startsWith(normalizePath(dir)));
        if (!isAllowed) {
            throw new Error(`Access denied - path outside allowed directories: ${absolute}`);
        }
    
        // Handle symlinks by checking their real path
        try {
            const realPath = await fs.realpath(absolute);
            const normalizedReal = normalizePath(realPath);
            const isRealPathAllowed = allowedDirectories.some(dir => normalizedReal.startsWith(normalizePath(dir)));
            if (!isRealPathAllowed) {
                throw new Error("Access denied - symlink target outside allowed directories");
            }
            return realPath;
        } catch (error) {
            // For new files that don't exist yet, verify parent directory
            const parentDir = path.dirname(absolute);
            try {
                const realParentPath = await fs.realpath(parentDir);
                const normalizedParent = normalizePath(realParentPath);
                const isParentAllowed = allowedDirectories.some(dir => normalizedParent.startsWith(normalizePath(dir)));
                if (!isParentAllowed) {
                    throw new Error("Access denied - parent directory outside allowed directories");
                }
                return absolute;
            } catch {
                throw new Error(`Parent directory does not exist: ${parentDir}`);
            }
        }
    }
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden and adds valuable behavioral context: it handles various text encodings, provides detailed error messages, and restricts to allowed directories. This covers key operational traits like input handling, error behavior, and security constraints, though it omits details like performance limits or output format.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is front-loaded with the core purpose in the first sentence, followed by additional context in two concise sentences. Each sentence adds value: the first defines the action, the second covers encoding and errors, and the third specifies directory restrictions, with no wasted words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (single parameter, no output schema, no annotations), the description is mostly complete: it explains what the tool does, behavioral traits, and usage constraints. However, it lacks details on return values (e.g., content format, encoding specifics) and does not fully address parameter semantics, leaving minor gaps.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 1 parameter with 0% description coverage, so the description must compensate. It implies the 'path' parameter by mentioning 'file from the file system' and 'allowed directories,' but does not explicitly define 'path' or its format (e.g., absolute/relative paths, file extensions). This adds some meaning but falls short of fully documenting the parameter.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('Read the complete contents') and resource ('a file from the file system'), distinguishing it from siblings like 'read_multiple_files' (plural vs. single) and 'get_file_info' (metadata vs. contents). The verb 'Read' is precise and unambiguous.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for usage with 'Only works within allowed directories,' which implicitly suggests using 'list_allowed_directories' to check permissions. However, it lacks explicit alternatives (e.g., when to use 'read_multiple_files' for bulk operations) and does not state when not to use it, such as for binary files or large files that might be inefficient.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/MrGNSS/ClaudeDesktopCommander'

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