Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/mcp/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def to_h
title: title_value,
description: description_value,
icons: icons_value&.then { |icons| icons.empty? ? nil : icons.map(&:to_h) },
arguments: arguments_value&.map(&:to_h),
arguments: arguments_value.empty? ? nil : arguments_value.map(&:to_h),
_meta: meta_value,
}.compact
end
Expand All @@ -32,7 +32,7 @@ def inherited(subclass)
subclass.instance_variable_set(:@title_value, nil)
subclass.instance_variable_set(:@description_value, nil)
subclass.instance_variable_set(:@icons_value, nil)
subclass.instance_variable_set(:@arguments_value, nil)
subclass.instance_variable_set(:@arguments_value, [])
subclass.instance_variable_set(:@meta_value, nil)
end

Expand Down Expand Up @@ -76,7 +76,7 @@ def arguments(value = NOT_SET)
if value == NOT_SET
@arguments_value
else
@arguments_value = value
@arguments_value = Array(value)
end
end

Expand All @@ -103,6 +103,7 @@ def define(name: nil, title: nil, description: nil, icons: [], arguments: [], me
end

def validate_arguments!(args)
args ||= {}
missing = required_args - args.keys
return if missing.empty?

Expand Down
47 changes: 46 additions & 1 deletion test/mcp/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class FullPrompt < Prompt
assert_equal expected, FullPrompt.to_h
end

test "#to_h handles nil arguments value" do
test "#to_h omits arguments key when arguments are not declared" do
class NoArgumentsPrompt < Prompt
description "No arguments prompt"
end
Expand All @@ -196,6 +196,51 @@ class NoArgumentsPrompt < Prompt
assert_equal expected, prompt.to_h
end

test "#validate_arguments! does not crash when arguments are not declared" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of "crash" (which evokes the process crashing and core-dumping) can we say "raise"?

prompt_class = Class.new(Prompt) do
prompt_name "no_args_prompt"
description "A prompt with no arguments"
# NOTE: no `arguments` declaration at all
end

assert_nothing_raised do
prompt_class.validate_arguments!({})
end
end

test "#validate_arguments! handles nil args" do
prompt_class = Class.new(Prompt) do
prompt_name "no_args_prompt"
description "A prompt with no arguments"
end

assert_nothing_raised do
prompt_class.validate_arguments!(nil)
end
end

test "#validate_arguments! does not crash when arguments is explicitly set to nil" do
prompt_class = Class.new(Prompt) do
prompt_name "nil_args_prompt"
description "A prompt with nil arguments"
arguments nil
end

assert_nothing_raised do
prompt_class.validate_arguments!({})
end
end

test "#to_h omits arguments key when arguments is empty" do
prompt = Prompt.define(
name: "no_args_prompt",
description: "a prompt without arguments",
arguments: [],
)

refute prompt.to_h.key?(:arguments)
end

test "#to_h does not have `:icons` key when icons is empty" do
prompt = Prompt.define(
name: "prompt_without_icons",
Expand Down