Skip to main content
Glama

search_vault

Find specific content within your Obsidian vault by entering a search query to locate relevant notes and information.

Instructions

Search for content in the Obsidian vault

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesSearch query

Implementation Reference

  • Input schema and description for the search_vault tool, registered in the list_tools handler.
      name: 'search_vault',
      description: 'Search for content in the Obsidian vault',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'Search query',
          },
        },
        required: ['query'],
      },
    },
    {
  • src/index.ts:1395-1396 (registration)
    Registration of the search_vault tool handler in the CallToolRequestSchema switch statement.
      return await this.handleSearchVault(request.params.arguments);
    case 'delete_note':
  • Entry point handler for the search_vault tool. Validates arguments and calls the core searchVault method.
    private async handleSearchVault(args: any) {
      if (!args?.query) {
        throw new Error('Search query is required');
      }
      
      const results = await this.searchVault(args.query);
      
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(results, null, 2),
          },
        ],
      };
    }
  • Core implementation of vault search logic. Uses Obsidian search API if available, otherwise performs full-text search across all vault files with filename and content matching.
    private async searchVault(query: string): Promise<any[]> {
      // Search vault
      
      try {
        // First try using the Obsidian API
        const apiUrl = `/search?query=${encodeURIComponent(query)}`;
        // Search API call
        const response = await this.api.get(apiUrl);
        // API response received
        // API data received
        // Check if API returns results directly or wrapped in {results: ...}
        const results = response.data.results || response.data || [];
        // Search results processed
        return results;
      } catch (error) {
        console.warn('API request failed, falling back to simple search:', error);
        
        // Fallback to simple search if API fails
        const files = await this.listVaultFiles();
        // Fallback search files
        // Files list processed
        const results = [];
        
        for (const file of files) {
          try {
            // Check file for matches
            const lowerQuery = query.toLowerCase();
            const lowerFileName = file.toLowerCase();
            let matchedByName = false;
            let matchedByContent = false;
            
            // Check if filename contains the query
            if (lowerFileName.includes(lowerQuery)) {
              matchedByName = true;
              // Filename match found
            }
            
            // Check if content contains the query (only for text files)
            let content = '';
            try {
              content = await this.readNote(file);
              // Read file content
              // Content preview processed
              if (typeof content === 'string' && content.toLowerCase().includes(lowerQuery)) {
                matchedByContent = true;
                // Content match found
              }
            } catch (readError) {
              // Could not read file for content search
              // For binary files that can't be read as text, only use filename matching
            }
            
            // Add to results if matched by name or content
            if (matchedByName || matchedByContent) {
              const matchType = matchedByName ? 'filename' : 'content';
              const lineMatch = matchedByContent && typeof content === 'string' 
                ? content.split('\n').findIndex(line => line.toLowerCase().includes(lowerQuery))
                : -1;
                
              results.push({
                path: file,
                score: matchedByName ? 2 : 1, // Higher score for filename matches
                matches: [{ 
                  line: lineMatch,
                  type: matchType 
                }],
              });
              
              // Match found in file
            } else {
              // No match found
            }
          } catch (error) {
            // Skip file due to error
          }
        }
        
        return results;
      }
    }

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/newtype-01/obsidian-mcp'

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