Skip to content

Commit d7c866e

Browse files
Simplify capability handling again
1 parent 8dda58e commit d7c866e

File tree

3 files changed

+13
-277
lines changed

3 files changed

+13
-277
lines changed

lib/mcp/server.rb

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require "json_rpc_handler"
44
require_relative "instrumentation"
55
require_relative "methods"
6-
require_relative "server/capabilities"
76

87
module MCP
98
class Server
@@ -23,8 +22,7 @@ def initialize(message, request, error_type: :internal_error, original_error: ni
2322

2423
include Instrumentation
2524

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

2927
def initialize(
3028
name: "model_context_protocol",
@@ -47,10 +45,7 @@ def initialize(
4745
@resource_index = index_resources_by_uri(resources)
4846
@server_context = server_context
4947
@configuration = MCP.configuration.merge(configuration)
50-
@capabilities = Capabilities.new(capabilities)
51-
@capabilities.support_tools if tools.any?
52-
@capabilities.support_prompts if prompts.any?
53-
@capabilities.support_resources if resources.any? || resource_templates.any?
48+
@capabilities = capabilities || default_capabilities
5449

5550
@handlers = {
5651
Methods::RESOURCES_LIST => method(:list_resources),
@@ -72,10 +67,6 @@ def initialize(
7267
@transport = transport
7368
end
7469

75-
def capabilities
76-
@capabilities.to_h
77-
end
78-
7970
def handle(request)
8071
JsonRpcHandler.handle(request) do |method|
8172
handle_request(request, method)
@@ -123,7 +114,6 @@ def notify_resources_list_changed
123114
end
124115

125116
def resources_list_handler(&block)
126-
@capabilities.support_resources
127117
@handlers[Methods::RESOURCES_LIST] = block
128118
end
129119

@@ -132,12 +122,10 @@ def resources_read_handler(&block)
132122
end
133123

134124
def resources_templates_list_handler(&block)
135-
@capabilities.support_resources
136125
@handlers[Methods::RESOURCES_TEMPLATES_LIST] = block
137126
end
138127

139128
def tools_list_handler(&block)
140-
@capabilities.support_tools
141129
@handlers[Methods::TOOLS_LIST] = block
142130
end
143131

@@ -146,7 +134,6 @@ def tools_call_handler(&block)
146134
end
147135

148136
def prompts_list_handler(&block)
149-
@capabilities.support_prompts
150137
@handlers[Methods::PROMPTS_LIST] = block
151138
end
152139

@@ -194,6 +181,14 @@ def handle_request(request, method)
194181
}
195182
end
196183

184+
def default_capabilities
185+
{
186+
tools: { listChanged: true },
187+
prompts: { listChanged: true },
188+
resources: { listChanged: true },
189+
}
190+
end
191+
197192
def server_info
198193
@server_info ||= {
199194
name:,

test/mcp/server/capabilities_test.rb

Lines changed: 0 additions & 197 deletions
This file was deleted.

test/mcp/server_test.rb

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ class ServerTest < ActiveSupport::TestCase
116116
"result": {
117117
"protocolVersion": "2024-11-05",
118118
"capabilities": {
119-
"prompts": {},
120-
"resources": {},
121-
"tools": {},
119+
"prompts": { listChanged: true },
120+
"resources": { listChanged: true },
121+
"tools": { listChanged: true },
122122
},
123123
"serverInfo": {
124124
"name": @server_name,
@@ -778,68 +778,6 @@ def call(message:, server_context: nil)
778778
assert_equal custom_version, response[:result][:protocolVersion]
779779
end
780780

781-
test "has tool capability only if tools or a tools_list_handler is defined" do
782-
server_with_tools = Server.new(name: "test_server", tools: [@tool])
783-
784-
assert_includes server_with_tools.capabilities, :tools
785-
786-
server_with_handler = Server.new(name: "test_server")
787-
server_with_handler.tools_list_handler do
788-
[{ name: "test_tool", description: "Test tool" }]
789-
end
790-
791-
assert_includes server_with_handler.capabilities, :tools
792-
793-
server_without_tools = Server.new(name: "test_server")
794-
795-
refute_includes server_without_tools.capabilities, :tools
796-
end
797-
798-
test "has prompt capability only if prompts or a prompts_list_handler is defined" do
799-
server_with_prompts = Server.new(name: "test_server", prompts: [@prompt])
800-
801-
assert_includes server_with_prompts.capabilities, :prompts
802-
803-
server_with_handler = Server.new(name: "test_server")
804-
server_with_handler.prompts_list_handler do
805-
[{ name: "test_prompt", description: "Test prompt" }]
806-
end
807-
808-
assert_includes server_with_handler.capabilities, :prompts
809-
810-
server_without_prompts = Server.new(name: "test_server")
811-
812-
refute_includes server_without_prompts.capabilities, :prompts
813-
end
814-
815-
test "has resources capability only if resources, template or custom handler is defined" do
816-
server_with_resources = Server.new(name: "test_server", resources: [@resource])
817-
818-
assert_includes server_with_resources.capabilities, :resources
819-
820-
server_with_resource_template = Server.new(name: "test_server", resource_templates: [@resource_template])
821-
822-
assert_includes server_with_resource_template.capabilities, :resources
823-
824-
server_with_resources_list_handler = Server.new(name: "test_server")
825-
server_with_resources_list_handler.resources_list_handler do
826-
[{ uri: "test_resource", name: "Test resource", description: "Test resource" }]
827-
end
828-
829-
assert_includes server_with_resources_list_handler.capabilities, :resources
830-
831-
server_with_resources_templates_list_handler = Server.new(name: "test_server")
832-
server_with_resources_templates_list_handler.resources_templates_list_handler do
833-
[{ uri_template: "test_resource/{id}", name: "Test resource", description: "Test resource" }]
834-
end
835-
836-
assert_includes server_with_resources_templates_list_handler.capabilities, :resources
837-
838-
server_without_resources = Server.new(name: "test_server")
839-
840-
refute_includes server_without_resources.capabilities, :resources
841-
end
842-
843781
test "tools/call validates arguments against input schema when validate_tool_call_arguments is true" do
844782
server = Server.new(
845783
tools: [TestTool],

0 commit comments

Comments
 (0)