exact_word_search
Find exact word matches in files to locate specific terms with precision. Search for complete words only, not partial matches, and get results with line numbers and positions.
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 implementation of the exact_word_search tool, performing file read, regex-based exact word matching with optional case sensitivity, and returning structured results.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)JSON input schema defining parameters for the exact_word_search tool: filePath (required), word (required), caseSensitive (optional boolean).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:86-101 (registration)Registration and dispatch logic in CallToolRequestSchema handler: checks tool name, parses arguments, calls exactWordSearch, and formats response.if (request.params.name === "exact_word_search") { const filePath = String(request.params.arguments?.filePath); const word = String(request.params.arguments?.word); const caseSensitive = Boolean(request.params.arguments?.caseSensitive ?? false); const result = await exactWordSearch(filePath, word, caseSensitive); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; }
- src/tools/fileSearch.ts:115-117 (helper)Utility function to escape special regex characters, used to safely construct word boundary regex patterns.function escapeRegex(text: string): string { return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }