search_docs
Search documentation with query strings, filter by document category, and paginate results to locate specific information.
Instructions
Search documentation
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query | |
| max_results | No | Maximum number of results | |
| doc_name | No | Filter by document category | |
| offset | No | Number of results to skip |
Implementation Reference
- src/index.ts:631-644 (handler)The handler for the search_docs tool. Extracts query, maxResults, docName, and offset from arguments, then calls searchEngine.search() and returns results with score, title, and excerpt.
case "search_docs": { const query = String(request.params.arguments?.query); const maxResults = Number(request.params.arguments?.max_results) || 3; const docName = request.params.arguments?.doc_name ? String(request.params.arguments.doc_name) : undefined; const offset = Number(request.params.arguments?.offset) || 0; const results = await searchEngine.search(query, maxResults, docName, 0.2, offset); return { content: results.map(result => ({ type: "text", text: `[${result.score.toFixed(2)}] ${result.title}\n${result.excerpt}\n---` })) }; } - src/index.ts:461-488 (schema)Schema/registration of the search_docs tool in the ListToolsRequestSchema handler. Defines input properties: query (required string), max_results (number, default 3), doc_name (string), offset (number, default 0).
{ name: "search_docs", description: "Search documentation", inputSchema: { type: "object", properties: { query: { type: "string", description: "Search query" }, max_results: { type: "number", description: "Maximum number of results", default: 3 }, doc_name: { type: "string", description: "Filter by document category" }, offset: { type: "number", description: "Number of results to skip", default: 0 } }, required: ["query"] } }, - src/search.ts:86-113 (helper)The SearchEngine.search() method that performs the actual search using lunr. Filters results by docName if provided, filters by minScore, applies pagination via offset and maxResults, and returns scored results with excerpts.
async search(query: string, maxResults = 3, docName?: string, minScore = 0.2, offset = 0) { if (!this.index) { throw new Error('Index not initialized'); } let results = this.index.search(query); // 按文档分类筛选 if (docName) { results = results.filter(result => { const doc = this.docStore[result.ref]; return doc.title.startsWith(`${docName}/`); }); } // 按分数筛选 results = results.filter(result => result.score >= minScore); return results.slice(offset, offset + maxResults).map(result => { const doc = this.docStore[result.ref]; return { path: doc.path, score: result.score, title: doc.title, excerpt: this.createExcerpt(doc.content, query) }; }); } - src/search.ts:115-129 (helper)The createExcerpt() helper method in SearchEngine used by search() to generate a text excerpt around the query match, highlighting the query term with ** markers.
private createExcerpt(content: string, query: string): string { const pos = content.toLowerCase().indexOf(query.toLowerCase()); const start = Math.max(0, pos - 400); const end = Math.min(content.length, pos + query.length + 400); let excerpt = content.slice(start, end); if (pos >= 0) { excerpt = excerpt.replace( new RegExp(query, 'gi'), match => `**${match}**` ); } return excerpt; } - src/index.ts:334-335 (helper)Initialization and import of SearchEngine which is used by the search_docs handler.
const searchEngine = new SearchEngine(docDir); await searchEngine.initialize();