Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 6 additions & 24 deletions lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,16 @@ def notify_log_message(data:, level:, logger: nil)
report_exception(e, { notification: "log_message" })
end

def resources_list_handler(&block)
@handlers[Methods::RESOURCES_LIST] = block
end

# Sets a custom handler for `resources/read` requests.
# The block receives the parsed request params and should return resource
# contents. The return value is set as the `contents` field of the response.
#
# @yield [params] The request params containing `:uri`.
# @yieldreturn [Array<Hash>, Hash] Resource contents.
def resources_read_handler(&block)
@handlers[Methods::RESOURCES_READ] = block
end

def resources_templates_list_handler(&block)
@handlers[Methods::RESOURCES_TEMPLATES_LIST] = block
end

def tools_list_handler(&block)
@handlers[Methods::TOOLS_LIST] = block
end

def tools_call_handler(&block)
@handlers[Methods::TOOLS_CALL] = block
end

def prompts_list_handler(&block)
@handlers[Methods::PROMPTS_LIST] = block
end

def prompts_get_handler(&block)
@handlers[Methods::PROMPTS_GET] = block
end

private

def validate!
Expand Down
123 changes: 0 additions & 123 deletions test/mcp/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,23 +270,6 @@ class ServerTest < ActiveSupport::TestCase
assert_equal({ foo: "bar" }, result[:tools][0][:_meta])
end

test "#tools_list_handler sets the tools/list handler" do
@server.tools_list_handler do
[{ name: "hammer", description: "Hammer time!" }]
end

request = {
jsonrpc: "2.0",
method: "tools/list",
id: 1,
}

response = @server.handle(request)
result = response[:result]
assert_equal({ tools: [{ name: "hammer", description: "Hammer time!" }] }, result)
assert_instrumentation_data({ method: "tools/list" })
end

test "#handle tools/call executes tool and returns result" do
tool_name = "test_tool"
tool_args = { arg: "value" }
Expand Down Expand Up @@ -564,24 +547,6 @@ class Example < Tool
assert_includes response[:error][:data], "Tool not found: unknown_tool"
end

test "#tools_call_handler sets the tools/call handler" do
@server.tools_call_handler do |request|
tool_name = request[:name]
Tool::Response.new("#{tool_name} called successfully").to_h
end

request = {
jsonrpc: "2.0",
method: "tools/call",
params: { name: "my_tool", arguments: {} },
id: 1,
}

response = @server.handle(request)
assert_equal({ content: "my_tool called successfully", isError: false }, response[:result])
assert_instrumentation_data({ method: "tools/call" })
end

test "#handle prompts/list returns list of prompts" do
request = {
jsonrpc: "2.0",
Expand All @@ -594,22 +559,6 @@ class Example < Tool
assert_instrumentation_data({ method: "prompts/list" })
end

test "#prompts_list_handler sets the prompts/list handler" do
@server.prompts_list_handler do
[{ name: "foo_prompt", description: "Foo prompt" }]
end

request = {
jsonrpc: "2.0",
method: "prompts/list",
id: 1,
}

response = @server.handle(request)
assert_equal({ prompts: [{ name: "foo_prompt", description: "Foo prompt" }] }, response[:result])
assert_instrumentation_data({ method: "prompts/list" })
end

test "#handle prompts/get returns templated prompt" do
request = {
jsonrpc: "2.0",
Expand Down Expand Up @@ -669,32 +618,6 @@ class Example < Tool
})
end

test "#prompts_get_handler sets the prompts/get handler" do
@server.prompts_get_handler do |request|
prompt_name = request[:name]
Prompt::Result.new(
description: prompt_name,
messages: [
Prompt::Message.new(role: "user", content: Content::Text.new(request[:arguments]["foo"])),
],
).to_h
end

request = {
jsonrpc: "2.0",
method: "prompts/get",
id: 1,
params: { name: "foo_bar_prompt", arguments: { "foo" => "bar" } },
}

response = @server.handle(request)
assert_equal(
{ description: "foo_bar_prompt", messages: [{ role: "user", content: { type: "text", text: "bar" } }] },
response[:result],
)
assert_instrumentation_data({ method: "prompts/get" })
end

test "#handle resources/list returns a list of resources" do
request = {
jsonrpc: "2.0",
Expand All @@ -707,25 +630,6 @@ class Example < Tool
assert_instrumentation_data({ method: "resources/list" })
end

test "#resources_list_handler sets the resources/list handler" do
@server.resources_list_handler do
[{ uri: "https://test_resource.invalid", name: "test-resource", title: "Test Resource", description: "Test resource" }]
end

request = {
jsonrpc: "2.0",
method: "resources/list",
id: 1,
}

response = @server.handle(request)
assert_equal(
{ resources: [{ uri: "https://test_resource.invalid", name: "test-resource", title: "Test Resource", description: "Test resource" }] },
response[:result],
)
assert_instrumentation_data({ method: "resources/list" })
end

test "#handle resources/read returns an empty array of contents by default" do
request = {
jsonrpc: "2.0",
Expand Down Expand Up @@ -783,33 +687,6 @@ class Example < Tool
assert_instrumentation_data({ method: "resources/templates/list" })
end

test "#resources_templates_list_handler sets the resources/templates/list handler" do
@server.resources_templates_list_handler do
[{ uriTemplate: "test_resource_template/{id}", name: "Test resource template", description: "a template" }]
end

request = {
jsonrpc: "2.0",
method: "resources/templates/list",
id: 1,
}

response = @server.handle(request)
assert_equal(
{
resourceTemplates: [
{
uriTemplate: "test_resource_template/{id}",
name: "Test resource template",
description: "a template",
},
],
},
response[:result],
)
assert_instrumentation_data({ method: "resources/templates/list" })
end

test "#configure_logging_level returns empty hash on success" do
response = @server.handle(
{
Expand Down