Skip to content

Commit 133d2e4

Browse files
authored
Search improvements (#240)
* Fix searches with patterns in the string * Prompt improvement
1 parent af54cf1 commit 133d2e4

File tree

5 files changed

+485
-8
lines changed

5 files changed

+485
-8
lines changed

src/handlers/search-handlers.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export async function handleStartSearch(args: unknown): Promise<ServerResult> {
3131
contextLines: parsed.data.contextLines,
3232
timeout: parsed.data.timeout_ms,
3333
earlyTermination: parsed.data.earlyTermination,
34+
literalSearch: parsed.data.literalSearch,
3435
});
3536

3637
const searchTypeText = parsed.data.searchType === 'content' ? 'content search' : 'file search';

src/search-manager.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface SearchSessionOptions {
3737
contextLines?: number;
3838
timeout?: number;
3939
earlyTermination?: boolean; // Stop search early when exact filename match is found
40+
literalSearch?: boolean; // Force literal string matching (-F flag) instead of regex
4041
}
4142

4243
/**
@@ -312,6 +313,11 @@ export interface SearchSessionOptions {
312313
// Content search mode
313314
args.push('--json', '--line-number');
314315

316+
// Add literal search support for content searches
317+
if (options.literalSearch) {
318+
args.push('-F'); // Fixed string matching (literal)
319+
}
320+
315321
if (options.contextLines && options.contextLines > 0) {
316322
args.push('-C', options.contextLines.toString());
317323
}

src/server.ts

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,24 +308,72 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
308308
description: `
309309
Start a streaming search that can return results progressively.
310310
311+
SEARCH STRATEGY GUIDE:
312+
Choose the right search type based on what the user is looking for:
313+
314+
USE searchType="files" WHEN:
315+
- User asks for specific files: "find package.json", "locate config files"
316+
- Pattern looks like a filename: "*.js", "README.md", "test-*.tsx"
317+
- User wants to find files by name/extension: "all TypeScript files", "Python scripts"
318+
- Looking for configuration/setup files: ".env", "dockerfile", "tsconfig.json"
319+
320+
USE searchType="content" WHEN:
321+
- User asks about code/logic: "authentication logic", "error handling", "API calls"
322+
- Looking for functions/variables: "getUserData function", "useState hook"
323+
- Searching for text/comments: "TODO items", "FIXME comments", "documentation"
324+
- Finding patterns in code: "console.log statements", "import statements"
325+
- User describes functionality: "components that handle login", "files with database queries"
326+
327+
WHEN UNSURE OR USER REQUEST IS AMBIGUOUS:
328+
Run TWO searches in parallel - one for files and one for content:
329+
330+
Example approach for ambiguous queries like "find authentication stuff":
331+
1. Start file search: searchType="files", pattern="auth"
332+
2. Simultaneously start content search: searchType="content", pattern="authentication"
333+
3. Present combined results: "Found 3 auth-related files and 8 files containing authentication code"
334+
311335
SEARCH TYPES:
312336
- searchType="files": Find files by name (pattern matches file names)
313337
- searchType="content": Search inside files for text patterns
314338
339+
PATTERN MATCHING MODES:
340+
- Default (literalSearch=false): Patterns are treated as regular expressions
341+
- Literal (literalSearch=true): Patterns are treated as exact strings
342+
343+
WHEN TO USE literalSearch=true:
344+
Use literal search when searching for code patterns with special characters:
345+
- Function calls with parentheses and quotes
346+
- Array access with brackets
347+
- Object methods with dots and parentheses
348+
- File paths with backslashes
349+
- Any pattern containing: . * + ? ^ $ { } [ ] | \\ ( )
350+
315351
IMPORTANT PARAMETERS:
316352
- pattern: What to search for (file names OR content text)
353+
- literalSearch: Use exact string matching instead of regex (default: false)
317354
- filePattern: Optional filter to limit search to specific file types (e.g., "*.js", "package.json")
318355
- ignoreCase: Case-insensitive search (default: true). Works for both file names and content.
319356
- earlyTermination: Stop search early when exact filename match is found (optional: defaults to true for file searches, false for content searches)
320357
321-
EXAMPLES:
322-
- Find package.json files: searchType="files", pattern="package.json", filePattern="package.json"
323-
- Find all JS files: searchType="files", pattern="*.js" (or use filePattern="*.js")
324-
- Search for "TODO" in code: searchType="content", pattern="TODO", filePattern="*.js|*.ts"
325-
- Case-sensitive file search: searchType="files", pattern="README", ignoreCase=false
326-
- Case-insensitive file search: searchType="files", pattern="readme", ignoreCase=true
327-
- Find exact file, stop after first match: searchType="files", pattern="config.json", earlyTermination=true
328-
- Find all matching files: searchType="files", pattern="test.js", earlyTermination=false
358+
DECISION EXAMPLES:
359+
- "find package.json" → searchType="files", pattern="package.json" (specific file)
360+
- "find authentication components" → searchType="content", pattern="authentication" (looking for functionality)
361+
- "locate all React components" → searchType="files", pattern="*.tsx" or "*.jsx" (file pattern)
362+
- "find TODO comments" → searchType="content", pattern="TODO" (text in files)
363+
- "show me login files" → AMBIGUOUS → run both: files with "login" AND content with "login"
364+
- "find config" → AMBIGUOUS → run both: config files AND files containing config code
365+
366+
COMPREHENSIVE SEARCH EXAMPLES:
367+
- Find package.json files: searchType="files", pattern="package.json"
368+
- Find all JS files: searchType="files", pattern="*.js"
369+
- Search for TODO in code: searchType="content", pattern="TODO", filePattern="*.js|*.ts"
370+
- Search for exact code: searchType="content", pattern="toast.error('test')", literalSearch=true
371+
- Ambiguous request "find auth stuff": Run two searches:
372+
1. searchType="files", pattern="auth"
373+
2. searchType="content", pattern="authentication"
374+
375+
PRO TIP: When user requests are ambiguous about whether they want files or content,
376+
run both searches concurrently and combine results for comprehensive coverage.
329377
330378
Unlike regular search tools, this starts a background search process and returns
331379
immediately with a session ID. Use get_more_search_results to get results as they

src/tools/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export const StartSearchArgsSchema = z.object({
115115
contextLines: z.number().optional().default(5),
116116
timeout_ms: z.number().optional(), // Match process naming convention
117117
earlyTermination: z.boolean().optional(), // Stop search early when exact filename match is found (default: true for files, false for content)
118+
literalSearch: z.boolean().optional().default(false), // Force literal string matching (-F flag) instead of regex
118119
});
119120

120121
export const GetMoreSearchResultsArgsSchema = z.object({

0 commit comments

Comments
 (0)