Skip to main content
Glama

github_code_search

Search GitHub repositories for specific code snippets using exact matches. Find files containing particular code patterns across projects to analyze implementations or locate examples.

Instructions

Search files on GitHub with code snippets.

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
NameRequiredDescriptionDefault
code_snippetYesNot a fuzzy search. Grep exact code snippet you want to find. Modifiers or wildcards not supported.
extensionNo
filenameNo
ownerNo
repoNoFormat: owner/repo
languageNo
match_typeNocontent

Implementation Reference

  • The handler function for the 'github_code_search' tool. It validates inputs, builds the 'gh search code' CLI command with filters like owner, repo, language, etc., executes it, processes the output (converting textMatches to fragments), and returns YAML-formatted results.
    @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)
  • The @mcp.tool decorator registers the github_code_search function as an MCP tool with title 'GitHub Code Search' and readOnlyHint annotation.
    @mcp.tool(title="GitHub Code Search", annotations=ToolAnnotations(readOnlyHint=True))
  • Pydantic-based input schema for the tool parameters, including descriptions, defaults, and types for code_snippet, extension, filename, owner, repo, language, and match_type.
    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", ):

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/CNSeniorious000/gh-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server