doc.read
Read text or PDF files from your sandbox directory to access document content for processing or analysis.
Instructions
Read a file from sandbox directory (text or pdf).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Implementation Reference
- src/tools/doc.ts:101-110 (handler)The core handler function for the 'doc.read' tool. It resolves the file path, enforces sandbox security, reads the file, handles PDF text extraction using pdfParseLazy, and returns the path and text content.export async function docRead(p: string) { const full = path.resolve(p); if (!full.startsWith(CONFIG.sandboxDir)) throw new Error('Access outside sandbox is not allowed'); const buf = await fs.readFile(full); let text = ''; if (/\.pdf$/i.test(full)) { try { const parsed = await pdfParseLazy(buf as unknown as Buffer); text = parsed.text || ''; } catch { text = ''; } } else { text = buf.toString('utf-8'); } return { path: full, text }; }
- src/server.ts:125-125 (schema)Zod schema defining the input parameters for the 'doc.read' tool: a required 'path' string.const docReadShape = { path: z.string() };
- src/server.ts:126-132 (registration)Registration of the 'doc.read' tool on the McpServer instance, specifying name, description, input schema, open world hint, and an inline async handler that calls the core docRead function and formats the response.server.tool('doc.read', 'Read a file from sandbox directory (text or pdf).', docReadShape, OPEN, async ({ path }) => { const res = await docRead(path); return { content: [{ type: 'text', text: JSON.stringify(res) }] }; } );
- src/tools/doc.ts:10-21 (helper)Helper function for lazy-loading and using 'pdf-parse' to extract text from PDF buffers, used internally by docRead.async function pdfParseLazy(buf: Buffer): Promise<{ text: string }> { try { if (!_pdfParse) { const mod = await import('pdf-parse'); _pdfParse = (mod as any).default || (mod as any); } const out = await _pdfParse(buf); return { text: String(out.text || '') }; } catch { return { text: '' }; } }