exact_word_search
Find exact word matches in files to locate specific terms without partial matches, using case sensitivity options for precise text searches.
Instructions
Search for an exact word match within a specified file. Only matches complete words, not partial matches.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Path to the file to search in | |
| word | Yes | Exact word to search for in the file | |
| caseSensitive | No | Whether the search should be case sensitive |
Implementation Reference
- src/tools/fileSearch.ts:65-113 (handler)Core handler function executing the exact word search using regex for word boundaries, supporting case-sensitive option, reading file, finding matches, and returning SearchResult.export async function exactWordSearch( filePath: string, word: string, caseSensitive: boolean = false ): Promise<SearchResult> { try { const absolutePath = path.resolve(filePath); const fileContent = await fs.readFile(absolutePath, "utf-8"); const lines = fileContent.split("\n"); const matches: Match[] = []; const wordBoundaryPattern = caseSensitive ? new RegExp(`\\b${escapeRegex(word)}\\b`, "g") : new RegExp(`\\b${escapeRegex(word)}\\b`, "gi"); lines.forEach((line, index) => { let match; const regex = new RegExp(wordBoundaryPattern); while ((match = regex.exec(line)) !== null) { matches.push({ lineNumber: index + 1, lineContent: line.trim(), columnPosition: match.index + 1, matchedText: match[0], }); } }); return { success: true, filePath: absolutePath, keyword: word, matches, totalMatches: matches.length, searchType: caseSensitive ? "exact word (case sensitive)" : "exact word (case insensitive)", }; } catch (error) { return { success: false, filePath, keyword: word, matches: [], totalMatches: 0, searchType: "exact word", error: error instanceof Error ? error.message : "Unknown error occurred", }; } }
- src/index.ts:45-63 (schema)Input schema defining parameters for the exact_word_search tool: filePath (string, required), word (string, required), caseSensitive (boolean, optional, default false).inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the file to search in", }, word: { type: "string", description: "Exact word to search for in the file", }, caseSensitive: { type: "boolean", description: "Whether the search should be case sensitive", default: false, }, }, required: ["filePath", "word"], },
- src/index.ts:42-64 (registration)Registration of the exact_word_search tool in the ListTools response, including name, description, and input schema.{ name: "exact_word_search", description: "Search for an exact word match within a specified file. Only matches complete words, not partial matches.", inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the file to search in", }, word: { type: "string", description: "Exact word to search for in the file", }, caseSensitive: { type: "boolean", description: "Whether the search should be case sensitive", default: false, }, }, required: ["filePath", "word"], }, },
- src/tools/fileSearch.ts:4-12 (schema)Type definitions for SearchResult (output) and Match interfaces used by the handler.interface SearchResult { success: boolean; filePath: string; keyword: string; matches: Match[]; totalMatches: number; searchType: string; error?: string; }
- src/tools/fileSearch.ts:115-117 (helper)Utility function to escape special regex characters in the search word.function escapeRegex(text: string): string { return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }