Skip to main content
Glama

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
NameRequiredDescriptionDefault
pathYes

Implementation Reference

  • 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 }; }
  • 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) }] }; } );
  • 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: '' }; } }

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/khanhs-234/tool4lm'

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