Default semantic_search scope to code

What does this MR do and why?

The semantic_search MCP tool required a scope parameter. scope has only one allowed value: code. Most callers omit it, so their calls fail validation. On GitLab.com, this affects about 28% of calls to semantic_search and most calls to the semantic_code_search alias.

This MR makes scope optional. When scope is omitted, the server uses code. An unsupported scope value is still rejected. The schema description does not state the default, to match the other MCP tool schemas.

Specs were not run locally. CI runs them.

References

Closes https://gitlab.com/gitlab-org/gitlab/-/work_items/629245

Screenshots or screen recordings

Not applicable. This is a backend change with no UI.

How to set up and validate locally

Runs on a GDK at http://gdk.test:3000 with Duo and the MCP server already working. Repo at ~/gdk/gitlab.

  1. Check out the branch and restart Rails:
    cd ~/gdk/gitlab && git fetch origin 629245-semantic-search-default-scope && git checkout 629245-semantic-search-default-scope && gdk restart rails-web
  2. Create a token with the mcp scope and pick a project (an api token gets 403):
    cd ~/gdk/gitlab && bin/rails runner 'u = User.find_by_username("root"); t = u.personal_access_tokens.create!(name: "mcp-629245", scopes: [:mcp], expires_at: 7.days.from_now); p = Project.find_by_full_path("gitlab-org/gitlab-test") || Project.first; puts "export TOKEN=#{t.token} PROJECT=#{p.full_path}"'
    Paste the printed export ... line into your shell.
  3. Call semantic_search without scope:
    curl -s http://gdk.test:3000/api/v4/mcp -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -d '{"jsonrpc":"2.0","id":"v629245-noscope","method":"tools/call","params":{"name":"semantic_search","arguments":{"project_id":"'"$PROJECT"'","q":"authentication middleware"}}}' | jq '.result.content[0].text'
    Expected: the text does NOT say Validation error: scope is missing. If semantic search is not indexed on your GDK, you may see an error from the search backend instead. That is fine: it proves the call got past validation and was dispatched.
  4. Check the log line for that request:
    cd ~/gdk/gitlab && grep '"request_id":"v629245-noscope"' log/mcp.log | tail -n 1 | jq '{tool_name, dispatched_tool_name, argument_keys}'
    Expected: "tool_name": "semantic_search", "dispatched_tool_name": "semantic_code_search", "argument_keys": ["project_id", "q"]. On master there is no dispatched_tool_name, because validation fails first.
  5. Same check through the alias:
    curl -s http://gdk.test:3000/api/v4/mcp -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -d '{"jsonrpc":"2.0","id":"v629245-alias","method":"tools/call","params":{"name":"semantic_code_search","arguments":{"project_id":"'"$PROJECT"'","q":"authentication middleware"}}}' | jq '.result.content[0].text'
    cd ~/gdk/gitlab && grep '"request_id":"v629245-alias"' log/mcp.log | tail -n 1 | jq '{tool_name, dispatched_tool_name}'
    Expected: no scope validation error, and "dispatched_tool_name": "semantic_code_search".
  6. Negative check: an unsupported scope is still rejected:
    curl -s http://gdk.test:3000/api/v4/mcp -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -d '{"jsonrpc":"2.0","id":"v629245-badscope","method":"tools/call","params":{"name":"semantic_search","arguments":{"scope":"issues","project_id":"'"$PROJECT"'","q":"x"}}}' | jq '.result.content[0].text'
    Expected: text starts with Validation error and mentions scope.
  7. Check the schema: tools/list no longer lists scope as required:
    curl -s http://gdk.test:3000/api/v4/mcp -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -H "Accept: application/json, text/event-stream" -d '{"jsonrpc":"2.0","id":"v629245-list","method":"tools/list"}' | jq '.result.tools[] | select(.name=="semantic_search") | .inputSchema.required'
    Expected: ["q", "project_id"].

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Merge request reports

Loading
Loading