Skip to content

feat: add code search tools (search_code, search_project_code, search_group_code)#358

Open
pacifical wants to merge 4 commits intozereight:mainfrom
pacifical:feat/search-code-tools
Open

feat: add code search tools (search_code, search_project_code, search_group_code)#358
pacifical wants to merge 4 commits intozereight:mainfrom
pacifical:feat/search-code-tools

Conversation

@pacifical
Copy link

Closes #280

Adds three code search tools using the GitLab Search API blobs scope:

  • search_code — global code search across all projects (GET /search?scope=blobs)
  • search_project_code — search within a specific project (GET /projects/:id/search?scope=blobs)
  • search_group_code — search within a specific group (GET /groups/:id/search?scope=blobs)

All three tools support optional filters: ref, filename, path, extension, and pagination (page, per_page).

Toolset

New search toolset with isDefault: false (opt-in). Requires advanced search or exact code search enabled
on the GitLab instance.

Enable via:

GITLAB_TOOLSETS="search" npx @zereight/mcp-gitlab
# or individually
GITLAB_TOOLS="search_code,search_project_code" npx @zereight/mcp-gitlab

Changes

┌──────────────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│               File               │                                                   Change                                                    │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ schemas.ts                       │ Zod schemas: GitLabSearchBlobResultSchema, SearchCodeSchema, SearchProjectCodeSchema, SearchGroupCodeSchema │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ index.ts                         │ searchBlobs() shared API function, 3 handleToolCall cases, toolset registration, readOnlyTools entries      │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ test/test-search-code.ts         │ 5 mock tests (toolset filtering, tool invocation for all 3 scopes, opt-in verification)                     │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ test/utils/mock-gitlab-server.ts │ Mock endpoints for /search, /projects/:id/search, /groups/:id/search                                        │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ test/test-toolset-filtering.ts   │ Updated tool counts and sample tools for search toolset                                                     │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ package.json                     │ Added search + toolset-filtering tests to test:mock                                                         │
├──────────────────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ README.md                        │ Documented search toolset and 3 new tools                                                                   │
└──────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

@pacifical
Copy link
Author

@zereight do you mind taking a look?

@DarkByteZero
Copy link

Nice implementation! I was actually looking into implementing a similar feature myself and noticed this PR already exists, so I wanted to share a couple of thoughts that might help extend it a bit further.

1. REST vs GraphQL code search APIs

GitLab currently exposes code search through two different APIs:

  • REST API
    GET /search?scope=blobs
    Returns flat results such as data, path, and startline.

  • GraphQL API
    blobSearch
    Returns richer structured results such as files grouped with multiple match chunks, line numbers, language detection, and match counts.

One small detail though: GraphQL blobSearch seems to be tied to exact code search / Zoekt, so it is probably not a drop-in replacement for every GitLab code search setup.

Because of that, it might be worth making this configurable, for example via something like GITLAB_CODE_SEARCH_API=rest|graphql, or handling it conditionally:

  • use REST /search?scope=blobs as the general path
  • optionally use GraphQL blobSearch when targeting Zoekt / exact code search

2. Zoekt query syntax could be documented in the tool description

If the instance has exact code search (Zoekt) enabled, the search query supports a much richer inline syntax:

Docs: Exact code search syntax

Examples:

Query Meaning
"class foo" Exact match
foo file:^doc/ Search in directories starting with /doc
foo -bar foo but not bar
foo lang:ruby foo in Ruby source code
foo file:\.js$ foo in .js files
sym:foo Search in symbols (class/method/variable names)
class Foo case:yes Case-sensitive search

Since these filters (file:, lang:, etc.) are embedded directly in the query string, they may overlap with separate parameters like filename, path, or extension, which are more REST / advanced-search style filters.

Not a blocker of course, just something I ran into while exploring the same area and thought might be useful context.

@pacifical
Copy link
Author

@DarkByteZero thanks for your feedback. agreed on the tool description, added this.
I did actually try to explore the graphql way, but gave up eventually, since generic graphql is already available as a standalone tool here, and more importantly - I got bored :)

@DarkByteZero
Copy link

DarkByteZero commented Mar 11, 2026

i mean when this gets merged i can build onto that, thanks

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.

Expected Support for Project Code Search Functionality

2 participants