Make server_context
optional for Tools and Prompts
#54
+477
−24
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_context
parameter truly optional for both tools and prompts, improving type checker compatibility and addressing developer experience concerns.Motivation and Context
The previous implementation required
server_context
as a keyword argument in the baseTool
andPrompt
classes, 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.rb
covering all scenarios:For Tools:
**kwargs
for 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_context
parameters 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_context
parameters**kwargs
for 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.