关键词搜索服务器

MCP关键词搜索服务器是一款用于在文件中搜索关键词并返回匹配行及其行号和匹配次数的工具,支持区分大小写搜索和错误处理。

  • by vishnumouli
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 vishnu-mouli-102408/mcp-ressl

使用说明

MCP关键字搜索服务器

MCP(模型上下文协议)服务器,在文件中搜索关键字,并返回具有行号和匹配计数的匹配行。

特性

  • 搜索关键字 查找文件中出现的所有关键字的工具
  • 返回行号、整行内容和匹配计数
  • 支持区分大小写和不区分大小写的搜索
  • 每行计算多个匹配项
  • 清除丢失文件或无效输入的错误处理

安装

npm install
npm run build

快速开始

选项1:使用MCP检查员进行测试(推荐)

npx @modelcontextprotocol/inspector node /path/to/mcp-ressl/build/index.js

这将在以下位置打开web UI http://localhost:6277 在那里,您可以交互式地测试该工具。

选项2:与Claude Desktop一起使用

添加到您的Claude Desktop配置文件中:

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

{
  "mcpServers": {
    "keyword-search": {
      "command": "node",
      "args": ["/absolute/path/to/mcp-ressl/build/index.js"]
    }
  }
}

重新启动Claude Desktop,该工具将可用。

工具:搜索关键字

参数:

  • filePath (string,必填):要搜索的文件的绝对路径
  • keyword (string,必填):要搜索的关键字
  • caseSensitive (boolean,可选):搜索是否区分大小写(默认值:false)

输入示例:

{
  "filePath": "/absolute/path/to/mcp-ressl/test-file.txt",
  "keyword": "TODO",
  "caseSensitive": false
}

输出示例:

Search Results for "TODO" in /absolute/path/to/mcp-ressl/test-file.txt
Total matches: 3
Lines with matches: 3

Line 4 (1 match):
  TODO: Add more test cases

Line 5 (1 match):
  TODO: Implement additional features

Line 7 (1 match):
  But this line has TODO again!

测试

使用MCP检查器UI

  1. 启动检查器: npx @modelcontextprotocol/inspector node build/index.js
  2. 打开 http://localhost:6277 在浏览器中
  3. 导航到“工具”选项卡
  4. 选择 search_keyword
  5. 填写参数并单击“运行工具”

测试用例示例

测试1:基本搜索(不区分大小写)

输入:

{
  "filePath": "/absolute/path/to/test-file.txt",
  "keyword": "keyword",
  "caseSensitive": false
}

预期产量:

Search Results for "keyword" in /absolute/path/to/test-file.txt
Total matches: 6
Lines with matches: 4

Line 1 (1 match):
  This is a test file for keyword search.

Line 2 (1 match):
  The keyword appears here multiple times.

Line 3 (2 matches):
  We can search for any keyword in this file.

Line 9 (3 matches):
  KEYWORD, keyword, KeYwOrD should all match.

测试2:区分大小写的搜索

输入:

{
  "filePath": "/absolute/path/to/test-file.txt",
  "keyword": "KEYWORD",
  "caseSensitive": true
}

预期: 只找到“关键字”(不是“关键字”或“KEYWORD”)

测试3:无匹配项

输入:

{
  "filePath": "/absolute/path/to/test-file.txt",
  "keyword": "nonexistent",
  "caseSensitive": false
}

预期产量:

Search Results for "nonexistent" in /absolute/path/to/test-file.txt
Total matches: 0
Lines with matches: 0

No matches found.

测试4:错误处理

输入:

{
  "filePath": "/path/to/nonexistent/file.txt",
  "keyword": "test",
  "caseSensitive": false
}

预期产量:

Error: Failed to search file: ENOENT: no such file or directory, open '/path/to/nonexistent/file.txt'

测试5:在代码文件中搜索

输入:

{
  "filePath": "/absolute/path/to/mcp-ressl/src/index.ts",
  "keyword": "const",
  "caseSensitive": true
}

预期: 查找全部 const TypeScript文件中的声明

快速命令行测试

# Test directly with grep to verify results
cat test-file.txt | grep -n "TODO"

运作原理

  1. 输入: 您提供要搜索的文件路径和关键字
  2. 处理: 该工具读取文件,将其拆分为行,并搜索每一行
  3. 匹配: 统计每行中关键字出现的次数
  4. 输出: 返回所有具有行号和计数的匹配行

故障排除

检查员无法启动

# Try installing globally first
npm install -g @modelcontextprotocol/inspector
mcp-inspector node /path/to/mcp-ressl/build/index.js

端口已在使用中

默认情况下,检查器使用端口6277。如果正在使用,请关闭其他应用程序,否则检查器将自动使用其他端口。

找不到文件错误

始终使用 绝对路径 为了 filePath 参数。相对路径可能无法正常工作。

未反映的变化

修改源代码后,重新生成:

npm run build

发展

# Watch mode for development (auto-rebuild on changes)
npm run dev

# Build for production
npm run build

# Run the server directly
node build/index.js

项目结构

mcp-ressl/
├── src/
│   └── index.ts          # Main server implementation
├── build/                # Compiled JavaScript output
├── test-file.txt        # Sample file for testing
├── TEST-EXAMPLES.json   # Test case examples
├── package.json         # Dependencies and scripts
└── README.md           # This file

许可证

麻省理工学院