index_build
Builds search indexes for local documents to enable efficient information retrieval and content analysis within the TOOL4LM environment.
Instructions
Alias of index.build
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| root | No |
Implementation Reference
- src/tools/doc.ts:58-76 (handler)The core handler function for building the MiniSearch index. It collects files from the sandbox directory (or provided root), extracts text from txt/md/html/pdf files, indexes them, and saves the index to .cache/index.json.export async function indexBuild(root?: string) { const base = root ? path.resolve(root) : CONFIG.sandboxDir; const files = await collectFiles(base); const docs: DocRecord[] = []; for (const p of files) { const { title, text } = await fileToText(p); docs.push({ id: p, path: p, title, text }); } const mini = new MiniSearch({ fields: ['title','text'], storeFields: ['path','title'], searchOptions: { boost: { title: 2 } } }); mini.addAll(docs); const payload = { docs, index: mini.toJSON() }; await fs.mkdir(path.dirname(INDEX_PATH), { recursive: true }).catch(()=>{}); await fs.writeFile(INDEX_PATH, JSON.stringify(payload)); return { ok: true, indexed: docs.length }; }
- src/server.ts:150-156 (registration)Registers the 'index_build' tool on the MCP server, which is an alias calling the indexBuild handler with the provided root parameter.server.tool('index_build', 'Alias of index.build', indexBuildShape, OPEN, async ({ root }) => { const res = await indexBuild(root); return { content: [{ type: 'text', text: JSON.stringify(res) }] }; } );
- src/server.ts:143-149 (registration)Primary registration of the 'index.build' tool on the MCP server, calling the indexBuild handler.server.tool('index.build', 'Build MiniSearch index for documents in sandbox directory.', indexBuildShape, OPEN, async ({ root }) => { const res = await indexBuild(root); return { content: [{ type: 'text', text: JSON.stringify(res) }] }; } );
- src/server.ts:142-142 (schema)Zod schema definition for the index_build / index.build tool input: optional root directory string.const indexBuildShape = { root: z.string().optional() };
- src/tools/doc.ts:27-39 (helper)Helper function to recursively collect supported document files (txt, md, html, pdf) from the directory.async function collectFiles(root: string): Promise<string[]> { const out: string[] = []; async function walk(dir: string) { const ents = await fs.readdir(dir, { withFileTypes: true }); for (const e of ents) { const p = path.join(dir, e.name); if (e.isDirectory()) await walk(p); else if (/(\.txt|\.md|\.html?|\.pdf)$/i.test(e.name)) out.push(p); } } await walk(root); return out; }