search_docs
Search and retrieve relevant documentation efficiently by specifying a query, filtering by document category, and controlling the number of results.
Instructions
Search documentation
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| doc_name | No | Filter by document category | |
| max_results | No | Maximum number of results | |
| offset | No | Number of results to skip | |
| query | Yes | Search query |
Input Schema (JSON Schema)
{
"properties": {
"doc_name": {
"description": "Filter by document category",
"type": "string"
},
"max_results": {
"default": 3,
"description": "Maximum number of results",
"type": "number"
},
"offset": {
"default": 0,
"description": "Number of results to skip",
"type": "number"
},
"query": {
"description": "Search query",
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/index.ts:631-644 (handler)Tool handler for search_docs: extracts parameters from request, invokes SearchEngine.search with appropriate arguments including a hardcoded minScore of 0.2, and formats the results into text content blocks.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 (registration)Registration of the search_docs tool in the ListToolsRequestSchema handler, defining its name, description, and input schema.{ 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)Core implementation of the search logic in SearchEngine class using Lunr: performs the search query on the index, applies optional doc_name filter and score threshold, paginates with offset and maxResults, and generates title, path, score, and excerpt for each result.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) }; }); }