From 81fa3a9efa264a2535269269367925c315c8a7c9 Mon Sep 17 00:00:00 2001 From: Kevin Fischer Date: Wed, 14 May 2025 00:10:32 +0900 Subject: [PATCH 1/3] Move default capabilities into a private method --- lib/model_context_protocol/server.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/model_context_protocol/server.rb b/lib/model_context_protocol/server.rb index e7075667..1bfc8827 100644 --- a/lib/model_context_protocol/server.rb +++ b/lib/model_context_protocol/server.rb @@ -20,7 +20,8 @@ def initialize(message, request, error_type: :internal_error, original_error: ni include Instrumentation - attr_accessor :name, :tools, :prompts, :resources, :server_context, :configuration, :capabilities + attr_writer :capabilities + attr_accessor :name, :tools, :prompts, :resources, :server_context, :configuration def initialize( name: "model_context_protocol", @@ -30,7 +31,7 @@ def initialize( resource_templates: [], server_context: nil, configuration: nil, - capabilities: { prompts: {}, resources: {}, tools: {} } + capabilities: nil ) @name = name @tools = tools.to_h { |t| [t.name_value, t] } @@ -40,7 +41,6 @@ def initialize( @resource_index = index_resources_by_uri(resources) @server_context = server_context @configuration = ModelContextProtocol.configuration.merge(configuration) - @capabilities = capabilities @handlers = { Methods::RESOURCES_LIST => method(:list_resources), @@ -60,6 +60,10 @@ def initialize( } end + def capabilities + @capabilities ||= determine_capabilities + end + def handle(request) JsonRpcHandler.handle(request) do |method| handle_request(request, method) @@ -150,6 +154,10 @@ def handle_request(request, method) } end + def determine_capabilities + { prompts: {}, resources: {}, tools: {} } + end + def server_info @server_info ||= { name:, From 0f33dc05e1fa30e531078c3fd23cb80853711cc3 Mon Sep 17 00:00:00 2001 From: Kevin Fischer Date: Wed, 14 May 2025 00:19:48 +0900 Subject: [PATCH 2/3] 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 1bfc8827..944db861 100644 --- a/lib/model_context_protocol/server.rb +++ b/lib/model_context_protocol/server.rb @@ -155,7 +155,15 @@ def handle_request(request, method) 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 2a356671..83650f59 100644 --- a/test/model_context_protocol/server_test.rb +++ b/test/model_context_protocol/server_test.rb @@ -726,5 +726,67 @@ class ServerTest < ActiveSupport::TestCase 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 From 032a28d660b8117a7dd58128349b2621f8e4920c Mon Sep 17 00:00:00 2001 From: Topher Bullock Date: Tue, 20 May 2025 17:05:29 -0400 Subject: [PATCH 3/3] Update lib/model_context_protocol/server.rb --- lib/model_context_protocol/server.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/model_context_protocol/server.rb b/lib/model_context_protocol/server.rb index 6e31f69f..b9a34464 100644 --- a/lib/model_context_protocol/server.rb +++ b/lib/model_context_protocol/server.rb @@ -22,7 +22,6 @@ def initialize(message, request, error_type: :internal_error, original_error: ni include Instrumentation - attr_writer :capabilities attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration