Skip to main content
Glama

view

Display file contents or directory listings from specified paths, optionally showing specific line ranges for focused viewing.

Instructions

View file contents or directory listing

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesAbsolute path to the file or directory
view_rangeNoOptional range of lines to view [start, end]

Implementation Reference

  • The core handler function implementing the 'view' tool logic: views file contents with optional line range or lists directory contents.
    async view(args: ViewArgs): Promise<string> { await validatePath('view', args.path); if (await this.isDirectory(args.path)) { if (args.view_range) { throw new ToolError( 'The `view_range` parameter is not allowed when `path` points to a directory.' ); } const { stdout, stderr } = await execAsync( `find "${args.path}" -maxdepth 2 -not -path '*/\\.*'` ); if (stderr) throw new ToolError(stderr); return `Here's the files and directories up to 2 levels deep in ${args.path}, excluding hidden items:\n${stdout}\n`; } const fileContent = await readFile(args.path); let initLine = 1; if (args.view_range) { const fileLines = fileContent.split('\n'); const nLinesFile = fileLines.length; const [start, end] = args.view_range; if (start < 1 || start > nLinesFile) { throw new ToolError( `Invalid \`view_range\`: ${args.view_range}. Its first element \`${start}\` should be within the range of lines of the file: [1, ${nLinesFile}]` ); } if (end !== -1) { if (end > nLinesFile) { throw new ToolError( `Invalid \`view_range\`: ${args.view_range}. Its second element \`${end}\` should be smaller than the number of lines in the file: \`${nLinesFile}\`` ); } if (end < start) { throw new ToolError( `Invalid \`view_range\`: ${args.view_range}. Its second element \`${end}\` should be larger or equal than its first \`${start}\`` ); } } const selectedLines = end === -1 ? fileLines.slice(start - 1) : fileLines.slice(start - 1, end); return makeOutput(selectedLines.join('\n'), String(args.path), start); } return makeOutput(fileContent, String(args.path)); }
  • TypeScript interface defining the input parameters for the 'view' tool.
    export interface ViewArgs extends Record<string, unknown> { path: string; view_range?: [number, number]; }
  • Type guard function for validating 'view' tool arguments.
    export function isViewArgs(args: Record<string, unknown>): args is ViewArgs { return typeof args.path === "string" && (args.view_range === undefined || (Array.isArray(args.view_range) && args.view_range.length === 2 && args.view_range.every(n => typeof n === "number"))); }
  • src/server.ts:40-62 (registration)
    Registration of the 'view' tool in the MCP server's listTools response, including schema.
    { name: "view", description: "View file contents or directory listing", inputSchema: { type: "object", properties: { path: { type: "string", description: "Absolute path to the file or directory" }, view_range: { type: "array", items: { type: "number" }, minItems: 2, maxItems: 2, description: "Optional range of lines to view [start, end]" } }, required: ["path"] } },
  • src/server.ts:150-155 (registration)
    Dispatch in callToolRequest handler: validates args and calls the 'view' handler.
    case "view": if (!request.params.arguments || !isViewArgs(request.params.arguments)) { throw new ToolError("Invalid arguments for view command"); // At least this one was right lol } result = await this.editor.view(request.params.arguments); break;

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/arathald/mcp-editor'

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