gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

feat(core,cli,mcp): Add MCP list_resources and read_resource core tools

Open MrLesk opened this issue 2 months ago • 2 comments

Summary

This PR follows up on the base MCP Resources PR https://github.com/google-gemini/gemini-cli/pull/13178 and gives the model the possibility to automatically call MCP resources_list and resource_read whenever it's relevant for the conversation.

Features added:

  • Add list_resources and read_resource core tools

Details

As agreed in https://github.com/google-gemini/gemini-cli/pull/13178, I am moving here the core tools implementation for MCP Resources. This will allow the model to automatically fetch the resources list and the detail when it considers relevant: image

Related Issues

Related to #1459

How to Validate

I tested successfully with Backlog.md MCP Server resources and additionally with the @modelcontextprotocol/server-everything.

Pre-Merge Checklist

  • [x] Updated relevant documentation and README (if needed)
  • [x] Added/updated tests (if needed)
  • [x] Noted breaking changes (if any)
  • [ ] Validated on required platforms/methods:
    • [x] MacOS
      • [x] npm run
      • [ ] npx
      • [ ] Docker
      • [ ] Podman
      • [ ] Seatbelt
    • [ ] Windows
      • [ ] npm run
      • [ ] npx
      • [ ] Docker
    • [ ] Linux
      • [ ] npm run
      • [ ] npx
      • [ ] Docker

MrLesk avatar Dec 09 '25 21:12 MrLesk

Summary of Changes

Hello @MrLesk, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly extends the core capabilities by integrating two new tools, list_resources and read_resource, which are designed to interact with Model Context Protocol (MCP) resources. This enhancement allows the model to dynamically discover and access external resources, making its interactions more informed and versatile. The changes build upon a previous MCP Resources PR, moving the core tool implementations into the main codebase.

Highlights

  • New Core Tools: Introduced list_resources and read_resource to enable the model to interact with Model Context Protocol (MCP) resources.
  • MCP Resource Listing: The list_resources tool allows discovering available MCP resources, with an option to filter by server name.
  • MCP Resource Reading: The read_resource tool facilitates reading the content of a specified MCP resource URI.
  • Enhanced Content Processing: Updated convertResourceContentsToParts in atCommandProcessor.ts to directly handle ReadResourceResult types, streamlining how resource contents are converted into model parts.
  • Comprehensive Testing: Dedicated unit tests were added for both ListResourcesTool and ReadResourceTool, covering various scenarios including error handling and abort signal support.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

gemini-code-assist[bot] avatar Dec 09 '25 21:12 gemini-code-assist[bot]

Hi @chrstnb @jackwotherspoon This is the first out of the 3 follow up PRs we agreed in #13178

While I was working on this PR I've noticed one leftover from the previous PR where I was not using a provided type from the mcp sdk for the ReadResourceResult which I fixed in this commit https://github.com/google-gemini/gemini-cli/pull/14854/commits/d6c292c775bf651f27e87a7517e278946ef8173b

Additionally, while in gemini-cli we might always use server:resourceURI format, there might be cases where users have certain instructions in gemini.md or paste some text with a direct resource URI (without server prefix and without using the @ command).

I handled this special case here https://github.com/google-gemini/gemini-cli/pull/14854/commits/9319d8936072d645b8e9c3cada667a7e767b18ee with a fallback that tries to match any resource that we already know.

MrLesk avatar Dec 09 '25 21:12 MrLesk

@chrstnb I think the PR is ready for a final review.

Upon a final check I noticed a bug in my previous PR that I fixed here: https://github.com/google-gemini/gemini-cli/pull/14854/commits/2c077f949a9a897ee83f46cdbc4b341f153bff00

MrLesk avatar Dec 18 '25 21:12 MrLesk

@chrstnb feel free to merge this PR

MrLesk avatar Dec 19 '25 18:12 MrLesk

Just chatted with @jacob314, can we add an e2e test to confirm that we're not sending the resource tools to the model when there are no resources?

@chrstnb @jacob314 I added the e2e test. you can see these specific changes here https://github.com/google-gemini/gemini-cli/pull/14854/commits/80a84c09b6823d1d93376a49a2a3d850add15602

This was a bit more complex than I initially expected because I couldn't find a way to get the available tools sent to the model. I had to add them to the /packages/core/src/telemetry/types.ts as you can see in my commit above. I also added 2 extra unit tests for that.

I've verified that the tests are passing using GEMINI_API_KEY=MY_API_KEY GEMINI_MODEL=gemini-3-flash-preview npm run test:e2e -- mcp-resources-tools.test.ts

Please let me know if I went into the right direction.

MrLesk avatar Dec 20 '25 21:12 MrLesk

Addressed the merge conflict, apologies for letting this sit for a bit. Thank you for being so thorough!

chrstnb avatar Jan 12 '26 16:01 chrstnb

@chrstnb I fixed the type errors and updated the branch. The PR is ready for a final review before merging

MrLesk avatar Jan 12 '26 20:01 MrLesk

