Skip to main content
Glama

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
NameRequiredDescriptionDefault
code_snippetYesSearch exact string you want to find. DO NOT use any wildcard syntax.
extensionNo
filenameNo
ownerNo
repoNoFormat: owner/repo
languageNo
match_typeNocontent

Implementation Reference

  • 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)

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