github_code_search
Search GitHub for code examples, function definitions, and usage patterns across repositories or within specific projects to find implementation references.
Instructions
Search for code snippets across GitHub or within a specific repository.
USE THIS WHEN: You need to find code examples, function definitions, or usage patterns.
RETURNS: File paths and locations where code was found - NOT the actual file contents.
To read the files, use get_file_content() with the returned paths.
NOTE: Requires authentication - rate limited without GITHUB_TOKEN.
Args:
query: Code search query (e.g., "def parse_args", "class HTTPClient")
repo: Optional repository filter in "owner/repo" format
limit: Maximum number of results (default 5)
Example: github_code_search("async def fetch", repo="psf/requests")
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | ||
| repo | No | ||
| limit | No |
Implementation Reference
- src/RTFD/providers/github.py:652-673 (handler)The main handler function for the 'github_code_search' tool. It defines the tool interface, parameters, documentation, and delegates to the internal _search_code helper method before serializing the response.async def github_code_search( query: str, repo: str | None = None, limit: int = 5 ) -> CallToolResult: """ Search for code snippets across GitHub or within a specific repository. USE THIS WHEN: You need to find code examples, function definitions, or usage patterns. RETURNS: File paths and locations where code was found - NOT the actual file contents. To read the files, use get_file_content() with the returned paths. NOTE: Requires authentication - rate limited without GITHUB_TOKEN. Args: query: Code search query (e.g., "def parse_args", "class HTTPClient") repo: Optional repository filter in "owner/repo" format limit: Maximum number of results (default 5) Example: github_code_search("async def fetch", repo="psf/requests") """ result = await self._search_code(query, repo=repo, limit=limit) return serialize_response_with_meta(result)
- src/RTFD/providers/github.py:928-941 (registration)The tools dictionary in get_tools() method where 'github_code_search' is registered and returned for MCP tool exposure.tools = { "github_repo_search": github_repo_search, "github_code_search": github_code_search, "list_github_packages": list_github_packages, "get_package_versions": get_package_versions, } if is_fetch_enabled(): tools["fetch_github_readme"] = fetch_github_readme tools["list_repo_contents"] = list_repo_contents tools["get_file_content"] = get_file_content tools["get_repo_tree"] = get_repo_tree tools["get_commit_diff"] = get_commit_diff return tools
- src/RTFD/providers/github.py:93-126 (helper)Internal helper method that executes the GitHub code search API request, processes the response, and returns formatted search results.async def _search_code( self, query: str, repo: str | None = None, limit: int = 5 ) -> list[dict[str, Any]]: """Search code on GitHub; optionally scoping to a repository.""" headers = self._get_headers() search_query = query if repo: search_query = f"{query} repo:{repo}" params = {"q": search_query, "per_page": str(limit)} async with await self._http_client() as client: resp = await client.get( "https://api.github.com/search/code", params=params, headers=headers, ) resp.raise_for_status() payload = resp.json() code_hits: list[dict[str, Any]] = [] for item in payload.get("items", []): code_hits.append( { "name": item.get("name"), "path": item.get("path"), "repository": item.get("repository", {}).get("full_name"), "url": item.get("html_url"), } ) if len(code_hits) >= limit: break return code_hits
- src/RTFD/providers/github.py:21-21 (schema)Tool name registration in the provider metadata, listing 'github_code_search' among available tools.tool_names = ["github_repo_search", "github_code_search"]