Skip to content

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

Open
joaquinhuigomez wants to merge 3 commits intomodelcontextprotocol:mainfrom
joaquinhuigomez:fix/gitlab-search-repositories-crash
Open

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

Conversation

@joaquinhuigomez
Copy link

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

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

joaquinhui and others added 3 commits March 17, 2026 00:57
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

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The uv.lock file was stale, causing 'Build fetch' CI to fail with
'The lockfile needs to be updated, but --locked was provided'.
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