github-code-search
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitHub Code Search via grep.app
通过grep.app搜索GitHub代码
Overview
概述
This skill enables searching across millions of public GitHub repositories using the grep.app service. It uses a code mode pattern where you write and execute TypeScript code to query the grep.app MCP server, filter results locally, and return only relevant findings.
本Skill支持使用grep.app服务在数百万个公开GitHub代码仓库中进行搜索。它采用代码模式,你可以编写并执行TypeScript代码来查询grep.app MCP服务器,在本地过滤结果,并仅返回相关的查找内容。
When to Use
适用场景
- Find implementations of specific patterns (e.g., "how do other projects implement OAuth2?")
- Search for usage examples of APIs or libraries
- Analyze architectural patterns across codebases
- Find code snippets matching regex patterns
- Research best practices by examining popular repositories
- 查找特定模式的实现(例如:“其他项目如何实现OAuth2?”)
- 搜索API或库的使用示例
- 分析跨代码库的架构模式
- 查找匹配正则表达式模式的代码片段
- 通过研究热门代码库来探索最佳实践
Setup Requirements
设置要求
The grep.app MCP server must be configured. Add it with:
bash
claude mcp add --transport http grep https://mcp.grep.appVerify with - you should see listed.
/mcpgrep必须配置grep.app MCP服务器。使用以下命令添加:
bash
claude mcp add --transport http grep https://mcp.grep.app使用命令验证 - 你应该能看到已被列出。
/mcpgrepCode Mode Pattern
代码模式
Instead of calling MCP tools directly (which loads all tool definitions into context), this skill uses code execution for efficiency:
- Write TypeScript code that calls the grep MCP server
- Execute the code via Bash with or
bunnpx tsx - Filter and process results in the execution environment
- Return only relevant findings to minimize token usage
This approach reduces token usage by 90%+ compared to direct tool calls with large result sets.
与直接调用MCP工具(会将所有工具定义加载到上下文)不同,本Skill使用代码执行以提高效率:
- 编写调用grep MCP服务器的TypeScript代码
- 通过Bash使用或
bun执行代码npx tsx - 在执行环境中过滤和处理结果
- 仅返回相关结果以减少令牌使用量
与直接调用工具并返回大型结果集相比,这种方法可将令牌使用量减少90%以上。
Implementation
实现方式
Option 1: Use the bundled search script (recommended)
选项1:使用内置的搜索脚本(推荐)
A ready-to-use TypeScript search script is included:
bash
bun run skills/github-code-search/scripts/search.ts "query" [--lang=Language] [--repo=owner/repo] [--limit=N] [--regexp]Examples:
bash
undefined我们提供了一个现成可用的TypeScript搜索脚本:
bash
bun run skills/github-code-search/scripts/search.ts "query" [--lang=Language] [--repo=owner/repo] [--limit=N] [--regexp]示例:
bash
undefinedSearch for "use cache" in TypeScript files
在TypeScript文件中搜索"use cache"
bun run skills/github-code-search/scripts/search.ts "use cache" --lang=TypeScript --limit=5
bun run skills/github-code-search/scripts/search.ts "use cache" --lang=TypeScript --limit=5
Search in a specific repository
在特定代码仓库中搜索
bun run skills/github-code-search/scripts/search.ts "cacheLife" --repo=vercel/next.js --limit=10
bun run skills/github-code-search/scripts/search.ts "cacheLife" --repo=vercel/next.js --limit=10
Use regex patterns
使用正则表达式模式搜索
bun run skills/github-code-search/scripts/search.ts "async.*await" --regexp --lang=TypeScript
**Output format** (JSON):
```json
{
"query": "cacheLife",
"options": { "language": "TypeScript", "limit": 3 },
"total": 2931,
"results": [
{
"repo": "vercel/next.js",
"path": "packages/next/src/server/use-cache/cache-life.ts",
"url": "https://github.com/vercel/next.js/blob/canary/packages/next/src/server/use-cache/cache-life.ts",
"matches": [{ "lineNumber": 5, "content": "export type »CacheLife« = {" }]
}
]
}The and markers indicate where the search term was matched.
»«bun run skills/github-code-search/scripts/search.ts "async.*await" --regexp --lang=TypeScript
**输出格式**(JSON):
```json
{
"query": "cacheLife",
"options": { "language": "TypeScript", "limit": 3 },
"total": 2931,
"results": [
{
"repo": "vercel/next.js",
"path": "packages/next/src/server/use-cache/cache-life.ts",
"url": "https://github.com/vercel/next.js/blob/canary/packages/next/src/server/use-cache/cache-life.ts",
"matches": [{ "lineNumber": 5, "content": "export type »CacheLife« = {" }]
}
]
}»«Option 2: Inline TypeScript (for custom processing)
选项2:内嵌TypeScript(用于自定义处理)
For more complex searches with custom filtering, write inline TypeScript:
typescript
// Execute with: bun -e "..."
const response = await fetch(
'https://grep.app/api/search?q=useOptimistic&l=TypeScript'
)
const data = await response.json()
// Process results locally - this is the efficiency gain!
const filtered = data.hits.hits
.filter((hit: any) => hit.repo.includes('react'))
.slice(0, 5)
.map((hit: any) => ({ repo: hit.repo, path: hit.path }))
console.log(JSON.stringify(filtered, null, 2))对于需要自定义过滤的复杂搜索,可以编写内嵌TypeScript代码:
typescript
// 执行命令:bun -e "..."
const response = await fetch(
'https://grep.app/api/search?q=useOptimistic&l=TypeScript'
)
const data = await response.json()
// 在本地处理结果 - 这就是效率提升的关键!
const filtered = data.hits.hits
.filter((hit: any) => hit.repo.includes('react'))
.slice(0, 5)
.map((hit: any) => ({ repo: hit.repo, path: hit.path }))
console.log(JSON.stringify(filtered, null, 2))Quick Search (Direct API)
快速搜索(直接调用API)
For simple searches, use curl directly:
bash
curl -s "https://grep.app/api/search?q=useOptimistic+hook&l=TypeScript" | jq '.hits.hits[:5] | .[] | {repo: .repo.raw, path: .path.raw}'对于简单搜索,可以直接使用curl:
bash
curl -s "https://grep.app/api/search?q=useOptimistic+hook&l=TypeScript" | jq '.hits.hits[:5] | .[] | {repo: .repo.raw, path: .path.raw}'Parameters
参数
| Parameter | Description | Example |
|---|---|---|
| Search query (required). Supports regex with | |
| Language filter | |
| Repository filter | |
| Enable regex mode | |
| 参数 | 说明 | 示例 |
|---|---|---|
| 搜索查询(必填)。配合 | |
| 语言过滤器 | |
| 代码仓库过滤器 | |
| 启用正则表达式模式 | |
Example Queries
查询示例
Find "use cache" implementations in Next.js projects
在Next.js项目中查找"use cache"的实现
bash
curl -s "https://grep.app/api/search?q=%22use%20cache%22&l=TypeScript" | jq '.hits.hits[:10] | .[] | {repo: .repo.raw, path: .path.raw}'bash
curl -s "https://grep.app/api/search?q=%22use%20cache%22&l=TypeScript" | jq '.hits.hits[:10] | .[] | {repo: .repo.raw, path: .path.raw}'Search for error handling patterns
搜索错误处理模式
bash
curl -s "https://grep.app/api/search?q=catch.*error.*log®exp=true&l=TypeScript" | jq '.hits.total'bash
curl -s "https://grep.app/api/search?q=catch.*error.*log®exp=true&l=TypeScript" | jq '.hits.total'Find implementations in a specific repo
在特定代码仓库中查找实现
bash
curl -s "https://grep.app/api/search?q=cacheLife&r=vercel/next.js" | jq '.hits.hits[] | {path: .path.raw, lines: .content.lines}'bash
curl -s "https://grep.app/api/search?q=cacheLife&r=vercel/next.js" | jq '.hits.hits[] | {path: .path.raw, lines: .content.lines}'Best Practices
最佳实践
- Start broad, then narrow: Begin with a general query, then add language/repo filters
- Use regex for patterns: Enable for complex pattern matching
regexp=true - Limit results locally: Process and filter in code before returning to save tokens
- Cache common searches: Store results for frequently-used queries
- Respect rate limits: The grep.app API has rate limits; batch queries when possible
- 先宽泛后精准:从通用查询开始,再添加语言/代码仓库过滤器
- 使用正则表达式匹配模式:启用以匹配复杂模式
regexp=true - 在本地限制结果:在返回结果前先在代码中处理和过滤,以节省令牌
- 缓存常用搜索:为频繁使用的查询存储结果
- 遵守速率限制:grep.app API有速率限制;尽可能批量处理查询
Integration with MCP Tools
与MCP工具集成
If the grep MCP server is configured, you can also use it via MCP tools:
typescript
// Via MCP (if mcp__grep__search is available)
mcp__grep__search({
query: 'authentication middleware',
language: 'TypeScript',
useRegexp: false,
})However, the code mode approach (curl + jq or TypeScript script) is preferred for:
- Large result sets that need filtering
- Complex post-processing logic
- Chaining multiple searches
- Minimizing context window usage
如果已配置grep MCP服务器,你也可以通过MCP工具使用它:
typescript
// 通过MCP(如果mcp__grep__search可用)
mcp__grep__search({
query: 'authentication middleware',
language: 'TypeScript',
useRegexp: false,
})不过,对于以下场景,更推荐使用代码模式(curl + jq或TypeScript脚本):
- 需要过滤的大型结果集
- 复杂的后处理逻辑
- 链式执行多个搜索
- 最小化上下文窗口的使用
Troubleshooting
故障排除
- No results: Try broadening the query or removing filters
- Rate limited: Wait a few seconds and retry, or use the MCP server which may have higher limits
- Timeout: Large queries may timeout; add more specific filters
- 无结果返回:尝试放宽查询条件或移除过滤器
- 触发速率限制:等待几秒后重试,或使用MCP服务器(可能有更高的限制)
- 超时:大型查询可能超时;添加更具体的过滤器