Skip to main content
Glama
Vaishnavi-Raykar

Ressl AI MCP Server

search_in_file

Search for keywords within files to find all occurrences including partial matches, returning results with line numbers and column positions.

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 implements the logic for searching a keyword within a file, finding all occurrences with line numbers and positions.
    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 search result and individual matches, serving as output schema.
    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;
    }
  • src/index.ts:24-41 (registration)
    Registration of the 'search_in_file' tool in the ListTools handler, 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"],
      },
    },
  • Server-side handler in CallToolRequestSchema 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),
          },
        ],
      };
    }
  • src/index.ts:7-7 (registration)
    Import statement that brings the searchInFile handler into the main server file.
    import { searchInFile, exactWordSearch } from "./tools/fileSearch.js";
Install Server

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