Skip to content

Commit 805e020

Browse files
committed
Remove undocumented handler override methods
## Motivation and Context The following public methods allow replacing the SDK's built-in request handlers: - `tools_call_handler` - `tools_list_handler` - `resources_list_handler` - `resources_templates_list_handler` - `prompts_list_handler` - `prompts_get_handler` These were introduced in Internal Release 0.2.0 (6004f42) as part of "allow setting handlers on Server", before declarative APIs (`define_tool`, `Prompt` class, `Resource` class) were fully established. Now that those declarative APIs exist, these handler overrides are redundant: none are documented in the README.md, and no usage exists outside of the SDK's own tests. These handler overrides also create architectural problems. Since custom handlers cannot receive session context, they would not work correctly with a per-session architecture, such as session-scoped notifications. ### Changes - Removed `tools_call_handler`, `tools_list_handler`, `resources_list_handler`, `resources_templates_list_handler`, `prompts_list_handler`, and `prompts_get_handler` from `Server`. - Removed the associated tests. - Retained `resources_read_handler` (documented in the README.md and actively used in examples and conformance server, because resource reading is application-specific). ## Breaking Change The following methods are removed without a deprecation period: `tools_call_handler`, `tools_list_handler`, `resources_list_handler`, `resources_templates_list_handler`, `prompts_list_handler`, `prompts_get_handler`. These were never documented in the README.md and have no known usage outside of the SDK's own tests. As a result, little to no impact on users is expected, and a deprecation warning would have no practical audience. Users who relied on these should use `define_tool`, prompt/resource registration, or `resources_read_handler` instead.
1 parent 3b1fc72 commit 805e020

File tree

2 files changed

+6
-147
lines changed

2 files changed

+6
-147
lines changed

lib/mcp/server.rb

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -199,34 +199,16 @@ def notify_log_message(data:, level:, logger: nil)
199199
report_exception(e, { notification: "log_message" })
200200
end
201201

202-
def resources_list_handler(&block)
203-
@handlers[Methods::RESOURCES_LIST] = block
204-
end
205-
202+
# Sets a custom handler for `resources/read` requests.
203+
# The block receives the parsed request params and should return resource
204+
# contents. The return value is set as the `contents` field of the response.
205+
#
206+
# @yield [params] The request params containing `:uri`.
207+
# @yieldreturn [Array<Hash>, Hash] Resource contents.
206208
def resources_read_handler(&block)
207209
@handlers[Methods::RESOURCES_READ] = block
208210
end
209211

210-
def resources_templates_list_handler(&block)
211-
@handlers[Methods::RESOURCES_TEMPLATES_LIST] = block
212-
end
213-
214-
def tools_list_handler(&block)
215-
@handlers[Methods::TOOLS_LIST] = block
216-
end
217-
218-
def tools_call_handler(&block)
219-
@handlers[Methods::TOOLS_CALL] = block
220-
end
221-
222-
def prompts_list_handler(&block)
223-
@handlers[Methods::PROMPTS_LIST] = block
224-
end
225-
226-
def prompts_get_handler(&block)
227-
@handlers[Methods::PROMPTS_GET] = block
228-
end
229-
230212
private
231213

232214
def validate!

test/mcp/server_test.rb

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -270,23 +270,6 @@ class ServerTest < ActiveSupport::TestCase
270270
assert_equal({ foo: "bar" }, result[:tools][0][:_meta])
271271
end
272272

273-
test "#tools_list_handler sets the tools/list handler" do
274-
@server.tools_list_handler do
275-
[{ name: "hammer", description: "Hammer time!" }]
276-
end
277-
278-
request = {
279-
jsonrpc: "2.0",
280-
method: "tools/list",
281-
id: 1,
282-
}
283-
284-
response = @server.handle(request)
285-
result = response[:result]
286-
assert_equal({ tools: [{ name: "hammer", description: "Hammer time!" }] }, result)
287-
assert_instrumentation_data({ method: "tools/list" })
288-
end
289-
290273
test "#handle tools/call executes tool and returns result" do
291274
tool_name = "test_tool"
292275
tool_args = { arg: "value" }
@@ -564,24 +547,6 @@ class Example < Tool
564547
assert_includes response[:error][:data], "Tool not found: unknown_tool"
565548
end
566549

