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_jsonlog 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_svalues for MCP requests.
This MR addresses these issues by:
- Restore the original HTTP method: Save the original
REQUEST_METHODbefore executing the REST route and restore it in the cleanup/ensure flow. - Suppress nested request logging: Use a custom subclass of
GrapeLogging::Middleware::RequestLoggerthat 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.
CallToolextracts it and records it inmcp.logusingLabkit::Fields::HTTP_STATUS_CODE, while preserving the existingtool_statusfield. 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
- Closes #638385.
- Related previous work: !268253 (closed) (closed).
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
- Check out this branch.
- Ensure the GitLab development environment and required Ruby dependencies are installed.
- 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
- Validate the behaviour using a route-backed MCP tool such as
list_branches:- Send a POST request to
/api/v4/mcpwith atools/callJSON-RPC payload. - Inspect
api_json.logand verify that only one entry is generated for the request correlation ID. - Verify that the entry retains
method: POSTand the MCP route. - Verify that request-level Gitaly and Redis counters are not duplicated and
db_duration_scovers the complete request. - Inspect
mcp.logand verify that the tool entry contains the actual REST HTTP status instatus, alongside the existingtool_status.
- Send a POST request to
MR acceptance checklist
- Implementation and regression specs added.
- Documentation updated.
git diff --checkpasses.- Targeted RSpec specs validated in CI.
- Review logging behaviour and middleware compatibility.
- Confirm no regressions in route-backed and aggregated MCP tools.