Default capability determination

This commit is contained in:
Kevin Fischer 2025-05-14 00:19:48 +09:00
commit 0f33dc05e1
No known key found for this signature in database
GPG key ID: 8E4CB46A9AC8D1C1
2 changed files with 71 additions and 1 deletions

View file

@ -155,7 +155,15 @@ module ModelContextProtocol
end
def determine_capabilities
{ prompts: {}, resources: {}, tools: {} }
defines_prompts = @prompts.any? || @handlers[Methods::PROMPTS_LIST] != method(:list_prompts)
defines_tools = @tools.any? || @handlers[Methods::TOOLS_LIST] != method(:list_tools)
defines_resources = @resources.any? || @handlers[Methods::RESOURCES_LIST] != method(:list_resources)
defines_resource_templates = @resource_templates.any? || @handlers[Methods::RESOURCES_TEMPLATES_LIST] != method(:list_resource_templates)
{
prompts: defines_prompts ? {} : nil,
resources: defines_resources || defines_resource_templates ? {} : nil,
tools: defines_tools ? {} : nil,
}.compact
end
def server_info

View file

@ -726,5 +726,67 @@ module ModelContextProtocol
response = server.handle(request)
assert_equal custom_version, response[:result][:protocolVersion]
end
test "has tool capability only if tools or a tools_list_handler is defined" do
server_with_tools = Server.new(name: "test_server", tools: [@tool])
assert_includes server_with_tools.capabilities, :tools
server_with_handler = Server.new(name: "test_server")
server_with_handler.tools_list_handler do
[{ name: "test_tool", description: "Test tool" }]
end
assert_includes server_with_handler.capabilities, :tools
server_without_tools = Server.new(name: "test_server")
refute_includes server_without_tools.capabilities, :tools
end
test "has prompt capability only if prompts or a prompts_list_handler is defined" do
server_with_prompts = Server.new(name: "test_server", prompts: [@prompt])
assert_includes server_with_prompts.capabilities, :prompts
server_with_handler = Server.new(name: "test_server")
server_with_handler.prompts_list_handler do
[{ name: "test_prompt", description: "Test prompt" }]
end
assert_includes server_with_handler.capabilities, :prompts
server_without_prompts = Server.new(name: "test_server")
refute_includes server_without_prompts.capabilities, :prompts
end
test "has resources capability only if resources, template or custom handler is defined" do
server_with_resources = Server.new(name: "test_server", resources: [@resource])
assert_includes server_with_resources.capabilities, :resources
server_with_resource_template = Server.new(name: "test_server", resource_templates: [@resource_template])
assert_includes server_with_resource_template.capabilities, :resources
server_with_resources_list_handler = Server.new(name: "test_server")
server_with_resources_list_handler.resources_list_handler do
[{ uri: "test_resource", name: "Test resource", description: "Test resource" }]
end
assert_includes server_with_resources_list_handler.capabilities, :resources
server_with_resources_templates_list_handler = Server.new(name: "test_server")
server_with_resources_templates_list_handler.resources_templates_list_handler do
[{ uri_template: "test_resource/{id}", name: "Test resource", description: "Test resource" }]
end
assert_includes server_with_resources_templates_list_handler.capabilities, :resources
server_without_resources = Server.new(name: "test_server")
refute_includes server_without_resources.capabilities, :resources
end
end
end