fix(mcp): prevent duplicate API request logging

What does this MR do and why?

Fixes duplicate and incorrectly formatted api_json log entries generated when an MCP tool executes a REST API route internally.

Previously, Mcp::Tools::Base::ApiTool#execute changed Rack::REQUEST_METHOD to the underlying REST route's method but did not restore the original MCP request method. The nested route execution also passed through the Grape request logger, producing a second api_json entry for the same request.

This caused:

  • Duplicate api_json log entries with the same correlation ID.
  • The original MCP POST request to be incorrectly logged as the underlying REST method (GET, PUT, etc.).
  • Duplicate Gitaly and Redis request counters.
  • The nested request logger to reset the database timer, resulting in incomplete db_duration_s values for MCP requests.

This MR addresses these issues by:

  • Restore the original HTTP method: Save the original REQUEST_METHOD before executing the REST route and restore it in the cleanup/ensure flow.
  • Suppress nested request logging: Use a custom subclass of GrapeLogging::Middleware::RequestLogger that skips logging while an MCP route-backed tool executes internally. The skip flag is deleted during cleanup.
  • Record the actual REST HTTP status: Carry the underlying REST route's HTTP status as internal metadata on the tool result. CallTool extracts it and records it in mcp.log using Labkit::Fields::HTTP_STATUS_CODE, while preserving the existing tool_status field. The internal metadata is stripped before returning the result to the MCP client.
  • Add regression coverage: Add unit and request specs covering HTTP method restoration, nested logger suppression, database timer behaviour, and MCP tool logging.
  • Update documentation: Document the route-backed MCP tool logging behaviour and relevant logging details.

The expected result is one correctly logged MCP request per route-backed tool call, with the original HTTP method, complete request-level database timing and counters, and the REST route's actual HTTP status recorded in mcp.log.

References

Screenshots or screen recordings

Not applicable. This is a backend logging and request-processing change.

Before

A single MCP request generates two api_json entries with the same correlation ID. Both can report the underlying REST method instead of the original MCP POST.

After

A single MCP request generates one api_json entry with the original POST method. The MCP tool log records the actual REST HTTP status.

How to set up and validate locally

  1. Check out this branch.
  2. Ensure the GitLab development environment and required Ruby dependencies are installed.
  3. Run the targeted regression specs:

bundle exec rspec \ spec/services/mcp/tools/base/api_tool_spec.rb \ spec/lib/api/mcp/handlers/call_tool_spec.rb \ spec/requests/api/mcp/handlers/call_tool_spec.rb \ spec/lib/gitlab/grape_logging/middleware/request_logger_spec.rb

  1. Validate the behaviour using a route-backed MCP tool such as list_branches:
    • Send a POST request to /api/v4/mcp with a tools/call JSON-RPC payload.
    • Inspect api_json.log and verify that only one entry is generated for the request correlation ID.
    • Verify that the entry retains method: POST and the MCP route.
    • Verify that request-level Gitaly and Redis counters are not duplicated and db_duration_s covers the complete request.
    • Inspect mcp.log and verify that the tool entry contains the actual REST HTTP status in status, alongside the existing tool_status.

MR acceptance checklist

  • Implementation and regression specs added.
  • Documentation updated.
  • git diff --check passes.
  • Targeted RSpec specs validated in CI.
  • Review logging behaviour and middleware compatibility.
  • Confirm no regressions in route-backed and aggregated MCP tools.
Edited by Aryan Rajput

Merge request reports

Loading
Loading