Skip to main content
Glama

read_file

Read a file's full body and metadata by its ID. Returns content, title, path, tags, and estimated token count for context budgeting.

Instructions

Read one file's full body and metadata by ID. Read-only; no side effects, auth, or rate limits. Returns title, path, content, tags, est_tokens (so you can budget context before opening more files), and timestamps. Throws if the ID is unknown. Use for a single known file. Prefer describe_file to inspect without paying body tokens; read_files for batches; read_file_lines/read_section for partial reads; read_file_by_path when you only have the absolute path.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
idYesFile ID

Implementation Reference

  • The core implementation of the read_file tool. Queries the SQLite database for a file record by ID, reads the file content from disk using readFileSync, and returns the combined record with content.
    export function readFile(id: number): FileRecordWithContent {
      const db = getDatabase();
    
      const fileRecord = db.prepare("SELECT * FROM files WHERE id = ?").get(id) as FileRecord | undefined;
      if (!fileRecord) {
        throw new Error(`File not found: ${id}`);
      }
    
      const content = readFileSync(fileRecord.path, "utf8");
    
      return {
        ...fileRecord,
        content,
      };
    }
  • MCP Server tool registration for 'read_file'. Defines the input schema (id: number), calls the core readFile function, and annotates the result with token estimates before returning as JSON.
    server.tool(
      "read_file",
      "Read one file's full body and metadata by ID. Read-only; no side effects, auth, or rate limits. Returns title, path, content, tags, est_tokens (so you can budget context before opening more files), and timestamps. Throws if the ID is unknown. Use for a single known file. Prefer `describe_file` to inspect without paying body tokens; `read_files` for batches; `read_file_lines`/`read_section` for partial reads; `read_file_by_path` when you only have the absolute path.",
      {
        id: z.number().describe("File ID"),
      },
      async ({ id }) => {
        const result = readFile(id);
        return {
          content: [{ type: "text", text: JSON.stringify(annotateTokens(result), null, 2) }],
        };
      }
    );
  • Zod schema for the read_file tool - accepts a single numeric 'id' parameter.
      id: z.number().describe("File ID"),
    },
  • Type definition for the return type of readFile, extending FileRecord with content and optional git_warning.
    export interface FileRecordWithContent extends FileRecord {
      content: string;
      git_warning?: string;
    }
  • Re-exports the readFile function from the core package so it can be imported by the MCP server app.
    import { createFile, readFile, updateFile, deleteFile, listFiles, moveFile, createFolder, deleteFolder, listProjectFolders, slugify } from "./files/index.js";
    export { createFile, readFile, updateFile, deleteFile, listFiles, moveFile, createFolder, deleteFolder, listProjectFolders, slugify };
    export { parseOutline, findSection, replaceSection, type OutlineNode } from "./files/sections.js";
    export { assertPathInside } from "./util/safety.js";
    export {
      addTags, removeTags, setFavorite, search,
      registerProject, unregisterProject, discoverFiles, listTags, listProjects,
      findRelated, getTagsForFiles, type RelatedFileRecord,
    } from "./metadata/index.js";
    export { commitFile, getHistory, getDiff, restoreVersion, syncBackup, syncGlobalVault, getGlobalRemote, setGlobalRemote, isValidGitRemoteUrl, type SyncStage } from "./git/index.js";
    export { withLock } from "./util/safety.js";
    export { createFileWatcher, type WatcherEvent } from "./watcher/index.js";
    export { bundleSearch, type BundleFormat, type BundleOptions, type BundleResult, type BundleIncludedItem, type BundleSkippedItem } from "./bundle/index.js";
    export { estimateTokensFromBuffer, estimateTokensFromString } from "./util/tokens.js";
    export type * from "./types.js";
    export { clipUrl, ClipError, type ClipErrorCode, type ClipUrlOptions } from "./clip/index.js";
    export {
      whatsNew, resolveSince,
      type WhatsNewOptions, type WhatsNewResult, type WhatsNewEntry, type ChangeKind,
    } from "./whats-new/index.js";
    export {
      projectMap,
      type ProjectMapOptions, type ProjectMapResult, type ProjectMapStats,
    } from "./project-map/index.js";
Behavior5/5

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

No annotations, so description carries full burden. States read-only, no side effects, auth, or rate limits. Discloses error condition ('Throws if ID unknown') and return fields (title, path, content, tags, est_tokens, timestamps).

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?

Concise multi-purpose sentences: main action, behavioral claims, return fields, error, and usage guidance. No fluff.

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

Completeness5/5

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

Given simplicity (1 param, no output schema), description covers purpose, usage, behavior, error, and sibling differentiation comprehensively.

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?

Schema has 1 param with description 'File ID' (100% coverage). Description mentions file by ID but adds no further detail beyond schema. Baseline 3 applies.

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?

Clearly states 'Read one file's full body and metadata by ID' with specific verb and resource. Explicitly distinguishes from siblings by naming alternatives.

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

Usage Guidelines5/5

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

Provides explicit when-to-use guidance: 'Use for a single known file.' Lists alternatives: describe_file, read_files, read_file_lines, read_section, read_file_by_path.

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/safiyu/ctxnest'

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