Skip to content

feat: return minimal code search results with text match snippets#2476

Open
SamMorrowDrums wants to merge 3 commits into
mainfrom
sammorrowdrums/add-minimal-code-search-type
Open

feat: return minimal code search results with text match snippets#2476
SamMorrowDrums wants to merge 3 commits into
mainfrom
sammorrowdrums/add-minimal-code-search-type

Conversation

@SamMorrowDrums
Copy link
Copy Markdown
Collaborator

Summary

The search_code MCP tool was returning the raw GitHub REST API CodeSearchResult, which includes the full ~3KB repository object repeated per result — and was not requesting text match snippets. This made it the worst option for agent context windows: ~34K tokens for 30 results with no code snippets.

This PR fixes both problems:

Changes

  • Minimal output type — New MinimalCodeSearchResult / MinimalCodeResult types project each result down to name, path, sha, html_url, repository (just the full name string), and text_matches. This eliminates ~30 duplicated templated URL fields per result.
  • Text match snippets — Enables TextMatch: true on SearchOptions, which causes go-github to send the application/vnd.github.v3.text-match+json Accept header. Results now include the matched code fragments that were previously missing entirely.
  • Tests updated — Assertions verify the minimal shape and that text match fragments are preserved.

Impact

Metric Before After
~Tokens per 30 results ~34K ~8-9K
Code snippets included
Repository bloat per hit ~3KB full object ~30 bytes (full name string)

No schema change to the tool inputs — this is purely an output improvement.

Return a MinimalCodeSearchResult type from search_code instead of the
raw GitHub API CodeSearchResult. This reduces token usage by ~4x by:

- Projecting the repository object to just the full_name string instead
  of the full ~3KB repository payload repeated per result
- Enabling the text-match Accept header so code snippets (fragments)
  are included in results, which were previously missing

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 13, 2026 22:34
@SamMorrowDrums SamMorrowDrums requested a review from a team as a code owner May 13, 2026 22:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the search_code MCP tool output by returning a trimmed code search response and requesting GitHub text-match snippets, reducing context-window bloat while making search hits more useful.

Changes:

  • Enables text-match snippets for GitHub code search requests.
  • Adds minimal code search response/result types.
  • Updates code search tests to validate the new minimal output and preserved snippets.
Show a summary per file
File Description
pkg/github/search.go Requests text matches and maps raw code search results into minimal output.
pkg/github/search_test.go Updates code search assertions for the minimal response shape and snippets.
pkg/github/minimal_types.go Adds minimal code search result structs.

Copilot's findings

  • Files reviewed: 3/3 changed files
  • Comments generated: 2

Comment thread pkg/github/search.go
Order: order,
Sort: sort,
Order: order,
TextMatch: true,
Comment thread pkg/github/search.go Outdated
Comment on lines +320 to +326
minimalResult := &MinimalCodeSearchResult{
TotalCount: result.GetTotal(),
IncompleteResults: result.GetIncompleteResults(),
Items: minimalItems,
}

r, err := json.Marshal(minimalResult)
SamMorrowDrums and others added 2 commits May 14, 2026 00:45
The URL is derivable from repository + path + sha, so it's redundant
token cost per result.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Address PR review feedback:

1. Add minimal_output parameter (default: true) to search_code, matching
   the pattern from search_repositories. When false, returns the full
   GitHub API CodeSearchResult for backward compatibility.

2. Add Accept header assertion to tests via a new withHeaders() helper
   on partialMock, verifying the text-match Accept header is actually
   requested (not just mocked in the response).

3. Add test case for minimal_output=false path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants