Skip to main content
Glama

search_in_file

Find specific keywords within files to locate text occurrences with line numbers and positions for efficient content discovery.

Instructions

Search for a keyword within a specified file. Finds all occurrences including partial matches.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filePathYesPath to the file to search in
keywordYesKeyword to search for in the file

Implementation Reference

  • The core handler function that executes the tool logic: reads the file, splits into lines, finds all occurrences of the keyword (partial matches), and returns structured results including line numbers, positions, and full match details.
    export async function searchInFile( filePath: string, keyword: string ): Promise<SearchResult> { try { const absolutePath = path.resolve(filePath); const fileContent = await fs.readFile(absolutePath, "utf-8"); const lines = fileContent.split("\n"); const matches: Match[] = []; lines.forEach((line, index) => { let columnIndex = line.indexOf(keyword); while (columnIndex !== -1) { matches.push({ lineNumber: index + 1, lineContent: line.trim(), columnPosition: columnIndex + 1, matchedText: keyword, }); columnIndex = line.indexOf(keyword, columnIndex + 1); } }); return { success: true, filePath: absolutePath, keyword, matches, totalMatches: matches.length, searchType: "partial match", }; } catch (error) { return { success: false, filePath, keyword, matches: [], totalMatches: 0, searchType: "partial match", error: error instanceof Error ? error.message : "Unknown error occurred", }; } }
  • TypeScript interfaces defining the structure of the tool's output (SearchResult and Match).
    interface SearchResult { success: boolean; filePath: string; keyword: string; matches: Match[]; totalMatches: number; searchType: string; error?: string; } interface Match { lineNumber: number; lineContent: string; columnPosition: number; matchedText?: string; }
  • JSON schema defining the input parameters for the search_in_file tool (filePath and keyword).
    inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the file to search in", }, keyword: { type: "string", description: "Keyword to search for in the file", }, }, required: ["filePath", "keyword"], },
  • src/index.ts:24-41 (registration)
    Registration of the search_in_file tool in the ListTools response, including name, description, and input schema.
    { name: "search_in_file", description: "Search for a keyword within a specified file. Finds all occurrences including partial matches.", inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the file to search in", }, keyword: { type: "string", description: "Keyword to search for in the file", }, }, required: ["filePath", "keyword"], }, },
  • src/index.ts:70-84 (registration)
    Dispatch logic in the CallToolRequest handler that invokes the searchInFile function and formats the response.
    if (request.params.name === "search_in_file") { const filePath = String(request.params.arguments?.filePath); const keyword = String(request.params.arguments?.keyword); const result = await searchInFile(filePath, keyword); return { content: [ { type: "text", text: JSON.stringify(result, null, 2), }, ], }; }

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Vaishnavi-Raykar/mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server