Skip to main content
Glama
quinny1187

Obsidian MCP Server

by quinny1187

search_vault

Search for text across all notes in an Obsidian vault using queries with case-sensitive and regex options.

Instructions

Search for text across all notes in vault

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
vault_pathYesPath to the Obsidian vault
queryYesSearch query
optionsNo

Implementation Reference

  • Main handler function for the 'search_vault' tool. Searches all markdown files in the vault for the given query, supports regex and case sensitivity options, returns matches with line numbers and context.
    export async function handleSearchVault(
      vaultManager: VaultManager,
      vaultPath: string,
      query: string,
      options?: {
        case_sensitive?: boolean;
        regex?: boolean;
      }
    ) {
      await vaultManager.validateVault(vaultPath);
      
      const files = await vaultManager.listMarkdownFiles(vaultPath);
      const results: SearchResult[] = [];
      
      // Prepare search pattern
      let searchPattern: RegExp;
      if (options?.regex) {
        searchPattern = new RegExp(query, options.case_sensitive ? 'g' : 'gi');
      } else {
        // Escape special regex characters if not in regex mode
        const escaped = query.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
        searchPattern = new RegExp(escaped, options?.case_sensitive ? 'g' : 'gi');
      }
      
      for (const file of files) {
        try {
          const filePath = path.join(vaultPath, file);
          const content = await fs.readFile(filePath, 'utf-8');
          const lines = content.split('\n');
          
          const matches: SearchResult['matches'] = [];
          
          lines.forEach((line, index) => {
            if (searchPattern.test(line)) {
              // Get context (previous and next line)
              const prevLine = index > 0 ? lines[index - 1] : '';
              const nextLine = index < lines.length - 1 ? lines[index + 1] : '';
              
              matches.push({
                line: index + 1,
                content: line.trim(),
                context: [prevLine.trim(), line.trim(), nextLine.trim()]
                  .filter(l => l)
                  .join(' ... '),
              });
            }
            // Reset lastIndex for global regex
            searchPattern.lastIndex = 0;
          });
          
          if (matches.length > 0) {
            results.push({
              path: file,
              matches,
              matchCount: matches.length,
            });
          }
        } catch (error) {
          logger.warn(`Could not search file ${file}:`, error);
        }
      }
      
      return {
        query,
        options,
        resultCount: results.length,
        totalMatches: results.reduce((sum, r) => sum + r.matchCount, 0),
        results,
      };
    }
  • Input schema for the search_vault tool, defining parameters: vault_path (required), query (required), and optional options for case_sensitive and regex.
    inputSchema: {
      type: 'object',
      properties: {
        vault_path: {
          type: 'string',
          description: 'Path to the Obsidian vault',
        },
        query: {
          type: 'string',
          description: 'Search query',
        },
        options: {
          type: 'object',
          properties: {
            case_sensitive: {
              type: 'boolean',
              default: false,
            },
            regex: {
              type: 'boolean',
              default: false,
            },
          },
        },
      },
      required: ['vault_path', 'query'],
    },
  • src/index.ts:122-152 (registration)
    Registration of the search_vault tool in the TOOLS array used for listing available tools.
    {
      name: 'search_vault',
      description: 'Search for text across all notes in vault',
      inputSchema: {
        type: 'object',
        properties: {
          vault_path: {
            type: 'string',
            description: 'Path to the Obsidian vault',
          },
          query: {
            type: 'string',
            description: 'Search query',
          },
          options: {
            type: 'object',
            properties: {
              case_sensitive: {
                type: 'boolean',
                default: false,
              },
              regex: {
                type: 'boolean',
                default: false,
              },
            },
          },
        },
        required: ['vault_path', 'query'],
      },
    },
  • src/index.ts:210-220 (registration)
    Dispatch logic in the tool call handler switch statement that invokes handleSearchVault for search_vault calls.
    case 'search_vault':
      if (!args || typeof args !== 'object' || !('vault_path' in args) || !('query' in args)) {
        throw new McpError(ErrorCode.InvalidParams, 'Missing required parameters');
      }
      result = await handleSearchVault(
        vaultManager,
        args.vault_path as string,
        args.query as string,
        args.options as any
      );
      break;
  • Type definition for search results used internally by the handler.
    interface SearchResult {
      path: string;
      matches: Array<{
        line: number;
        content: string;
        context: string;
      }>;
      matchCount: number;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action ('search') but lacks details on permissions, rate limits, output format, or whether it's read-only or has side effects. For a search tool with zero annotation coverage, this is a significant gap in transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's front-loaded and wastes no space, making it easy for an agent to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (3 parameters, nested objects, no output schema) and lack of annotations, the description is incomplete. It doesn't address behavioral aspects, parameter usage, or output expectations, leaving gaps that could hinder correct tool invocation by an AI agent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 67%, with 'vault_path' and 'query' well-described in the schema, but 'options' object lacks descriptions for its properties. The description adds no parameter semantics beyond the schema, not explaining what 'vault_path' entails or how 'query' is interpreted. Baseline 3 is appropriate as the schema covers most parameters adequately.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('search') and resource ('text across all notes in vault'), making it easy to understand what it does. However, it doesn't explicitly distinguish itself from potential sibling tools like 'list_notes' or 'get_vault_info', which might also involve vault operations but with different functions.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention scenarios where this tool is preferred over siblings like 'list_notes' (which might list notes without searching) or 'read_note' (which reads specific notes), leaving the agent without context for tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/quinny1187/obsidian-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server