Repository navigation
Add a tools_list_handler for context-dependent tool lists - #690
Open
mmcrockett wants to merge 1 commit into
Open
mmcrockett wants to merge 1 commit into
mmcrockett wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
Motivation
We run an MCP server inside a Rails app, mounted through
StreamableHTTPTransport. Every signed-in developer and QA user can use it, but three reporting tools are for admins only.Today the only way to express that is a guard on each restricted tool:
The guard can refuse the call, but it cannot stop the tool from being advertised: every user's
tools/liststill shows the admin tools, so the model sees them and tries them. The alternative is building aServerper request.With this change the rule lives in one place and covers both the list and the call:
Context
This is the tools equivalent of
resources_list_handler(#509, #507). The Python SDK (on_list_tools) and the TypeScript SDK (setRequestHandler('tools/list', ...)) both let the tool list vary per request.tools_list_handler(&block)returns the tool collection for the request. As in #509, the framework paginates the array and stamps the cache hints, and a block that declaresserver_context:receives anMCP::ServerContext.One difference from #509:
tools/callresolves against the same collection. A tool the block leaves out is refused asTool not found, and a tool it returns is callable whether or not it is registered. Resources can pair the list handler withresources_read_handler; tools have no call-side hook since #270, so a list-only handler would leave hidden tools callable by name.Unlike the handler removed in #270, this one is documented and receives
server_context:.Limits, both documented:
tools/listpage and once pertools/call.How Has This Been Tested?
New tests in
test/mcp/server_test.rbcover the list path (replacement,server_context:, pagination and cache hints) and the call path (hidden tool refused, unregistered tool callable,server_context:).rake test,rake rubocop, andrake conformance:testpass.I also ran a local build in the Rails app described above, over
StreamableHTTPTransport, replacing the per-tool guard with the handler. For a non-admin token:tools_list_handler)tools/listtools/callon an admin tooladmin only,isError: true-32602 Tool not foundtools/callon a shared toolAn admin token lists and calls all 8 tools in both.
Breaking Changes
None. With no handler set,
tools/listandtools/callbehave as before.Types of changes
Checklist
🤖 assisted by claude