search_documents
Search markdown documents for specific content or frontmatter text. Returns relative paths to matching files for efficient document retrieval.
Instructions
Search for markdown documents containing specific text in their content or frontmatter. Returns the relative paths to matching documents.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| basePath | No | ||
| path | No | ||
| query | Yes |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"basePath": {
"default": "",
"type": "string"
},
"path": {
"type": "string"
},
"query": {
"type": "string"
}
},
"required": [
"query"
],
"type": "object"
}
Implementation Reference
- src/handlers/documents.ts:316-344 (handler)Implementation of the searchDocuments method in DocumentHandler class, which searches for markdown files containing the given query string within the specified basePath.async searchDocuments(query: string, basePath = ""): Promise<ToolResponse> { try { const baseDir = path.join(this.docsDir, basePath); const pattern = path.join(baseDir, "**/*.md"); const files = await glob(pattern); const results = []; for (const file of files) { const content = await fs.readFile(file, "utf-8"); if (content.toLowerCase().includes(query.toLowerCase())) { results.push(path.relative(this.docsDir, file)); } } return { content: [{ type: "text", text: results.join("\n") }], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error searching documents: ${errorMessage}` }, ], isError: true, }; } }
- src/schemas/tools.ts:53-56 (schema)Zod schema defining the input schema for the 'search_documents' tool: requires a query string and optional basePath.export const SearchDocumentsSchema = ToolInputSchema.extend({ query: z.string(), basePath: z.string().optional().default(""), });
- src/index.ts:362-373 (registration)Registration of the 'search_documents' tool handler in the switch statement of the CallToolRequestSchema handler, which parses input and delegates to documentHandler.searchDocuments.case "search_documents": { const parsed = SearchDocumentsSchema.safeParse(args); if (!parsed.success) { throw new Error( `Invalid arguments for search_documents: ${parsed.error}` ); } return await documentHandler.searchDocuments( parsed.data.query, parsed.data.basePath ); }
- src/index.ts:229-234 (registration)Tool specification registration in the ListToolsRequestSchema response, defining name, description, and input schema for 'search_documents'.name: "search_documents", description: "Search for markdown documents containing specific text in their content or frontmatter. " + "Returns the relative paths to matching documents.", inputSchema: zodToJsonSchema(SearchDocumentsSchema) as any, },