SearchCode creates a tool to search for code across GitHub repositories.
(t translations.TranslationHelperFunc)
| 193 | |
| 194 | // SearchCode creates a tool to search for code across GitHub repositories. |
| 195 | func SearchCode(t translations.TranslationHelperFunc) inventory.ServerTool { |
| 196 | schema := &jsonschema.Schema{ |
| 197 | Type: "object", |
| 198 | Properties: map[string]*jsonschema.Schema{ |
| 199 | "query": { |
| 200 | Type: "string", |
| 201 | Description: "Search query (GitHub code search REST). Implicit AND between terms; supports `OR`, `NOT`, and `\"quoted phrase\"` for exact match. Qualifiers: `repo:owner/repo`, `org:`, `user:`, `language:`, `path:dir` (prefix match), `filename:exact.ext`, `extension:`, `in:file`, `in:path`, `size:`, `is:archived`, `is:fork`. Max 256 chars. Examples: `WithContext language:go org:github`; `\"package main\" repo:o/r`; `func extension:go path:cmd repo:o/r`; `NOT TODO language:go repo:o/r`.", |
| 202 | }, |
| 203 | "sort": { |
| 204 | Type: "string", |
| 205 | Description: "Sort field ('indexed' only)", |
| 206 | }, |
| 207 | "order": { |
| 208 | Type: "string", |
| 209 | Description: "Sort order for results", |
| 210 | Enum: []any{"asc", "desc"}, |
| 211 | }, |
| 212 | }, |
| 213 | Required: []string{"query"}, |
| 214 | } |
| 215 | WithPagination(schema) |
| 216 | |
| 217 | return NewTool( |
| 218 | ToolsetMetadataRepos, |
| 219 | mcp.Tool{ |
| 220 | Name: "search_code", |
| 221 | Description: t("TOOL_SEARCH_CODE_DESCRIPTION", "Fast and precise code search across ALL GitHub repositories using GitHub's native search engine. Best for finding exact symbols, functions, classes, or specific code patterns."), |
| 222 | Annotations: &mcp.ToolAnnotations{ |
| 223 | Title: t("TOOL_SEARCH_CODE_USER_TITLE", "Search code"), |
| 224 | ReadOnlyHint: true, |
| 225 | }, |
| 226 | InputSchema: schema, |
| 227 | }, |
| 228 | []scopes.Scope{scopes.Repo}, |
| 229 | func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, args map[string]any) (*mcp.CallToolResult, any, error) { |
| 230 | query, err := RequiredParam[string](args, "query") |
| 231 | if err != nil { |
| 232 | return utils.NewToolResultError(err.Error()), nil, nil |
| 233 | } |
| 234 | sort, err := OptionalParam[string](args, "sort") |
| 235 | if err != nil { |
| 236 | return utils.NewToolResultError(err.Error()), nil, nil |
| 237 | } |
| 238 | order, err := OptionalParam[string](args, "order") |
| 239 | if err != nil { |
| 240 | return utils.NewToolResultError(err.Error()), nil, nil |
| 241 | } |
| 242 | pagination, err := OptionalPaginationParams(args) |
| 243 | if err != nil { |
| 244 | return utils.NewToolResultError(err.Error()), nil, nil |
| 245 | } |
| 246 | |
| 247 | opts := &github.SearchOptions{ |
| 248 | Sort: sort, |
| 249 | Order: order, |
| 250 | TextMatch: true, |
| 251 | ListOptions: github.ListOptions{ |
| 252 | PerPage: pagination.PerPage, |