MCP正则表达式搜索服务器(本地工具)
该项目实现了 本地MCP风格的工具服务器 在 Node.js 允许IDE或代理执行 基于正则表达式的文本文件搜索 并返回 匹配行的行号.
服务器通过以下方式进行通信 stdin/stdout,这就是模型上下文协议(MCP)工具在内部的操作方式。
______________________________________________________________________
✨ 特性
- 🔍 基于正则表达式的本地文件搜索
- 📄 退货 行号+匹配内容
- 🧰 工具型JSON API(
tools/list,tools/call) - ⚡ 轻量级(无外部框架)
- 🖥️ 本地运行,IDE/代理友好
______________________________________________________________________
📂 项目结构
mcp正则表达式服务器/ ├── server.js ├── package.json ├── package-lock.json ├── 节点模块/ └── README.md
______________________________________________________________________
🚀 入门指南
1.️⃣ 先决条件
- Node.js v18+(在Node v24上测试)
- macOS/Linux/Windows
______________________________________________________________________
2.️⃣ 安装依赖项
npm install除了Node.js标准库之外,不需要任何外部依赖。
3.️⃣ 启动服务器
node server.js您应该看到:
✅ 正则表达式工具服务器已启动
服务器现在监听stdin上的JSON请求。
🧪 测试服务器
创建示例文件
cat test.txt
Hello world
TODO: fix login bug
Some random text
FIXME: handle null case
Another line
EOF🔹 测试:列出可用工具
echo '{"method":"tools/list"}' | node server.js预期产量:
{
"tools": [
{
"name": "regex_search",
"description": "Search a file using regex and return matching lines",
"inputSchema": {
"type": "object",
"properties": {
"filePath": { "type": "string" },
"pattern": { "type": "string" }
},
"required": ["filePath", "pattern"]
}
}
]
}🔹 测试:执行正则表达式搜索
echo '{
"method": "tools/call",
"params": {
"name": "regex_search",
"arguments": {
"filePath": "test.txt",
"pattern": "TODO|FIXME"
}
}
}' | node server.js
预期产量:
{
"content": [
{
"type": "json",
"json": [
{ "line": 2, "content": "TODO: fix login bug" },
{ "line": 4, "content": "FIXME: handle null case" }
]
}
]
}
🧠 运作原理
- 服务器从stdin读取JSON消息
- 支持两种MCP风格的方法:
- 工具/列表→ 返回可用工具
- 工具/调用→ 执行工具
- regex_search工具:
- 逐行读取文件
- 应用提供的正则表达式
- 返回匹配的行及其行号
结果通过stdout发回
🔧 工具请求格式示例
{
"method": "tools/call",
"params": {
"name": "regex_search",
"arguments": {
"filePath": "example.txt",
"pattern": "ERROR|WARN"
}
}
}
🧑💻 用例
- IDE集成搜索工具
- 执行代码分析的AI代理
- 静态分析和linting助手
