From 0f33dc05e1fa30e531078c3fd23cb80853711cc3 Mon Sep 17 00:00:00 2001 From: Kevin Fischer Date: Wed, 14 May 2025 00:19:48 +0900 Subject: [PATCH] Default capability determination --- lib/model_context_protocol/server.rb | 10 +++- test/model_context_protocol/server_test.rb | 62 ++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/model_context_protocol/server.rb b/lib/model_context_protocol/server.rb index 1bfc882..944db86 100644 --- a/lib/model_context_protocol/server.rb +++ b/lib/model_context_protocol/server.rb @@ -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 diff --git a/test/model_context_protocol/server_test.rb b/test/model_context_protocol/server_test.rb index 2a35667..83650f5 100644 --- a/test/model_context_protocol/server_test.rb +++ b/test/model_context_protocol/server_test.rb @@ -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