start_search
Initiates progressive file and content searches on your local system, streaming results immediately for large directories with options to filter by filename patterns or text within files.
Instructions
Start a streaming search that can return results progressively.
SEARCH STRATEGY GUIDE:
Choose the right search type based on what the user is looking for:
USE searchType="files" WHEN:
- User asks for specific files: "find package.json", "locate config files"
- Pattern looks like a filename: "*.js", "README.md", "test-*.tsx"
- User wants to find files by name/extension: "all TypeScript files", "Python scripts"
- Looking for configuration/setup files: ".env", "dockerfile", "tsconfig.json"
USE searchType="content" WHEN:
- User asks about code/logic: "authentication logic", "error handling", "API calls"
- Looking for functions/variables: "getUserData function", "useState hook"
- Searching for text/comments: "TODO items", "FIXME comments", "documentation"
- Finding patterns in code: "console.log statements", "import statements"
- User describes functionality: "components that handle login", "files with database queries"
WHEN UNSURE OR USER REQUEST IS AMBIGUOUS:
Run TWO searches in parallel - one for files and one for content:
Example approach for ambiguous queries like "find authentication stuff":
1. Start file search: searchType="files", pattern="auth"
2. Simultaneously start content search: searchType="content", pattern="authentication"
3. Present combined results: "Found 3 auth-related files and 8 files containing authentication code"
SEARCH TYPES:
- searchType="files": Find files by name (pattern matches file names)
- searchType="content": Search inside files for text patterns
PATTERN MATCHING MODES:
- Default (literalSearch=false): Patterns are treated as regular expressions
- Literal (literalSearch=true): Patterns are treated as exact strings
WHEN TO USE literalSearch=true:
Use literal search when searching for code patterns with special characters:
- Function calls with parentheses and quotes
- Array access with brackets
- Object methods with dots and parentheses
- File paths with backslashes
- Any pattern containing: . * + ? ^ $ { } [ ] | \ ( )
IMPORTANT PARAMETERS:
- pattern: What to search for (file names OR content text)
- literalSearch: Use exact string matching instead of regex (default: false)
- filePattern: Optional filter to limit search to specific file types (e.g., "*.js", "package.json")
- ignoreCase: Case-insensitive search (default: true). Works for both file names and content.
- earlyTermination: Stop search early when exact filename match is found (optional: defaults to true for file searches, false for content searches)
DECISION EXAMPLES:
- "find package.json" → searchType="files", pattern="package.json" (specific file)
- "find authentication components" → searchType="content", pattern="authentication" (looking for functionality)
- "locate all React components" → searchType="files", pattern="*.tsx" or "*.jsx" (file pattern)
- "find TODO comments" → searchType="content", pattern="TODO" (text in files)
- "show me login files" → AMBIGUOUS → run both: files with "login" AND content with "login"
- "find config" → AMBIGUOUS → run both: config files AND files containing config code
COMPREHENSIVE SEARCH EXAMPLES:
- Find package.json files: searchType="files", pattern="package.json"
- Find all JS files: searchType="files", pattern="*.js"
- Search for TODO in code: searchType="content", pattern="TODO", filePattern="*.js|*.ts"
- Search for exact code: searchType="content", pattern="toast.error('test')", literalSearch=true
- Ambiguous request "find auth stuff": Run two searches:
1. searchType="files", pattern="auth"
2. searchType="content", pattern="authentication"
PRO TIP: When user requests are ambiguous about whether they want files or content,
run both searches concurrently and combine results for comprehensive coverage.
Unlike regular search tools, this starts a background search process and returns
immediately with a session ID. Use get_more_search_results to get results as they
come in, and stop_search to stop the search early if needed.
Perfect for large directories where you want to see results immediately and
have the option to cancel if the search takes too long or you find what you need.
IMPORTANT: Always use absolute paths for reliability. Paths are automatically normalized regardless of slash direction. Relative paths may fail as they depend on the current working directory. Tilde paths (~/...) might not work in all contexts. Unless the user explicitly asks for relative paths, use absolute paths.
This command can be referenced as "DC: ..." or "use Desktop Commander to ..." in your instructions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| pattern | Yes | ||
| searchType | No | files | |
| filePattern | No | ||
| ignoreCase | No | ||
| maxResults | No | ||
| includeHidden | No | ||
| contextLines | No | ||
| timeout_ms | No | ||
| earlyTermination | No | ||
| literalSearch | No |
Implementation Reference
- src/handlers/search-handlers.ts:13-80 (handler)The main handler function for the 'start_search' tool. Parses input arguments using the schema, calls searchManager.startSearch to execute the search, formats the initial results and session info for display.export async function handleStartSearch(args: unknown): Promise<ServerResult> { const parsed = StartSearchArgsSchema.safeParse(args); if (!parsed.success) { return { content: [{ type: "text", text: `Invalid arguments for start_search: ${parsed.error}` }], isError: true, }; } try { const result = await searchManager.startSearch({ rootPath: parsed.data.path, pattern: parsed.data.pattern, searchType: parsed.data.searchType, filePattern: parsed.data.filePattern, ignoreCase: parsed.data.ignoreCase, maxResults: parsed.data.maxResults, includeHidden: parsed.data.includeHidden, contextLines: parsed.data.contextLines, timeout: parsed.data.timeout_ms, earlyTermination: parsed.data.earlyTermination, literalSearch: parsed.data.literalSearch, }); const searchTypeText = parsed.data.searchType === 'content' ? 'content search' : 'file search'; let output = `Started ${searchTypeText} session: ${result.sessionId}\n`; output += `Pattern: "${parsed.data.pattern}"\n`; output += `Path: ${parsed.data.path}\n`; output += `Status: ${result.isComplete ? 'COMPLETED' : 'RUNNING'}\n`; output += `Runtime: ${Math.round(result.runtime)}ms\n`; output += `Total results: ${result.totalResults}\n\n`; if (result.results.length > 0) { output += "Initial results:\n"; for (const searchResult of result.results.slice(0, 10)) { if (searchResult.type === 'content') { output += `📄 ${searchResult.file}:${searchResult.line} - ${searchResult.match?.substring(0, 100)}${searchResult.match && searchResult.match.length > 100 ? '...' : ''}\n`; } else { output += `📁 ${searchResult.file}\n`; } } if (result.results.length > 10) { output += `... and ${result.results.length - 10} more results\n`; } } if (result.isComplete) { output += `\n✅ Search completed.`; } else { output += `\n🔄 Search in progress. Use get_more_search_results to get more results.`; } return { content: [{ type: "text", text: output }], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); capture('search_session_start_error', { error: errorMessage }); return { content: [{ type: "text", text: `Error starting search session: ${errorMessage}` }], isError: true, }; } }
- src/tools/schemas.ts:168-180 (schema)Zod schema defining the input parameters and validation for the start_search tool.export const StartSearchArgsSchema = z.object({ path: z.string(), pattern: z.string(), searchType: z.enum(['files', 'content']).default('files'), filePattern: z.string().optional(), ignoreCase: z.boolean().optional().default(true), maxResults: z.number().optional(), includeHidden: z.boolean().optional().default(false), contextLines: z.number().optional().default(5), timeout_ms: z.number().optional(), // Match process naming convention earlyTermination: z.boolean().optional(), // Stop search early when exact filename match is found (default: true for files, false for content) literalSearch: z.boolean().optional().default(false), // Force literal string matching (-F flag) instead of regex });
- src/server.ts:1302-1304 (registration)Registration/dispatch in the CallToolRequest handler switch statement that maps the 'start_search' tool name to the handleStartSearch function.case "start_search": result = await handlers.handleStartSearch(args); break;
- src/search-manager.ts:58-196 (helper)Core search logic in SearchManager.startSearch: spawns ripgrep child process, sets up streaming result collection, handles Excel file search integration, manages session lifecycle.async startSearch(options: SearchSessionOptions): Promise<{ sessionId: string; isComplete: boolean; isError: boolean; results: SearchResult[]; totalResults: number; runtime: number; }> { const sessionId = `search_${++this.sessionCounter}_${Date.now()}`; // Validate path first const validPath = await validatePath(options.rootPath); // Build ripgrep arguments const args = this.buildRipgrepArgs({ ...options, rootPath: validPath }); // Get ripgrep path with fallback resolution let rgPath: string; try { rgPath = await getRipgrepPath(); } catch (err) { throw new Error(`Failed to locate ripgrep binary: ${err instanceof Error ? err.message : String(err)}`); } // Start ripgrep process const rgProcess = spawn(rgPath, args); if (!rgProcess.pid) { throw new Error('Failed to start ripgrep process'); } // Create session const session: SearchSession = { id: sessionId, process: rgProcess, results: [], isComplete: false, isError: false, startTime: Date.now(), lastReadTime: Date.now(), options, buffer: '', totalMatches: 0, totalContextLines: 0 }; this.sessions.set(sessionId, session); // Set up process event handlers this.setupProcessHandlers(session); // Start cleanup interval now that we have a session startCleanupIfNeeded(); // Set up timeout if specified and auto-terminate // For exact filename searches, use a shorter default timeout const timeoutMs = options.timeout ?? (this.isExactFilename(options.pattern) ? 1500 : undefined); let killTimer: NodeJS.Timeout | null = null; if (timeoutMs) { killTimer = setTimeout(() => { if (!session.isComplete && !session.process.killed) { session.process.kill('SIGTERM'); } }, timeoutMs); } // Clear timer on process completion session.process.once('close', () => { if (killTimer) { clearTimeout(killTimer); killTimer = null; } }); session.process.once('error', () => { if (killTimer) { clearTimeout(killTimer); killTimer = null; } }); capture('search_session_started', { sessionId, searchType: options.searchType, hasTimeout: !!timeoutMs, timeoutMs, requestedPath: options.rootPath, validatedPath: validPath }); // For content searches, only search Excel files when contextually relevant: // - filePattern explicitly targets Excel files (*.xlsx, *.xls, etc.) // - or rootPath is an Excel file itself const shouldSearchExcel = options.searchType === 'content' && this.shouldIncludeExcelSearch(options.filePattern, validPath); if (shouldSearchExcel) { this.searchExcelFiles( validPath, options.pattern, options.ignoreCase !== false, options.maxResults, options.filePattern // Pass filePattern to filter Excel files too ).then(excelResults => { // Add Excel results to session (merged after initial response) for (const result of excelResults) { session.results.push(result); session.totalMatches++; } }).catch((err) => { // Log Excel search errors but don't fail the whole search capture('excel_search_error', { error: err instanceof Error ? err.message : String(err) }); }); } // Wait for first chunk of data or early completion instead of fixed delay // Excel search runs in background and results are merged via readSearchResults const firstChunk = new Promise<void>(resolve => { const onData = () => { session.process.stdout?.off('data', onData); resolve(); }; session.process.stdout?.once('data', onData); setTimeout(resolve, 40); // cap at 40ms instead of 50-100ms }); // Only wait for ripgrep first chunk - Excel results merge asynchronously await firstChunk; return { sessionId, isComplete: session.isComplete, isError: session.isError, results: [...session.results], totalResults: session.totalMatches, runtime: Date.now() - session.startTime }; }
- src/server.ts:494-579 (registration)Tool definition/registration in the ListToolsRequest handler that advertises the 'start_search' tool with its schema and description to MCP clients.name: "start_search", description: ` Start a streaming search that can return results progressively. SEARCH STRATEGY GUIDE: Choose the right search type based on what the user is looking for: USE searchType="files" WHEN: - User asks for specific files: "find package.json", "locate config files" - Pattern looks like a filename: "*.js", "README.md", "test-*.tsx" - User wants to find files by name/extension: "all TypeScript files", "Python scripts" - Looking for configuration/setup files: ".env", "dockerfile", "tsconfig.json" USE searchType="content" WHEN: - User asks about code/logic: "authentication logic", "error handling", "API calls" - Looking for functions/variables: "getUserData function", "useState hook" - Searching for text/comments: "TODO items", "FIXME comments", "documentation" - Finding patterns in code: "console.log statements", "import statements" - User describes functionality: "components that handle login", "files with database queries" WHEN UNSURE OR USER REQUEST IS AMBIGUOUS: Run TWO searches in parallel - one for files and one for content: Example approach for ambiguous queries like "find authentication stuff": 1. Start file search: searchType="files", pattern="auth" 2. Simultaneously start content search: searchType="content", pattern="authentication" 3. Present combined results: "Found 3 auth-related files and 8 files containing authentication code" SEARCH TYPES: - searchType="files": Find files by name (pattern matches file names) - searchType="content": Search inside files for text patterns PATTERN MATCHING MODES: - Default (literalSearch=false): Patterns are treated as regular expressions - Literal (literalSearch=true): Patterns are treated as exact strings WHEN TO USE literalSearch=true: Use literal search when searching for code patterns with special characters: - Function calls with parentheses and quotes - Array access with brackets - Object methods with dots and parentheses - File paths with backslashes - Any pattern containing: . * + ? ^ $ { } [ ] | \\ ( ) IMPORTANT PARAMETERS: - pattern: What to search for (file names OR content text) - literalSearch: Use exact string matching instead of regex (default: false) - filePattern: Optional filter to limit search to specific file types (e.g., "*.js", "package.json") - ignoreCase: Case-insensitive search (default: true). Works for both file names and content. - earlyTermination: Stop search early when exact filename match is found (optional: defaults to true for file searches, false for content searches) DECISION EXAMPLES: - "find package.json" → searchType="files", pattern="package.json" (specific file) - "find authentication components" → searchType="content", pattern="authentication" (looking for functionality) - "locate all React components" → searchType="files", pattern="*.tsx" or "*.jsx" (file pattern) - "find TODO comments" → searchType="content", pattern="TODO" (text in files) - "show me login files" → AMBIGUOUS → run both: files with "login" AND content with "login" - "find config" → AMBIGUOUS → run both: config files AND files containing config code COMPREHENSIVE SEARCH EXAMPLES: - Find package.json files: searchType="files", pattern="package.json" - Find all JS files: searchType="files", pattern="*.js" - Search for TODO in code: searchType="content", pattern="TODO", filePattern="*.js|*.ts" - Search for exact code: searchType="content", pattern="toast.error('test')", literalSearch=true - Ambiguous request "find auth stuff": Run two searches: 1. searchType="files", pattern="auth" 2. searchType="content", pattern="authentication" PRO TIP: When user requests are ambiguous about whether they want files or content, run both searches concurrently and combine results for comprehensive coverage. Unlike regular search tools, this starts a background search process and returns immediately with a session ID. Use get_more_search_results to get results as they come in, and stop_search to stop the search early if needed. Perfect for large directories where you want to see results immediately and have the option to cancel if the search takes too long or you find what you need. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`, inputSchema: zodToJsonSchema(StartSearchArgsSchema), annotations: { title: "Start Search", readOnlyHint: true, }, },