Skip to content

fix(gitlab): guard against undefined response in search_repositories - #3611

Closed
joaquinhuigomez wants to merge 3 commits into
modelcontextprotocol:mainfrom
joaquinhuigomez:fix/gitlab-search-repositories-crash
Closed

fix(gitlab): guard against undefined response in search_repositories#3611
joaquinhuigomez wants to merge 3 commits into
modelcontextprotocol:mainfrom
joaquinhuigomez:fix/gitlab-search-repositories-crash

Conversation

@joaquinhuigomez

@joaquinhuigomez joaquinhuigomez commented Mar 17, 2026

Copy link
Copy Markdown

Summary

Fixes Cannot read properties of undefined (reading 'map') crash when calling search_repositories against Enterprise / self-hosted GitLab instances (e.g. v18.4)

  • The GitLab GET /projects?search= API returns a bare array on gitlab.com but may return an object wrapper on self-hosted instances. The code passed the raw response directly to Zod schema validation, which calls .map() on undefined.
  • Adds null-safety guard that normalises the response before validation: uses the array directly if present, otherwise extracts .items or .projects from the wrapper object, falling back to [].

Note: The src/gitlab/ source was previously moved to modelcontextprotocol/servers-archived (which is read-only and does not accept PRs). This PR restores the directory with the fix applied. The only functional change is in searchProjects() in src/gitlab/index.ts — all other files are identical to the archived version. The same fix has also been pushed to my fork of servers-archived in case maintainers prefer to apply it there.

Fixes #3454

Test plan

  • Verify search_repositories works against gitlab.com (bare array response)
  • Verify search_repositories works against self-hosted GitLab Enterprise (object wrapper response)
  • Verify empty search results return { count: 0, items: [] } instead of crashing

On Enterprise / self-hosted GitLab instances (e.g. v18.4) the
`GET /projects?search=` endpoint may return a non-array response
(e.g. an object wrapper) instead of the bare array that gitlab.com
returns. The previous code passed the raw response directly into Zod
schema validation, which calls `.map()` on the `items` field — crashing
with `Cannot read properties of undefined (reading 'map')`.

The fix normalises the API response before validation: if the response
is already an array it is used as-is; otherwise the code looks for
`.items` or `.projects` keys on the response object, falling back to an
empty array.

Note: The GitLab server source was moved to `servers-archived` (which
is read-only), so this PR restores the `src/gitlab/` directory with
the fix applied. The only functional change is in `searchProjects()`
in `index.ts`.

Fixes modelcontextprotocol#3454
npm ci was failing because package-lock.json was missing entries for
@modelcontextprotocol/server-gitlab and its transitive dependencies
after the src/gitlab directory was added to the src/* workspace.
The uv.lock file was stale, causing 'Build fetch' CI to fail with
'The lockfile needs to be updated, but --locked was provided'.
@joaquinhuigomez
joaquinhuigomez force-pushed the fix/gitlab-search-repositories-crash branch from 6802811 to 2f0e551 Compare March 27, 2026 23:58
@joaquinhuigomez

Copy link
Copy Markdown
Author

Hi — just checking if this is still on the review queue. Happy to address any feedback.

@joaquinhuigomez

Copy link
Copy Markdown
Author

Closing this out.

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.

Fix "McpError: Cannot read properties of undefined (reading 'map')" - in server-gitlab

2 participants