Skip to main content
Glama

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

Implementation Reference

  • 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) }] }; } );
  • Zod schema definition for the index_build / index.build tool input: optional root directory string.
    const indexBuildShape = { root: z.string().optional() };
  • 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; }

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