Handle ping and unhandled notifications in MCP server

What does this MR do and why?

The main MCP endpoint (POST /api/v4/mcp) returned HTTP 404 for any method not in JSONRPC_METHOD_HANDLERS, including the spec-required ping request and notifications such as notifications/cancelled.

The same bug on the Orbit endpoint made GitLab Duo CLI drop the server after its first tool approval (#631580 (moved)). This MR fixes the main endpoint.

Changes

  • lib/api/mcp/handlers/ping.rb: new handler returning {}, as required by the MCP spec.
  • lib/api/mcp/base.rb: wire ping into JSONRPC_METHOD_HANDLERS; in invoke_basic_handler, return nil (which format_jsonrpc_response turns into 202 Accepted) for any notification (no id) with no handler, instead of calling method_not_found!.
  • spec/requests/api/mcp/handlers/ping_spec.rb: request specs for ping and for an unknown notification.

How to verify

# ping should return 200 with result: {}
curl -s -X POST http://gdk.test:3000/api/v4/mcp \
  -H "Authorization: Bearer $MCP_TOKEN" -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"ping"}' | jq .

# unknown notification should return 202 with empty body
curl -s -w " -> HTTP %{http_code}\n" -X POST http://gdk.test:3000/api/v4/mcp \
  -H "Authorization: Bearer $MCP_TOKEN" -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":1}}'

MR acceptance checklist

  • This MR has been reviewed against the MR acceptance checklist.
  • This contribution was prepared with the help of an AI assistant. I reviewed the changes and take responsibility for them.

Changelog: fixed Closes #632259 (closed)

Merge request reports

Loading
Loading