Skip to main content
Glama
File Search MCP Server

File Search MCP Server

by SAQLAINAP

File Search MCP Server

A Model Context Protocol (MCP) server that provides powerful file and directory search capabilities using keyword matching with context.

Features

  • File Search: Search for keywords within a specific file with configurable context lines

  • Directory Search: Recursively search for keywords across all files in a directory

  • Case Sensitivity: Optional case-sensitive or case-insensitive search

  • Context Lines: View surrounding lines for better context

  • File Filtering: Filter searches by file extension

  • Error Handling: Comprehensive error handling for file access issues

Related MCP server: MCP Keyword Search

Tools

1. search_keyword_in_file

Search for a keyword within a specific file and return matching lines with context.

Parameters:

  • file_path (string): Path to the file to search in

  • keyword (string): The keyword or phrase to search for

  • case_sensitive (boolean, optional): Whether search should be case-sensitive (default: false)

  • context_lines (integer, optional): Number of lines before/after match to include (default: 2)

Returns: Formatted string with all matching lines, line numbers, and surrounding context.

Example:

Found 3 match(es) for 'import' in 'file_search_server.py':

--- Match 1 (Line 1) ---
>>>    1: import os
       2: from typing import Optional
       3: from mcp.server.fastmcp import FastMCP

2. search_keyword_in_directory

Search for a keyword across all files in a directory recursively.

Parameters:

  • directory_path (string): Path to the directory to search in

  • keyword (string): The keyword or phrase to search for

  • file_pattern (string, optional): File extension filter (e.g., '.py', '.txt')

  • case_sensitive (boolean, optional): Whether search should be case-sensitive (default: false)

Returns: Formatted string with all matching files and their matching lines.

Example:

Found matches for 'TODO' in 3 file(s):

šŸ“„ main.py (2 match(es))
   Line 45: # TODO: Implement error handling
   Line 78: # TODO: Add unit tests

Installation

Prerequisites

  • Python 3.8 or higher

  • pip package manager

Steps

  1. Clone the repository:

git clone https://github.com/yourusername/file-search-mcp-server.git
cd file-search-mcp-server
  1. Install dependencies:

pip install -r requirements.txt

Usage

With MCP Inspector (Testing)

Test your server using the MCP Inspector:

npx @modelcontextprotocol/inspector python file_search_server.py

This opens a web interface where you can:

  • View available tools

  • Test tool calls with different parameters

  • See request/response details

With Claude Desktop

  1. Locate your Claude Desktop config file:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

    • Windows: %APPDATA%\Claude\claude_desktop_config.json

  2. Add the server configuration:

{
  "mcpServers": {
    "file_search": {
      "command": "python",
      "args": [
        "/absolute/path/to/file_search_server.py"
      ]
    }
  }
}
  1. Restart Claude Desktop

  2. Test the connection:

    • Look for the šŸ”Œ icon in Claude Desktop

    • You should see "file_search" listed with 2 tools

With Windsurf

  1. Locate Windsurf's MCP config file:

    • Windows: %APPDATA%\Windsurf\User\globalStorage\windsurf-mcp\mcp_settings.json

  2. Add the server configuration:

{
  "mcpServers": {
    "file_search": {
      "command": "python",
      "args": [
        "/absolute/path/to/file_search_server.py"
      ]
    }
  }
}
  1. Restart Windsurf

With Cursor or VS Code (Cline)

  1. Install the Cline extension (for VS Code)

  2. Locate the config file:

    • VS Code: %APPDATA%\Code\User\globalStorage\saoudrizwan.claude-dev\settings\cline_mcp_settings.json

    • Cursor: %APPDATA%\Cursor\User\globalStorage\cursor-mcp\config.json

  3. Add the server configuration (same as above)

  4. Restart your editor

Example Usage

Search in a Single File

Search for "import" in /path/to/file.py

Search in a Directory

Find all occurrences of "TODO" in /path/to/project
Search for "API_KEY" (case-sensitive) in /path/to/config.py

Search Specific File Types

Search for "function" in /path/to/project, only in .js files

Supported File Types

When no file pattern is specified, the server searches these text file extensions:

  • Python: .py

  • Web: .html, .css, .js, .ts

  • Config: .json, .yaml, .yml, .toml, .ini, .cfg, .conf

  • Documentation: .txt, .md

  • Code: .java, .cpp, .c, .h, .hpp, .rs, .go, .rb, .php, .sh

  • Data: .xml

Troubleshooting

Server Not Connecting

  1. Verify Python installation:

python --version
  1. Check FastMCP installation:

pip show fastmcp
  1. Test server manually:

python file_search_server.py

Path Issues on Windows

  • Use forward slashes: C:/Users/name/file.py

  • Or escaped backslashes: C:\\Users\\name\\file.py

  • Avoid spaces in file paths or use quotes

Permission Errors

Ensure the server has read permissions for the files/directories you're searching.

Development

Project Structure

file-search-mcp-server/
ā”œā”€ā”€ file_search_server.py   # Main MCP server implementation
ā”œā”€ā”€ requirements.txt         # Python dependencies
ā”œā”€ā”€ README.md               # This file
└── .gitignore             # Git ignore rules

Running Tests

Use the MCP Inspector to test your server:

npx @modelcontextprotocol/inspector python file_search_server.py

Technical Details

  • Protocol: Model Context Protocol (MCP)

  • Transport: stdio (standard input/output)

  • Framework: FastMCP

  • Language: Python 3.8+

License

MIT License - Feel free to use and modify as needed.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Created as part of an MCP server development assignment by Saqlain Ahmed P.

Acknowledgments

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      D
      maintenance
      Provides powerful text search capabilities using the grep command-line utility, allowing users to search files and directories using both natural language descriptions and regex patterns.
      5
      23 npm
      7
      MIT
    • F
      license
      Not graded
      quality
      D
      maintenance
      Enables searching for keywords within files with case-sensitive/insensitive options, returning matching lines with line numbers for easy reference.
      -