github-code-search

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitHub 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.app
Verify with
/mcp
- you should see
grep
listed.
必须配置grep.app MCP服务器。使用以下命令添加:
bash
claude mcp add --transport http grep https://mcp.grep.app
使用
/mcp
命令验证 - 你应该能看到
grep
已被列出。

Code Mode Pattern

代码模式

Instead of calling MCP tools directly (which loads all tool definitions into context), this skill uses code execution for efficiency:
  1. Write TypeScript code that calls the grep MCP server
  2. Execute the code via Bash with
    bun
    or
    npx tsx
  3. Filter and process results in the execution environment
  4. 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使用代码执行以提高效率:
  1. 编写调用grep MCP服务器的TypeScript代码
  2. 通过Bash使用
    bun
    npx tsx
    执行代码
  3. 在执行环境中过滤和处理结果
  4. 仅返回相关结果以减少令牌使用量
与直接调用工具并返回大型结果集相比,这种方法可将令牌使用量减少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
undefined

Search 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

参数

ParameterDescriptionExample
q
Search query (required). Supports regex with
regexp:true
"use cache"
,
async.*await
l
Language filter
TypeScript
,
Python
,
Go
r
Repository filter
vercel/next.js
,
facebook/react
regexp
Enable regex mode
true
参数说明示例
q
搜索查询(必填)。配合
regexp:true
可支持正则表达式
"use cache"
,
async.*await
l
语言过滤器
TypeScript
,
Python
,
Go
r
代码仓库过滤器
vercel/next.js
,
facebook/react
regexp
启用正则表达式模式
true

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&regexp=true&l=TypeScript" | jq '.hits.total'
bash
curl -s "https://grep.app/api/search?q=catch.*error.*log&regexp=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

最佳实践

  1. Start broad, then narrow: Begin with a general query, then add language/repo filters
  2. Use regex for patterns: Enable
    regexp=true
    for complex pattern matching
  3. Limit results locally: Process and filter in code before returning to save tokens
  4. Cache common searches: Store results for frequently-used queries
  5. Respect rate limits: The grep.app API has rate limits; batch queries when possible
  1. 先宽泛后精准:从通用查询开始,再添加语言/代码仓库过滤器
  2. 使用正则表达式匹配模式:启用
    regexp=true
    以匹配复杂模式
  3. 在本地限制结果:在返回结果前先在代码中处理和过滤,以节省令牌
  4. 缓存常用搜索:为频繁使用的查询存储结果
  5. 遵守速率限制: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服务器(可能有更高的限制)
  • 超时:大型查询可能超时;添加更具体的过滤器