Add granular token authorization spec for the save_pipeline_job MCP tool

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Problem

The save_pipeline_job MCP tool was added in !264879 (merged) without an endpoint-level fine-grained token test. The MCP GraphQL integration docs (step 5) ask for a context per tool in spec/requests/api/mcp/granular_token_authorization_spec.rb, which calls the tool through /api/v4/mcp with a fine-grained personal access token.

The service spec already covers the three token cases, so this is a test gap, not a security bug.

There is also a problem with the existing spec: its subject posts form-encoded params, so integer arguments arrive as strings. For save_pipeline_job, schema validation returns Validation error: job_id is invalid before the tool runs. As a result, the deny example passes for the wrong reason and the allow example fails. Any future tool with an integer argument will hit the same issue.

Original discussion: !264879 (comment 4220895416)

Proposal

All changes are in spec/requests/api/mcp/granular_token_authorization_spec.rb.

  1. Post the request as JSON in the subject:

    post api('/mcp', personal_access_token: pat),
      params: { jsonrpc: '2.0', method: 'tools/call', id: '1', params: tool_params }, as: :json
  2. Add a context for the save_pipeline_job tool:

    context 'with the save_pipeline_job tool' do
      let_it_be(:pipeline) { create(:ci_pipeline, project: project, sha: project.commit.sha) }
      let(:job) { create(:ci_build, :playable, pipeline: pipeline) }
      let(:tool_params) { { name: 'save_pipeline_job', arguments: { project_id: project.full_path, job_id: job.id } } }
    
      it_behaves_like 'a tool enforcing granular token permissions', %i[run_job read_job read_pipeline]
    
      it 'does not play the job' do
        tool_result
    
        expect(job.reload).to be_manual
      end
    end

Grant run_job, read_job, and read_pipeline together. With run_job alone the job is played but the tool returns an error, which is tracked separately in #639710 and is out of scope here.

Implementation guide

  1. Set up the GDK if you haven't already.

  2. Make the two changes above.

  3. Run the spec:

    bin/rspec spec/requests/api/mcp/granular_token_authorization_spec.rb
  4. Open a merge request that references this issue.

Acceptance criteria

  • The subject in granular_token_authorization_spec.rb posts as: :json.
  • A with the save_pipeline_job tool context exists and uses the a tool enforcing granular token permissions shared example.
  • All existing examples in the file still pass (expected: 23 examples, 0 failures).
  • No production code changes.
Edited by 🤖 GitLab Bot 🤖