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
4 changes: 2 additions & 2 deletions lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ def handle_json(request, session: nil)
end
end

def define_tool(name: nil, title: nil, description: nil, input_schema: nil, annotations: nil, meta: nil, &block)
tool = Tool.define(name: name, title: title, description: description, input_schema: input_schema, annotations: annotations, meta: meta, &block)
def define_tool(name: nil, title: nil, description: nil, input_schema: nil, output_schema: nil, annotations: nil, meta: nil, &block)
tool = Tool.define(name: name, title: title, description: description, input_schema: input_schema, output_schema: output_schema, annotations: annotations, meta: meta, &block)
tool_name = tool.name_value

@tool_names << tool_name
Expand Down
9 changes: 7 additions & 2 deletions test/mcp/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1127,19 +1127,24 @@ class Example < Tool
name: "defined_tool",
description: "Defined tool",
input_schema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
output_schema: { type: "object", properties: { response: { type: "string" } }, required: ["response"] },
meta: { foo: "bar" },
) do |message:|
Tool::Response.new(message)
Tool::Response.new({ "response" => message })
end

stored_tool = @server.instance_variable_get(:@tools)["defined_tool"]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
stored_tool = @server.instance_variable_get(:@tools)["defined_tool"]
stored_tool = @server.tools["defined_tool"]

assert_not_nil(stored_tool)
assert_equal(MCP::Tool::OutputSchema.new({ type: "object", properties: { response: { type: "string" } }, required: ["response"] }), stored_tool.output_schema)

response = @server.handle({
jsonrpc: "2.0",
method: "tools/call",
params: { name: "defined_tool", arguments: { message: "success" } },
id: 1,
})

assert_equal({ content: "success", isError: false }, response[:result])
assert_equal({ content: { "response" => "success" }, isError: false }, response[:result])
end

test "#define_tool adds a tool with duplicated tool name to the server" do
Expand Down