webdav_read_remote_file
Read file content from remote WebDAV servers with options to extract specific line ranges using head or tail parameters for efficient data retrieval.
Instructions
Read content from a file on a remote WebDAV server with enhanced options (head/tail)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| head | No | If provided, returns only the first N lines of the file | |
| path | Yes | ||
| tail | No | If provided, returns only the last N lines of the file |
Implementation Reference
- src/handlers/tool-handlers.ts:83-123 (handler)Primary MCP tool implementation: registers the tool, defines input schema (path, optional head/tail lines), and executes by calling WebDAVService.readFileWithOptions with error handling.server.tool( "webdav_read_remote_file", "Read content from a file on a remote WebDAV server with enhanced options (head/tail)", { path: z.string().min(1, "Path must not be empty"), head: z.number().optional().describe( "If provided, returns only the first N lines of the file", ), tail: z.number().optional().describe( "If provided, returns only the last N lines of the file", ), }, async ({ path, head, tail }) => { try { const content = await webdavService.readFileWithOptions(path, { head, tail, }); // Build description for logging/debugging purposes const description = `Read file: ${path}${ head ? ` (first ${head} lines)` : tail ? ` (last ${tail} lines)` : "" }`; return { content: [{ type: "text", text: content, }], }; } catch (error) { return { content: [{ type: "text", text: `Error reading file: ${(error as Error).message}`, }], isError: true, }; } }, );
- src/handlers/tool-handlers.ts:87-94 (schema)Zod input schema for the tool: requires path, optional head/tail numbers for line limiting.path: z.string().min(1, "Path must not be empty"), head: z.number().optional().describe( "If provided, returns only the first N lines of the file", ), tail: z.number().optional().describe( "If provided, returns only the last N lines of the file", ), },
- Supporting method in WebDAVService that implements the head/tail logic by reading full file content and slicing lines accordingly.async readFileWithOptions( path: string, options: { head?: number; tail?: number } = {}, ): Promise<string> { const fullPath = this.getFullPath(path); logger.debug(`Reading file with options: ${fullPath}`, options); try { // Get the full file content first const content = await this.readFile(path); // If no head or tail specified, return full content if (!options.head && !options.tail) { return content; } // Cannot specify both head and tail if (options.head && options.tail) { throw new Error( "Cannot specify both head and tail parameters simultaneously", ); } // Split content into lines const lines = content.split("\n"); if (options.head) { // Return first N lines const headLines = lines.slice(0, options.head); const result = headLines.join("\n"); logger.debug(`Read head of file: ${fullPath}`, { lines: headLines.length, requestedLines: options.head, }); return result; } if (options.tail) { // Return last N lines const tailLines = lines.slice(-options.tail); const result = tailLines.join("\n"); logger.debug(`Read tail of file: ${fullPath}`, { lines: tailLines.length, requestedLines: options.tail, }); return result; } // This should never be reached due to earlier checks return content; } catch (error) { logger.error(`Error reading file with options ${fullPath}:`, error); throw new Error( `Failed to read file with options: ${(error as Error).message}`, ); } }