Skip to content

fix: validate tool definitions before registration - #754

Merged
koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/tool-registration-validation-before-mutation
Dec 4, 2026
Merged

koic merged 1 commit into
modelcontextprotocol:mainfrom
yorzi:fix/tool-registration-validation-before-mutation

Conversation

@yorzi

@yorzi yorzi commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Motivation and Context

Server#define_tool added the tool to the registry and to @tool_names before validating, so a call that raised still replaced a registered tool, kept a refused one, and left a duplicate name that made every later define_tool call raise.

define_tool now checks the name against the registered tools, then validates the registry with the new tool merged in, and changes the registry only after both pass. validate! takes the tools to check as an argument, defaulting to the registered ones. @tool_names is gone: Server.new still checks its tools: array for duplicate names, and define_tool checks the registry itself.

Fixes #747

How Has This Been Tested?

  • test/mcp/server_test.rb: a duplicate definition leaves the original tool in place, listed and callable, and a later tool can still be defined. The test for a title on 2025-03-26 now also checks that the refused tool is not registered and that the name can be defined afterwards.
  • Both tests fail on main.
  • With this change the script in the issue serves original, defines another, and leaves no tool after the refusal.

bundle exec rake passes on Ruby 4.0.10 (RuboCop, 2858 runs with 0 failures, and the conformance baseline). yard --no-output passes.

  • Automated unit / integration tests
  • Tested manually
  • Tested in a real application

Breaking Changes

None for a define_tool call that succeeds. One that raises no longer changes the registered tools.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Test update (non-breaking change which adds or updates tests)
  • Refactoring (non-breaking change which makes things better)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • CI/build script update (update CI workflow or build scripts)

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • I have added automated tests for my change
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Check duplicate names against the live map, then validate a candidate
map before updating the existing registry. Initialization still detects
duplicate input entries.

Fixes modelcontextprotocol#747.
@koic
koic merged commit ee65b6b into modelcontextprotocol:main Dec 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A rejected define_tool call still changes the server's tools

2 participants