567-
test "#tools_call_handler sets the tools/call handler" do
568-
@server.tools_call_handler do |request|
569-
tool_name = request[:name]
570-
Tool::Response.new("#{tool_name} called successfully").to_h
571-
end
572-
573-
request = {
574-
jsonrpc: "2.0",
575-
method: "tools/call",
576-
params: { name: "my_tool", arguments: {} },
577-
id: 1,
578-
}
579-
580-
response = @server.handle(request)
581-
assert_equal({ content: "my_tool called successfully", isError: false }, response[:result])
582-
assert_instrumentation_data({ method: "tools/call" })
583-
end
584-
585550
test "#handle prompts/list returns list of prompts" do
586551
request = {
587552
jsonrpc: "2.0",
@@ -594,22 +559,6 @@ class Example < Tool
594559
assert_instrumentation_data({ method: "prompts/list" })
595560
end
596561

597-
test "#prompts_list_handler sets the prompts/list handler" do
598-
@server.prompts_list_handler do
599-
[{ name: "foo_prompt", description: "Foo prompt" }]
600-
end
601-
602-
request = {
603-
jsonrpc: "2.0",
604-
method: "prompts/list",
605-
id: 1,
606-
}
607-
608-
response = @server.handle(request)
609-
assert_equal({ prompts: [{ name: "foo_prompt", description: "Foo prompt" }] }, response[:result])
610-
assert_instrumentation_data({ method: "prompts/list" })
611-
end
612-
613562
test "#handle prompts/get returns templated prompt" do
614563
request = {
615564
jsonrpc: "2.0",
@@ -669,32 +618,6 @@ class Example < Tool
669618
})
670619
end
671620

672-
test "#prompts_get_handler sets the prompts/get handler" do
673-
@server.prompts_get_handler do |request|
674-
prompt_name = request[:name]
675-
Prompt::Result.new(
676-
description: prompt_name,
677-
messages: [
678-
Prompt::Message.new(role: "user", content: Content::Text.new(request[:arguments]["foo"])),
679-
],
680-
).to_h
681-
end
682-
683-
request = {
684-
jsonrpc: "2.0",
685-
method: "prompts/get",
686-
id: 1,
687-
params: { name: "foo_bar_prompt", arguments: { "foo" => "bar" } },
688-
}
689-
690-
response = @server.handle(request)
691-
assert_equal(
692-
{ description: "foo_bar_prompt", messages: [{ role: "user", content: { type: "text", text: "bar" } }] },
693-
response[:result],
694-
)
695-
assert_instrumentation_data({ method: "prompts/get" })
696-
end
697-
698621
test "#handle resources/list returns a list of resources" do
699622
request = {
700623
jsonrpc: "2.0",
@@ -707,25 +630,6 @@ class Example < Tool
707630
assert_instrumentation_data({ method: "resources/list" })
708631
end
709632

710-
test "#resources_list_handler sets the resources/list handler" do
711-
@server.resources_list_handler do
712-
[{ uri: "https://test_resource.invalid", name: "test-resource", title: "Test Resource", description: "Test resource" }]
713-
end
714-
715-
request = {
716-
jsonrpc: "2.0",
717-
method: "resources/list",
718-
id: 1,
719-
}
720-
721-
response = @server.handle(request)
722-
assert_equal(
723-
{ resources: [{ uri: "https://test_resource.invalid", name: "test-resource", title: "Test Resource", description: "Test resource" }] },
724-
response[:result],
725-
)
726-
assert_instrumentation_data({ method: "resources/list" })
727-
end
728-
729633
test "#handle resources/read returns an empty array of contents by default" do
730634
request = {
731635
jsonrpc: "2.0",
@@ -783,33 +687,6 @@ class Example < Tool
783687
assert_instrumentation_data({ method: "resources/templates/list" })
784688
end
785689

786-
test "#resources_templates_list_handler sets the resources/templates/list handler" do
787-
@server.resources_templates_list_handler do
788-
[{ uriTemplate: "test_resource_template/{id}", name: "Test resource template", description: "a template" }]
789-
end
790-
791-
request = {
792-
jsonrpc: "2.0",
793-
method: "resources/templates/list",
794-
id: 1,
795-
}
796-
797-
response = @server.handle(request)
798-
assert_equal(
799-
{
800-
resourceTemplates: [
801-
{
802-
uriTemplate: "test_resource_template/{id}",
803-
name: "Test resource template",
804-
description: "a template",
805-
},
806-
],
807-
},
808-
response[:result],
809-
)
810-
assert_instrumentation_data({ method: "resources/templates/list" })
811-
end
812-
813690
test "#configure_logging_level returns empty hash on success" do
814691
response = @server.handle(
815692
{

0 commit comments

Comments
 (0)