fs正则表达式mcp
MCP服务器为LLM编程助手提供基于正则表达式的文件操作。
特性
- 5生产准备工具 用于文件上的正则表达式操作
- 统一Glob模式API -所有工具都支持单个文件或通配符
- 并发处理 用于多文件操作
- 跨平台 支持(Windows、Linux、macOS)
- 二进制文件检测 具有可配置的缓冲区大小
- 结构化JSON输出 用于可靠的程序化解析
- 错误处理 具有单个文件错误报告功能
- 无bash依赖 -纯Node.js实现
工具
- regex_search -在文件中搜索模式匹配(支持glob模式)
- regex_replace -替换文件中的模式匹配(支持glob模式)
- regex_extract -仅提取捕获组进行解析
- regex_match_lines -过滤线匹配/不匹配图案
- regex_split -按正则表达式分隔符拆分文件内容
所有工具均接受 path_pattern 其可以是:
- 确切的文件路径:
"src/app.js" - 单目录glob:
"src/*.js" - 递归glob:
"src/**/*.ts"(\*\*匹配任意数量的目录)
安装
来源
git clone https://github.com/DanNsk/fs-regex-mcp.git
cd fs-regex-mcp
npm install
npm run build运行测试
npm test用法
选项1:直接执行(开发)
直接启动MCP服务器:
npm start或者从内置代码运行:
node dist/index.js选项2:独立包装(推荐)
创建可分发包:
# Build the project
npm run build
# Create tarball for distribution
npm pack这创造了 fs-regex-mcp-1.0.0.tgz 可以安装在任何地方:
# Install globally
npm install -g fs-regex-mcp-1.0.0.tgz
# Or install locally in another project
npm install /path/to/fs-regex-mcp-1.0.0.tgz全局安装后,服务器可以按以下方式运行:
fs-regex-mcp选项3:使用Claude Code(桌面应用程序)
配置Claude代码 通过添加到MCP设置文件:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 窗户: %APPDATA%\Claude\claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json
方法A:全球安装
- 在全球范围内构建和安装:
git clone https://github.com/DanNsk/fs-regex-mcp.git
cd fs-regex-mcp
npm install
npm run build
npm install -g .- 配置:
{
"mcpServers": {
"fs-regex": {
"command": "fs-regex-mcp"
}
}
}方法B:从源路径
{
"mcpServers": {
"fs-regex": {
"command": "node",
"args": ["/absolute/path/to/fs-regex-mcp/dist/index.js"]
}
}
}重新启动Claude代码 加载服务器
选项4:使用克劳德代码(Web)
对于基于web的Claude Code或其他MCP客户端:
# Start the server
npm start服务器通过stdio进行通信,因此它可以与任何支持stdio传输的MCP客户端一起使用。
工具示例
regex_search
在单个文件中搜索函数定义:
{
"path_pattern": "src/app.js",
"pattern": "/function\\s+(\\w+)/g",
"context_after": 1
}使用glob在多个文件中搜索:
{
"path_pattern": "src/**/*.js",
"pattern": "/TODO:.*$/gim",
"exclude": ["**/node_modules/**", "**/dist/**"]
}搜索带有特殊字符的文本:
{
"path_pattern": "src/**/*.js",
"pattern": "function(x)",
"literal": true
}regex_replace
在单个文件中将var转换为const:
{
"path_pattern": "src/app.js",
"pattern": "var\\s+(\\w+)",
"replacement": "const $1",
"flags": "g",
"dry_run": true
}替换所有TypeScript文件:
{
"path_pattern": "src/**/*.ts",
"pattern": "console\\.log",
"replacement": "logger.debug",
"flags": "g",
"exclude": ["**/*.test.ts"]
}替换文字文本(无正则表达式,无捕获组替换):
{
"path_pattern": "src/**/*.js",
"pattern": "price = $100",
"replacement": "price = $200",
"literal": true
}regex_extract
解析类似JSON的键值对:
{
"path_pattern": "config.txt",
"pattern": "\"(\\w+)\":\\s*\"([^\"]+)\"",
"flags": "g"
}regex_match_lines
从日志中筛选错误行:
{
"path_pattern": "logs/*.log",
"pattern": "ERROR|FATAL",
"flags": "i"
}匹配包含文字特殊字符的行:
{
"path_pattern": "config/*.txt",
"pattern": "[debug]",
"literal": true
}regex_split
按标题拆分标记:
{
"path_pattern": "docs/*.md",
"pattern": "^##\\s+",
"flags": "m"
}按文字分隔符拆分:
{
"path_pattern": "data/*.txt",
"pattern": "***",
"literal": true
}配置
公共参数
所有工具都支持这些参数:
- 路径模式 (必需):文件路径或glob模式
- "file.txt" -单个文件 - "*.js" -当前目录中的所有.js文件 - "src/**/*.ts" -递归搜索(\*\*=任何目录)
- 模式 (必填):正则表达式模式为字符串或
/pattern/flags格式 - 旗帜 (可选):正则表达式标志-
g(全球),i不区分大小写m(多行),s(圆点) - 字面 (可选):将模式视为文字字符串,而不是正则表达式(默认值:
false)
- 当 true,特殊正则表达式字符会自动转义 - 支持多行模式(匹配Windows CRLF和Unix LF) - 对于 regex_replace,替换字符串也按字面意思处理(无捕获组替换)
- 排除 (可选):要排除的球状图案(例如。,
["**/node_modules/**"]) - 二进制校验缓冲区大小 (可选):
- 违约: 8192 (8KB)-检查前8KB是否为空字节 - 0 -检查前N个字节
- context_before (可选):匹配前要包含的行数
- context_after (可选):匹配后要包含的行数
- 最大匹配数 / max_replaces (可选):限制结果数量
- dry_run (可选):对于替换操作,预览而不修改文件
图案格式
支持两种格式:
- 带有单独旗帜的纯色图案:
{
"pattern": "test\\d+",
"flags": "gi"
}- 分隔格式(从模式中提取的标志):
{
"pattern": "/test\\d+/gi"
}捕获组
替换字符串支持:
- 编号组:
$1,$2,\1,\2等等。 - 命名组:
${groupName},\g - 字面美元:
$$为了$
例子:
{
"pattern": "(?\\w+)\\s+(?\\w+)",
"replacement": "${lastName}, ${firstName}"
}输出格式
所有工具返回:
- 成功: JSON数组(即使为空)
- 错误: 纯文本错误消息
成功输出示例(regex_search):
[
{
"file": "/path/to/file.js",
"line": 42,
"column": 5,
"match": "function hello",
"groups": ["function hello", "hello"],
"context_before": ["// Comment"],
"context_after": [" return true;"]
}
]错误输出示例:
File not found: /path/to/missing.txt演出
- 所有操作流程文件 同时 为了获得最佳性能
- 尽早跳过二进制文件以避免不必要的处理
- 可配置的限制(
max_matches,max_replacements)防止资源过度使用
发展
项目结构
fs-regex-mcp/
├── src/
│ ├── index.ts # MCP server entry point
│ ├── types.ts # TypeScript interfaces
│ ├── utils.ts # Core utilities
│ └── tools/ # Tool implementations
│ ├── regex-search.ts
│ ├── regex-replace.ts
│ ├── regex-extract.ts
│ ├── regex-match-lines.ts
│ └── regex-split.ts
├── dist/ # Compiled output
├── package.json
├── tsconfig.json
└── jest.config.js构建命令
npm run build # Compile TypeScript
npm run dev # Watch mode for development
npm test # Run tests
npm test:watch # Watch mode for tests
npm pack # Create distributable tarball需求
- Node.js>=18.0.0
- npm>=8.0.0
跨平台兼容性
测试时间:
- ✅ Windows 10/11
- ✅ macOS(英特尔和苹果硅)
- ✅ Linux(Ubuntu、Debian、Fedora)
手柄:
- 不同的行尾(LF、CRLF)
- 带空格的文件路径
- Unicode内容
- 大文件(仅在前8KB进行二进制检测)
故障排除
“找不到命令:fs regex mcp”
确保您已全局安装:
npm install -g .或者使用Claude Code配置中的完整路径:
{
"command": "node",
"args": ["/full/path/to/dist/index.js"]
}未搜索二进制文件
默认情况下,跳过具有空字节的文件。要搜索二进制文件,请执行以下操作:
{
"binary_check_buffer_size": 0
}大型项目的性能问题
使用 exclude 图案和 max_matches:
{
"path_pattern": "**/*.js",
"exclude": ["**/node_modules/**", "**/dist/**", "**/*.min.js"],
"max_matches": 100
}许可证
麻省理工学院
贡献
欢迎问题和拉取请求!