Hi @MrLesk, thank you so much for your contribution to Gemini CLI! We really appreciate the time and effort you've put into this.

We're making some updates to our contribution process to improve how we track and review changes. Please take a moment to review our recent discussion post: Improving Our Contribution Process & Introducing New Guidelines.

Key Update: Starting January 26, 2026, the Gemini CLI project will require all pull requests to be associated with an existing issue. Any pull requests not linked to an issue by that date will be automatically closed.

Thank you for your understanding and for being a part of our community!

bdmorgan avatar Jan 15 '26 03:01 bdmorgan

hey @bdmorgan. This PR is a follow up of another PR that was related to an issue. Also there is #15852

In addition to this, the PR is almost ready to be merged

MrLesk avatar Jan 15 '26 22:01 MrLesk

@chrstnb I noticed that the e2e tests were failing. I believe due to a race condition due to the slower machines on CI. I modified them to wait until the tools are loaded before checking for the mcp resources tool. This should hopefully do the job for the tests once you approve the changes

MrLesk avatar Jan 20 '26 19:01 MrLesk

Looks like the sandbox docker test is failing

 ❯ mcp-resources-tools.test.ts (3 tests | 3 failed) 336712ms
   × mcp-resources-tools > does not send resource tools to model when MCP server has no resources capability 104985ms (retry x2)
     → expected [ Array(14) ] to include 'ping'
     → expected [ Array(14) ] to include 'ping'
     → expected [ Array(14) ] to include 'ping'
   × mcp-resources-tools > does not send resource tools to model when MCP server has resources capability but empty list 128622ms (retry x2)
     → expected [ Array(14) ] to include 'ping'
     → expected [ Array(14) ] to include 'ping'
     → expected [ Array(14) ] to include 'ping'
   × mcp-resources-tools > sends resource tools to model when MCP server has resources 103103ms (retry x2)
     → expected [ Array(14) ] to include 'list_resources'
     → expected [ Array(14) ] to include 'list_resources'
     → expected [ Array(14) ] to include 'list_resources'

⎯⎯⎯⎯⎯⎯⎯ Failed Tests 3 ⎯⎯⎯⎯⎯⎯⎯

 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > does not send resource tools to model when MCP server has no resources capability
 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > does not send resource tools to model when MCP server has no resources capability
 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > does not send resource tools to model when MCP server has no resources capability
AssertionError: expected [ Array(14) ] to include 'ping'
 ❯ mcp-resources-tools.test.ts:139:23
    137| 
    138|     // But other tools should still be available (sanity check)
    139|     expect(toolNames).toContain('ping');
       |                       ^
    140|   });
    141| 

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/9]⎯

 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > does not send resource tools to model when MCP server has resources capability but empty list
 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > does not send resource tools to model when MCP server has resources capability but empty list
 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > does not send resource tools to model when MCP server has resources capability but empty list
AssertionError: expected [ Array(14) ] to include 'ping'
 ❯ mcp-resources-tools.test.ts:172:23
    170| 
    171|     // But other tools should still be available (sanity check)
    172|     expect(toolNames).toContain('ping');
       |                       ^
    173|   });
    174| 

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[2/9]⎯

 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > sends resource tools to model when MCP server has resources
 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > sends resource tools to model when MCP server has resources
 FAIL  mcp-resources-tools.test.ts > mcp-resources-tools > sends resource tools to model when MCP server has resources
AssertionError: expected [ Array(14) ] to include 'list_resources'
 ❯ mcp-resources-tools.test.ts:217:23
    215| 
    216|     // Resource tools SHOULD be sent to the model when resources exist
    217|     expect(toolNames).toContain('list_resources');
       |                       ^
    218|     expect(toolNames).toContain('read_resource');
    219| 

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[3/9]⎯


 Test Files  1 failed | 22 passed | 5 skipped (28)
      Tests  3 failed | 76 passed | 17 skipped (96)
   Start at  19:46:09
   Duration  341.44s (transform 5.27s, setup 0ms, collect 91.38s, tests 888.12s, environment 7ms, prepare 2.72s)

Error: Process completed with exit code 1.

bdmorgan avatar Jan 25 '26 20:01 bdmorgan

Hey @bdmorgan The test was failing due to incorrect mcp host in the tests. It needs a different one when running on the host compared to running on docker. I pushed a fix and validated by running GEMINI_API_KEY="myApiKey" GEMINI_MODEL=gemini-3-flash-preview npm run test:integration:sandbox:docker

Test Files 22 passed | 6 skipped (28) Tests 78 passed | 18 skipped (96) Start at 19:05:40 Duration 196.59s

MrLesk avatar Jan 26 '26 18:01 MrLesk

Thanks - now there's a merge conflict. Can you resolve this then tag me - I will attempt to merge at that point.

bdmorgan avatar Jan 27 '26 12:01 bdmorgan

@bdmorgan it should work fine now and sorry for taking so much of your time.

MrLesk avatar Jan 27 '26 21:01 MrLesk