Make server_context optional for Tools and Prompts#54
Merged
topherbullock merged 2 commits intomodelcontextprotocol:mainfrom Jul 9, 2025
Conversation
This was referenced Jun 10, 2025
sambostock
reviewed
Jun 10, 2025
Contributor
sambostock
left a comment
There was a problem hiding this comment.
#56 explores the idea of making tools & prompts into simple data/config/value objects, and gets rid of the inheritance based approach in favour of the define based approach.
One of the benefits of this is that the implementation of the tool/prompt always uses a block, and blocks implicitly handle omitting arguments, meaning we can get rid of all the logic to do with conditionally passing server_context:.
- Updated `call` method in `MCP::Tool` and `MCP::Prompt` to accept `server_context` as an optional parameter. - Refactored `MCP::Server` to streamline argument handling for tool and prompt calls, introducing helper methods `call_tool_with_args` and `call_prompt_template_with_args`. - Adjusted tests to reflect changes in method signatures and ensure compatibility with optional `server_context` parameter.
8fcd48e to
c10c0dd
Compare
…_optional_and_sorbet_friendly
Contributor
Author
|
Hey @kfischer-okarin, anything else to add here? |
kfischer-okarin
approved these changes
Jun 19, 2025
Contributor
kfischer-okarin
left a comment
There was a problem hiding this comment.
Depending on how #56 turns out this might needs to change or even become unnecessage but this PR as it is now looks good to me for approve for now
topherbullock
approved these changes
Jul 9, 2025
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.
This PR makes the
server_contextparameter truly optional for both tools and prompts, improving type checker compatibility and addressing developer experience concerns.Motivation and Context
The previous implementation required
server_contextas a keyword argument in the baseToolandPromptclasses, which caused several issues:server_context:in its method signature, even when unusedHow Has This Been Tested?
Added comprehensive test coverage in
test/mcp/server_context_test.rbcovering all scenarios:For Tools:
**kwargsfor flexible parameter handlingFor Prompts:
All existing tests have been updated and continue to pass.
Breaking Changes
No breaking changes. This change maintains full backward compatibility:
server_contextparameters continue to work unchangedImplementation Details
1. Made server_context optional in base classes
2. Improved parameter detection
The new implementation properly inspects method parameters to determine if a tool/prompt accepts server_context:
This approach:
server_contextparameters**kwargsfor maximum flexibility3. Usage Examples
Now you can define tools and prompts more flexibly:
Types of changes
Checklist
Additional context
This PR addresses issue #39: Unintuitive use of meta programming for :server context in tool call by making the server_context handling explicit and transparent.
The change improves compatibility with Ruby type checkers (like Sorbet) and makes the gem more accessible to teams using static typing, while maintaining full backward compatibility for existing implementations.