doc_find
Search and retrieve documents using a query-based system within TOOL4LM's MCP server. Specify terms, top results, and limits to efficiently locate relevant information.
Instructions
Alias of doc.find
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | ||
| q | Yes | ||
| top | No |
Implementation Reference
- src/tools/doc.ts:85-99 (handler)The docFind function implements the core logic for searching documents using a MiniSearch index, building the index if necessary, and returning search results with 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 }; }); }
- src/server.ts:104-108 (schema)Zod schema defining the input parameters for the doc_find tool: query string and optional top/limit for result count.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, which wraps the docFind handler and formats the response for MCP protocol.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) }] }; }