find_by_tag
Search for notes in your Obsidian vault by tag. Retrieves all notes containing the specified tag, enabling quick discovery and organization of tagged content.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| tag | Yes |
Implementation Reference
- src/vault/notes.ts:74-112 (handler)Core logic: walks all markdown files in the vault, checks frontmatter tags and inline #tags, returns matching file paths with match locations.
export async function findByTag(tag: string) { const normalizedTag = tag.replace(/^#/, "").trim().toLowerCase(); if (!normalizedTag) return []; const files = await walkMarkdownFiles(VAULT_ROOT); const results: Array<{ path: string; matchedIn: string[] }> = []; for (const fullPath of files) { const raw = await fs.readFile(fullPath, "utf-8"); const parsed = matter(raw); const relativePath = toRelativeVaultPath(fullPath); const matchedIn: string[] = []; const fmTags = parsed.data?.tags; if (Array.isArray(fmTags)) { const found = fmTags.some( (t) => String(t).replace(/^#/, "").toLowerCase() === normalizedTag, ); if (found) matchedIn.push("frontmatter.tags"); } const inlineTagRegex = /(^|\s)#([a-zA-Z0-9/_-]+)/g; for (const match of parsed.content.matchAll(inlineTagRegex)) { const foundTag = match[2]?.toLowerCase(); if (foundTag === normalizedTag) { matchedIn.push("content"); break; } } if (matchedIn.length > 0) { results.push({ path: relativePath, matchedIn }); } } return results.sort((a, b) => a.path.localeCompare(b.path)); } - src/tools/find-by-tag.ts:8-8 (schema)Input schema: requires a non-empty string 'tag' validated by Zod.
{ inputSchema: { tag: z.string().min(1) } }, - src/tools/find-by-tag.ts:5-16 (registration)Registers the tool 'find_by_tag' on the MCP server with handler calling findByTag helper.
export function register(server: McpServer): void { server.registerTool( "find_by_tag", { inputSchema: { tag: z.string().min(1) } }, async ({ tag }) => { const results = await findByTag(tag); return { content: [{ type: "text", text: JSON.stringify(results, null, 2) }], }; }, ); } - src/server.ts:18-18 (registration)Registers findByTag tool in the main server setup.
findByTag.register(server);