Skip to content

Commit 18f3e87

Browse files
Merge pull request #8 from kfischer-okarin/auto-determine-capabilities
Auto determine capabilities
2 parents 049bac6 + 032a28d commit 18f3e87

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

lib/model_context_protocol/server.rb

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def initialize(message, request, error_type: :internal_error, original_error: ni
2222

2323
include Instrumentation
2424

25-
attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration, :capabilities
25+
attr_writer :capabilities
26+
attr_accessor :name, :version, :tools, :prompts, :resources, :server_context, :configuration
2627

2728
def initialize(
2829
name: "model_context_protocol",
@@ -33,7 +34,7 @@ def initialize(
3334
resource_templates: [],
3435
server_context: nil,
3536
configuration: nil,
36-
capabilities: { prompts: {}, resources: {}, tools: {} }
37+
capabilities: nil
3738
)
3839
@name = name
3940
@version = version
@@ -44,7 +45,6 @@ def initialize(
4445
@resource_index = index_resources_by_uri(resources)
4546
@server_context = server_context
4647
@configuration = ModelContextProtocol.configuration.merge(configuration)
47-
@capabilities = capabilities
4848

4949
@handlers = {
5050
Methods::RESOURCES_LIST => method(:list_resources),
@@ -64,6 +64,10 @@ def initialize(
6464
}
6565
end
6666

67+
def capabilities
68+
@capabilities ||= determine_capabilities
69+
end
70+
6771
def handle(request)
6872
JsonRpcHandler.handle(request) do |method|
6973
handle_request(request, method)
@@ -154,6 +158,18 @@ def handle_request(request, method)
154158
}
155159
end
156160

161+
def determine_capabilities
162+
defines_prompts = @prompts.any? || @handlers[Methods::PROMPTS_LIST] != method(:list_prompts)
163+
defines_tools = @tools.any? || @handlers[Methods::TOOLS_LIST] != method(:list_tools)
164+
defines_resources = @resources.any? || @handlers[Methods::RESOURCES_LIST] != method(:list_resources)
165+
defines_resource_templates = @resource_templates.any? || @handlers[Methods::RESOURCES_TEMPLATES_LIST] != method(:list_resource_templates)
166+
{
167+
prompts: defines_prompts ? {} : nil,
168+
resources: defines_resources || defines_resource_templates ? {} : nil,
169+
tools: defines_tools ? {} : nil,
170+
}.compact
171+
end
172+
157173
def server_info
158174
@server_info ||= {
159175
name:,

test/model_context_protocol/server_test.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,5 +739,67 @@ class ServerTest < ActiveSupport::TestCase
739739
response = server.handle(request)
740740
assert_equal custom_version, response[:result][:protocolVersion]
741741
end
742+
743+
test "has tool capability only if tools or a tools_list_handler is defined" do
744+
server_with_tools = Server.new(name: "test_server", tools: [@tool])
745+
746+
assert_includes server_with_tools.capabilities, :tools
747+
748+
server_with_handler = Server.new(name: "test_server")
749+
server_with_handler.tools_list_handler do
750+
[{ name: "test_tool", description: "Test tool" }]
751+
end
752+
753+
assert_includes server_with_handler.capabilities, :tools
754+
755+
server_without_tools = Server.new(name: "test_server")
756+
757+
refute_includes server_without_tools.capabilities, :tools
758+
end
759+
760+
test "has prompt capability only if prompts or a prompts_list_handler is defined" do
761+
server_with_prompts = Server.new(name: "test_server", prompts: [@prompt])
762+
763+
assert_includes server_with_prompts.capabilities, :prompts
764+
765+
server_with_handler = Server.new(name: "test_server")
766+
server_with_handler.prompts_list_handler do
767+
[{ name: "test_prompt", description: "Test prompt" }]
768+
end
769+
770+
assert_includes server_with_handler.capabilities, :prompts
771+
772+
server_without_prompts = Server.new(name: "test_server")
773+
774+
refute_includes server_without_prompts.capabilities, :prompts
775+
end
776+
777+
test "has resources capability only if resources, template or custom handler is defined" do
778+
server_with_resources = Server.new(name: "test_server", resources: [@resource])
779+
780+
assert_includes server_with_resources.capabilities, :resources
781+
782+
server_with_resource_template = Server.new(name: "test_server", resource_templates: [@resource_template])
783+
784+
assert_includes server_with_resource_template.capabilities, :resources
785+
786+
server_with_resources_list_handler = Server.new(name: "test_server")
787+
server_with_resources_list_handler.resources_list_handler do
788+
[{ uri: "test_resource", name: "Test resource", description: "Test resource" }]
789+
end
790+
791+
assert_includes server_with_resources_list_handler.capabilities, :resources
792+
793+
server_with_resources_templates_list_handler = Server.new(name: "test_server")
794+
server_with_resources_templates_list_handler.resources_templates_list_handler do
795+
[{ uri_template: "test_resource/{id}", name: "Test resource", description: "Test resource" }]
796+
end
797+
798+
assert_includes server_with_resources_templates_list_handler.capabilities, :resources
799+
800+
server_without_resources = Server.new(name: "test_server")
801+
802+
refute_includes server_without_resources.capabilities, :resources
803+
end
742804
end
743805
end

0 commit comments

Comments
 (0)