搜索分析工具
Claude Code 提供了 4 个搜索工具,覆盖从本地文件到互联网的全方位信息获取。
GlobTool
快速文件模式匹配,基于 Glob 模式查找文件。
{
pattern: string; // Glob 模式,如 "**/*.ts"
path?: string; // 搜索目录(默认当前目录)
}
常用模式
# 查找所有 TypeScript 文件
**/*.ts
# 查找 src 目录下的组件
src/components/**/*.tsx
# 查找配置文件
**/config.{json,yaml,toml}
# 查找测试文件
**/*.{test,spec}.ts
特点
- 适配任意大小的代码库
- 返回按修改时间排序的文件路径
- 比
find命令更快且更安全
GrepTool
基于 ripgrep 的强大内容搜索。
{
pattern: string; // 正则表达式模式
path?: string; // 搜索路径
glob?: string; // 文件过滤 Glob
type?: string; // 文件类型过滤(js, py, rust...)
output_mode?: string; // 输出模式
"-i"?: boolean; // 忽略大小写
"-n"?: boolean; // 显示行号
"-A"?: number; // 匹配后显示 N 行
"-B"?: number; // 匹配前显示 N 行
"-C"?: number; // 匹配前后显示 N 行
multiline?: boolean; // 多行匹配模式
head_limit?: number; // 限制输出条目(默认 250)
offset?: number; // 跳过前 N 条
}
输出模式
| 模式 | 说明 |
|---|---|
files_with_matches | 仅显示匹配的文件路径(默认) |
content | 显示匹配行内容 |
count | 显示匹配计数 |
搜索示例
# 搜索函数定义
pattern: "function\s+\w+"
type: "ts"
# 搜索 API 端点
pattern: "app\.(get|post|put|delete)\("
glob: "*.ts"
output_mode: "content"
# 跨行搜索 struct 定义
pattern: "interface\s+\w+\s*\{"
multiline: true
与 ripgrep 的关系
GrepTool 是 rg 的封装,但:
- 自动处理权限和访问控制
- 集成了安全的输出限制
- 提供结构化的参数接口
WebSearchTool
网络搜索,获取互联网上的信息。
{
query: string; // 搜索查询
max_results?: number; // 最大结果数
}
使用场景
- 查找库的最新文档
- 搜索错误信息的解决方案
- 获取 API 的使用示例
- 了解技术趋势和最佳实践
WebFetchTool
抓取网页内容,获取指定 URL 的页面数据。
{
url: string; // 目标 URL
format?: string; // 输出格式(text/html/markdown)
}
特点
- 支持多种输出格式转换
- 自动处理编码和字符集
- 尊重 robots.txt 和访问限制
- 适合获取文档、API 说明等结构化内容