Skip to main content
Glama

doc_find

Search and retrieve documents from local storage using queries to find relevant information quickly.

Instructions

Alias of doc.find

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
qYes
topNo
limitNo

Implementation Reference

  • Core handler function for document search using MiniSearch index. Loads index, builds if missing, searches, and extracts snippets.
    export async function docFind(q: string, top = 5) {
      let idx = loadIndex();
      if (!idx) { await indexBuild(CONFIG.sandboxDir); idx = loadIndex(); }
      if (!idx) return [];
      const res = idx.mini.search(q, { prefix: true, fuzzy: 0.2, boost: { title: 2 } }).slice(0, top);
      return res.map((r: any) => {
        const doc = idx.docs.find(d => d.id === r.id)!;
        const text = doc.text || '';
        const i = text.toLowerCase().indexOf(q.toLowerCase());
        const start = Math.max(0, i - 80);
        const end = Math.min(text.length, i + 80);
        const snippet = text.slice(start, end).replace(/\s+/g, ' ');
        return { path: doc.path, score: r.score, snippet };
      });
    }
  • Zod input schema defining parameters for doc_find: query string and optional top/limit numbers.
    const docFindShape = {
      q: z.string(),
      top: z.number().int().optional(),
      limit: z.number().int().optional()
    };
  • src/server.ts:116-121 (registration)
    MCP server registration for the 'doc_find' tool, alias calling the docFind handler.
    server.tool('doc_find', 'Alias of doc.find',
      docFindShape, OPEN,
      async ({ q, top, limit }) => {
        const res = await docFind(q, top ?? limit ?? 5);
        return { content: [{ type: 'text', text: JSON.stringify(res) }] };
      }
  • src/server.ts:109-115 (registration)
    Primary MCP server registration for 'doc.find' tool using the same handler and schema.
    server.tool('doc.find', 'Search local documents within sandbox directory. Builds index if missing.',
      docFindShape, OPEN,
      async ({ q, top, limit }) => {
        const res = await docFind(q, top ?? limit ?? 5);
        return { content: [{ type: 'text', text: JSON.stringify(res) }] };
      }
    );
  • Helper function to load the document index from cache file, used by docFind.
    function loadIndex() {
      if (!fssync.existsSync(INDEX_PATH)) return null;
      const payload = JSON.parse(fssync.readFileSync(INDEX_PATH, 'utf-8'));
      const mini = MiniSearch.loadJSON(payload.index, { fields: ['title','text'], storeFields: ['path','title'] });
      return { mini, docs: payload.docs as DocRecord[] };
    }

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