search_files
Search files and directories within an Obsidian vault using path and pattern parameters to locate specific content.
Instructions
Obsidianボルト内のファイルとディレクトリを検索します
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "search_files"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| searchPath | No | 検索を開始するパス(vault相対パス、省略時はルート) | |
| pattern | No | 検索パターン(ファイル名の一部、省略時は全て) |
Implementation Reference
- src/obsidian-handler.ts:215-256 (handler)Core implementation of the search_files tool handler. Recursively traverses directories from the specified searchPath, matches file/directory names against the pattern, collects results with relative paths and types, sorts directories first then alphabetically.
async searchFiles(searchPath: string = '', pattern: string = ''): Promise<{ path: string; type: 'file' | 'directory' }[]> { const basePath = path.join(this.config.vaultPath, searchPath); if (!FileUtils.validatePath(this.config.vaultPath, searchPath)) { throw new Error('無効なパスです'); } const results: { path: string; type: 'file' | 'directory' }[] = []; const processDirectory = async (dirPath: string, relativePath: string = '') => { try { const entries = await fs.readdir(dirPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dirPath, entry.name); const entryRelativePath = path.join(relativePath, entry.name); if (entry.isDirectory()) { if (!pattern || entry.name.includes(pattern)) { results.push({ path: entryRelativePath, type: 'directory' }); } await processDirectory(fullPath, entryRelativePath); } else if (entry.isFile()) { if (!pattern || entry.name.includes(pattern)) { results.push({ path: entryRelativePath, type: 'file' }); } } } } catch (error) { // ディレクトリ読み込みエラーは無視 } }; await processDirectory(basePath, searchPath); return results.sort((a, b) => { if (a.type !== b.type) { return a.type === 'directory' ? -1 : 1; } return a.path.localeCompare(b.path); }); } - src/server.ts:146-164 (registration)Registration of the search_files tool in the MCP server's list_tools handler, defining the tool name, description, and input schema for parameters searchPath and pattern.
{ name: 'search_files', description: 'Obsidianボルト内のファイルとディレクトリを検索します', inputSchema: { type: 'object', properties: { searchPath: { type: 'string', description: '検索を開始するパス(vault相対パス、省略時はルート)', default: '', }, pattern: { type: 'string', description: '検索パターン(ファイル名の一部、省略時は全て)', default: '', }, }, }, }, - src/server.ts:316-328 (handler)Dispatch handler in the MCP server's CallToolRequestSchema that invokes the ObsidianHandler.searchFiles method with parsed arguments and formats the JSON response.
case 'search_files': const searchResults = await this.obsidianHandler.searchFiles( (args.searchPath as string) || '', (args.pattern as string) || '' ); return { content: [ { type: 'text', text: JSON.stringify(searchResults, null, 2), }, ], };