Skip to content

Commit 8fcd48e

Browse files
committed
Refactor MCP tool and server to handle optional server_context parameter
- 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.
1 parent 04710ac commit 8fcd48e

File tree

6 files changed

+487
-13
lines changed

6 files changed

+487
-13
lines changed

lib/mcp/prompt.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class << self
99
attr_reader :description_value
1010
attr_reader :arguments_value
1111

12-
def template(args, server_context:)
12+
def template(args, server_context: nil)
1313
raise NotImplementedError, "Subclasses must implement template"
1414
end
1515

@@ -57,7 +57,7 @@ def define(name: nil, description: nil, arguments: [], &block)
5757
prompt_name name
5858
description description
5959
arguments arguments
60-
define_singleton_method(:template) do |args, server_context:|
60+
define_singleton_method(:template) do |args, server_context: nil|
6161
instance_exec(args, server_context:, &block)
6262
end
6363
end

lib/mcp/server.rb

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,7 @@ def call_tool(request)
211211
end
212212

213213
begin
214-
call_params = tool_call_parameters(tool)
215-
216-
if call_params.include?(:server_context)
217-
tool.call(**arguments.transform_keys(&:to_sym), server_context:).to_h
218-
else
219-
tool.call(**arguments.transform_keys(&:to_sym)).to_h
220-
end
214+
call_tool_with_args(tool, arguments)
221215
rescue => e
222216
raise RequestHandlerError.new("Internal error calling tool #{tool_name}", request, original_error: e)
223217
end
@@ -242,7 +236,7 @@ def get_prompt(request)
242236
prompt_args = request[:arguments]
243237
prompt.validate_arguments!(prompt_args)
244238

245-
prompt.template(prompt_args, server_context:).to_h
239+
call_prompt_template_with_args(prompt, prompt_args)
246240
end
247241

248242
def list_resources(request)
@@ -292,5 +286,33 @@ def tool_call_method_def(tool)
292286
method
293287
end
294288
end
289+
290+
def call_tool_with_args(tool, arguments)
291+
args = arguments.transform_keys(&:to_sym)
292+
293+
# Check if the tool accepts server_context or has **kwargs
294+
parameters = tool.method(:call).parameters
295+
accepts_server_context = parameters.any? { |_type, name| name == :server_context }
296+
has_kwargs = parameters.any? { |type, _| type == :keyrest }
297+
298+
if accepts_server_context || has_kwargs
299+
tool.call(**args, server_context: server_context).to_h
300+
else
301+
tool.call(**args).to_h
302+
end
303+
end
304+
305+
def call_prompt_template_with_args(prompt, args)
306+
# Check if the prompt accepts server_context or has **kwargs
307+
parameters = prompt.method(:template).parameters
308+
accepts_server_context = parameters.any? { |_type, name| name == :server_context }
309+
has_kwargs = parameters.any? { |type, _| type == :keyrest }
310+
311+
if accepts_server_context || has_kwargs
312+
prompt.template(args, server_context: server_context).to_h
313+
else
314+
prompt.template(args).to_h
315+
end
316+
end
295317
end
296318
end

lib/mcp/tool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class << self
99
attr_reader :input_schema_value
1010
attr_reader :annotations_value
1111

12-
def call(*args, server_context:)
12+
def call(*args, server_context: nil)
1313
raise NotImplementedError, "Subclasses must implement call"
1414
end
1515

0 commit comments

Comments
 (0)