Skip to content
Merged
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
22 changes: 11 additions & 11 deletions lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def initialize(
@version = version
@website_url = website_url
@instructions = instructions
@tool_names = tools.map(&:name_value)
@tools = tools.to_h { |t| [t.name_value, t] }
@prompts = prompts.to_h { |p| [p.name_value, p] }
@resources = resources
Expand Down Expand Up @@ -274,6 +273,7 @@ def initialize(
# so the Skills extension rules see the final Hash.
@capabilities = normalize_capabilities(capabilities)

validate_tool_names!(tools.map(&:name_value))
validate!

@client_capabilities = nil
Expand Down Expand Up @@ -345,10 +345,11 @@ def define_tool(name: nil, title: nil, description: nil, input_schema: nil, outp
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
@tools[tool_name] = tool
raise ToolNotUnique, [tool_name] if @tools.key?(tool_name)

validate!
validate!(tools: @tools.merge(tool_name => tool))
@tools[tool_name] = tool
nil
end

def define_prompt(name: nil, title: nil, description: nil, arguments: [], &block)
Expand Down Expand Up @@ -707,8 +708,7 @@ def build_sampling_params(

private

def validate!
validate_tool_name!
def validate!(tools: @tools)
validate_skills_capabilities!(@capabilities)

# NOTE: The draft protocol version is the next version after 2025-11-25.
Expand All @@ -718,7 +718,7 @@ def validate!
raise ArgumentError, message
end

tools_with_ref = @tools.each_with_object([]) do |(tool_name, tool), names|
tools_with_ref = tools.each_with_object([]) do |(tool_name, tool), names|
names << tool_name if schema_contains_ref?(tool.input_schema_value.to_h)
end
unless tools_with_ref.empty?
Expand All @@ -733,7 +733,7 @@ def validate!
raise ArgumentError, message
end

primitive_titles = [@tools.values, @prompts.values, @resources, @resource_templates].flatten.map(&:title)
primitive_titles = [tools.values, @prompts.values, @resources, @resource_templates].flatten.map(&:title)

if primitive_titles.any?
message = "Error occurred in #{primitive_titles.join(", ")}. `title` is not supported in protocol version 2025-03-26 or earlier"
Expand All @@ -747,7 +747,7 @@ def validate!
raise ArgumentError, message
end

error_tool_names = @tools.each_with_object([]) do |(tool_name, tool), error_tool_names|
error_tool_names = tools.each_with_object([]) do |(tool_name, tool), error_tool_names|
if tool.annotations
error_tool_names << tool_name
end
Expand All @@ -759,8 +759,8 @@ def validate!
end
end

def validate_tool_name!
duplicated_tool_names = @tool_names.tally.filter_map { |name, count| name if count >= 2 }
def validate_tool_names!(tool_names)
duplicated_tool_names = tool_names.tally.filter_map { |name, count| name if count >= 2 }

raise ToolNotUnique, duplicated_tool_names unless duplicated_tool_names.empty?
end
Expand Down
24 changes: 24 additions & 0 deletions test/mcp/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2938,10 +2938,14 @@ def read_resource_request(uri)

exception = assert_raises(ArgumentError) do
server.define_tool(
name: "example",
title: "Test tool",
)
end
assert_equal("Error occurred in Test tool. `title` is not supported in protocol version 2025-03-26 or earlier", exception.message)
assert_empty(server.tools)
server.define_tool(name: "example") {}
assert_equal(["example"], server.tools.keys)
end

test "raises error if `title` of prompt is used with protocol version 2025-03-26" do
Expand Down Expand Up @@ -3061,6 +3065,26 @@ def read_resource_request(uri)
assert_equal({ content: { "response" => "success" }, isError: false }, response[:result])
end

test "#define_tool preserves the registered tool after a duplicate definition" do
server = Server.new(name: "test")
server.define_tool(name: "example", description: "original") { Tool::Response.new("original") }
tools = server.tools
original = tools.fetch("example")

assert_raises(ToolNotUnique) do
server.define_tool(name: "example", description: "replacement") { Tool::Response.new("replacement") }
end

assert_same(tools, server.tools)
assert_same(original, tools.fetch("example"))
list = server.handle({ jsonrpc: "2.0", id: 1, method: "tools/list" })
assert_equal("original", list.fetch(:result).fetch(:tools).first.fetch(:description))
call = server.handle({ jsonrpc: "2.0", id: 2, method: "tools/call", params: { name: "example" } })
assert_equal("original", call.dig(:result, :content))
server.define_tool(name: "another") {}
assert_equal(["example", "another"], tools.keys)
end

test "#define_tool adds a tool with duplicated tool name to the server" do
error = assert_raises(MCP::ToolNotUnique) do
@server.define_tool(
Expand Down