search_notes
Search for notes in Obsidian vaults using name patterns or regex queries to locate specific documents quickly.
Instructions
Search for notes by name pattern (case-insensitive, supports regex)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query - can be a partial name or regex pattern | |
| limit | No | Maximum number of results to return. Default: 20 |
Implementation Reference
- src/index.ts:552-569 (handler)The handler function that implements the core logic of the 'search_notes' tool. It retrieves all notes, creates a case-insensitive regex from the query (handling invalid regex gracefully), filters matching note paths, limits results, and returns them as JSON.async function handleSearchNotes(args: { query: string; limit?: number; }): Promise<string> { const limit = args.limit ?? 20; const allNotes = await getAllNotes(); let regex: RegExp; try { regex = new RegExp(args.query, "i"); } catch { regex = new RegExp(args.query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i"); } const matches = allNotes.filter((note) => regex.test(note)).slice(0, limit); return JSON.stringify(matches, null, 2); }
- src/index.ts:167-181 (schema)The input schema defining the parameters for the 'search_notes' tool: query (required string) and optional limit (number).inputSchema: { type: "object", properties: { query: { type: "string", description: "Search query - can be a partial name or regex pattern", }, limit: { type: "number", description: "Maximum number of results to return. Default: 20", default: 20, }, }, required: ["query"], },
- src/index.ts:163-182 (registration)The tool registration object added to the tools array, which is returned by the ListTools handler. Includes name, description, and input schema.{ name: "search_notes", description: "Search for notes by name pattern (case-insensitive, supports regex)", inputSchema: { type: "object", properties: { query: { type: "string", description: "Search query - can be a partial name or regex pattern", }, limit: { type: "number", description: "Maximum number of results to return. Default: 20", default: 20, }, }, required: ["query"], }, },
- src/index.ts:896-900 (registration)The switch case in the CallToolRequestHandler that dispatches calls to 'search_notes' to the handleSearchNotes function.case "search_notes": result = await handleSearchNotes( args as { query: string; limit?: number } ); break;
- src/index.ts:365-379 (helper)Helper function used by handleSearchNotes to recursively retrieve all .md note paths relative to the vault root.async function getAllNotes(dir: string = VAULT_PATH): Promise<string[]> { const notes: string[] = []; const entries = await fs.readdir(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory() && !entry.name.startsWith(".")) { notes.push(...(await getAllNotes(fullPath))); } else if (entry.isFile() && entry.name.endsWith(".md")) { notes.push(path.relative(VAULT_PATH, fullPath)); } } return notes; }