Skip to content

Commit 0f33dc0

Browse files
Default capability determination
1 parent 81fa3a9 commit 0f33dc0

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

lib/model_context_protocol/server.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,15 @@ def handle_request(request, method)
155155
end
156156

157157
def determine_capabilities
158-
{ prompts: {}, resources: {}, tools: {} }
158+
defines_prompts = @prompts.any? || @handlers[Methods::PROMPTS_LIST] != method(:list_prompts)
159+
defines_tools = @tools.any? || @handlers[Methods::TOOLS_LIST] != method(:list_tools)
160+
defines_resources = @resources.any? || @handlers[Methods::RESOURCES_LIST] != method(:list_resources)
161+
defines_resource_templates = @resource_templates.any? || @handlers[Methods::RESOURCES_TEMPLATES_LIST] != method(:list_resource_templates)
162+
{
163+
prompts: defines_prompts ? {} : nil,
164+
resources: defines_resources || defines_resource_templates ? {} : nil,
165+
tools: defines_tools ? {} : nil,
166+
}.compact
159167
end
160168

161169
def server_info

test/model_context_protocol/server_test.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,5 +726,67 @@ class ServerTest < ActiveSupport::TestCase
726726
response = server.handle(request)
727727
assert_equal custom_version, response[:result][:protocolVersion]
728728
end
729+
730+
test "has tool capability only if tools or a tools_list_handler is defined" do
731+
server_with_tools = Server.new(name: "test_server", tools: [@tool])
732+
733+
assert_includes server_with_tools.capabilities, :tools
734+
735+
server_with_handler = Server.new(name: "test_server")
736+
server_with_handler.tools_list_handler do
737+
[{ name: "test_tool", description: "Test tool" }]
738+
end
739+
740+
assert_includes server_with_handler.capabilities, :tools
741+
742+
server_without_tools = Server.new(name: "test_server")
743+
744+
refute_includes server_without_tools.capabilities, :tools
745+
end
746+
747+
test "has prompt capability only if prompts or a prompts_list_handler is defined" do
748+
server_with_prompts = Server.new(name: "test_server", prompts: [@prompt])
749+
750+
assert_includes server_with_prompts.capabilities, :prompts
751+
752+
server_with_handler = Server.new(name: "test_server")
753+
server_with_handler.prompts_list_handler do
754+
[{ name: "test_prompt", description: "Test prompt" }]
755+
end
756+
757+
assert_includes server_with_handler.capabilities, :prompts
758+
759+
server_without_prompts = Server.new(name: "test_server")
760+
761+
refute_includes server_without_prompts.capabilities, :prompts
762+
end
763+
764+
test "has resources capability only if resources, template or custom handler is defined" do
765+
server_with_resources = Server.new(name: "test_server", resources: [@resource])
766+
767+
assert_includes server_with_resources.capabilities, :resources
768+
769+
server_with_resource_template = Server.new(name: "test_server", resource_templates: [@resource_template])
770+
771+
assert_includes server_with_resource_template.capabilities, :resources
772+
773+
server_with_resources_list_handler = Server.new(name: "test_server")
774+
server_with_resources_list_handler.resources_list_handler do
775+
[{ uri: "test_resource", name: "Test resource", description: "Test resource" }]
776+
end
777+
778+
assert_includes server_with_resources_list_handler.capabilities, :resources
779+
780+
server_with_resources_templates_list_handler = Server.new(name: "test_server")
781+
server_with_resources_templates_list_handler.resources_templates_list_handler do
782+
[{ uri_template: "test_resource/{id}", name: "Test resource", description: "Test resource" }]
783+
end
784+
785+
assert_includes server_with_resources_templates_list_handler.capabilities, :resources
786+
787+
server_without_resources = Server.new(name: "test_server")
788+
789+
refute_includes server_without_resources.capabilities, :resources
790+
end
729791
end
730792
end

0 commit comments

Comments
 (0)