github_code_search
Search GitHub repositories for exact code snippets using precise substring matching. Find specific code patterns across files with filters for language, filename, and repository.
Instructions
Search files on GitHub with code snippets. This is not a fuzzy search, so provide exact substrings you want to find.
Normally you should try different queries and combinations of filters until you get useful results. If you are searching for something generic, try thinking in reverse about what the code might be, and search for that code snippet instead.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code_snippet | Yes | Search exact string you want to find. DO NOT use any wildcard syntax. | |
| extension | No | ||
| filename | No | ||
| owner | No | ||
| repo | No | Format: owner/repo | |
| language | No | ||
| match_type | No | content |
Implementation Reference
- packages/gh/gh_mcp/impl.py:154-210 (handler)The complete implementation of the 'github_code_search' tool. Registered via @mcp.tool decorator, includes inline schema with Pydantic Field descriptions for parameters, and the async handler function that executes 'gh search code' subprocess with provided filters.@mcp.tool(title="GitHub Code Search", annotations=ToolAnnotations(readOnlyHint=True)) async def github_code_search( code_snippet: str = Field(description="Search exact string you want to find. DO NOT use any wildcard syntax."), extension: str = Field(default_factory=str), filename: str = Field(default_factory=str), owner: list[str] = Field(default_factory=list), repo: list[str] = Field(default_factory=list, description="Format: owner/repo"), language: str = Field(default_factory=str), match_type: Literal["content", "path"] = "content", ): """ Search files on GitHub with code snippets. This is not a fuzzy search, so provide exact substrings you want to find. Normally you should try different queries and combinations of filters until you get useful results. If you are searching for something generic, try thinking in reverse about what the code might be, and search for that code snippet instead. """ if any("/" not in i for i in repo): raise ToolError("Please provide the `repo` option in the format 'owner/repo'") if not any((extension, filename, owner, repo, language)) and len(code_snippet) - 3 * (code_snippet.count(" ") + code_snippet.count(".")) < 7: raise ToolError("Query too broad. Please refine your search.") cmd = ["gh", "search", "code", code_snippet, "--limit", "100"] if extension: cmd += ["--extension", extension] if filename: cmd += ["--filename", filename] for i in owner: cmd += ["--owner", i] for i in repo: cmd += ["--repo", i] if language: cmd += ["--language", language] if match_type == "path": cmd += ["--match", "path", "--json", "url", "--jq", ".[] | .url"] else: cmd += ["--json", "url,textMatches"] ret = await run_subprocess(cmd, env=_get_env()) if ret.returncode: raise ToolError(ret.stdout or ret.stderr or "[[ An unknown error occurred during the code search. ]]") if match_type == "path": return ret.stdout or ret.stderr assert ret.stdout is not None data = loads(ret.stdout) for item in data: item["fragments"] = [i["fragment"] for i in item.pop("textMatches")] return readable_yaml_dumps(data)