Merge pull request #315 from koic/support_roots
Add `roots/list` and `notifications/roots/list_changed` support
This commit is contained in:
commit
7b5533c809
3 changed files with 324 additions and 8 deletions
|
|
@ -68,10 +68,7 @@ module MCP
|
|||
ensure_capability_does_not_raise_for Methods::NOTIFICATIONS_INITIALIZED
|
||||
|
||||
ensure_capability_raises_error_for Methods::ROOTS_LIST, required_capability_name: "roots"
|
||||
ensure_capability_raises_error_for Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED, required_capability_name: "roots"
|
||||
ensure_capability_raises_error_for Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED,
|
||||
required_capability_name: "roots.listChanged",
|
||||
capabilities: { roots: {} }
|
||||
ensure_capability_does_not_raise_for Methods::NOTIFICATIONS_ROOTS_LIST_CHANGED
|
||||
|
||||
ensure_capability_raises_error_for Methods::SAMPLING_CREATE_MESSAGE, required_capability_name: "sampling"
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,32 @@ module MCP
|
|||
assert_raises(NoMethodError) { server_context.nonexistent_method }
|
||||
end
|
||||
|
||||
test "ServerContext#list_roots delegates to notification_target" do
|
||||
notification_target = mock
|
||||
notification_target.expects(:list_roots).with(
|
||||
related_request_id: nil,
|
||||
).returns({ roots: [{ uri: "file:///project", name: "Project" }] })
|
||||
|
||||
context = mock
|
||||
progress = Progress.new(notification_target: notification_target, progress_token: nil)
|
||||
|
||||
server_context = ServerContext.new(context, progress: progress, notification_target: notification_target)
|
||||
|
||||
result = server_context.list_roots
|
||||
|
||||
assert_equal [{ uri: "file:///project", name: "Project" }], result[:roots]
|
||||
end
|
||||
|
||||
test "ServerContext#list_roots raises NoMethodError when notification_target does not respond" do
|
||||
notification_target = mock
|
||||
context = mock
|
||||
progress = Progress.new(notification_target: notification_target, progress_token: nil)
|
||||
|
||||
server_context = ServerContext.new(context, progress: progress, notification_target: notification_target)
|
||||
|
||||
assert_raises(NoMethodError) { server_context.list_roots }
|
||||
end
|
||||
|
||||
test "ServerContext#create_sampling_message delegates to notification_target over context" do
|
||||
notification_target = mock
|
||||
notification_target.expects(:create_sampling_message).with(
|
||||
|
|
|
|||
202
test/mcp/server_roots_test.rb
Normal file
202
test/mcp/server_roots_test.rb
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
|
||||
module MCP
|
||||
class ServerRootsTest < ActiveSupport::TestCase
|
||||
class MockTransport < Transport
|
||||
attr_reader :requests
|
||||
|
||||
def initialize(server)
|
||||
super
|
||||
@requests = []
|
||||
end
|
||||
|
||||
def send_request(method, params = nil)
|
||||
@requests << { method: method, params: params }
|
||||
{
|
||||
roots: [
|
||||
{ uri: "file:///home/user/projects/myproject", name: "My Project" },
|
||||
],
|
||||
}
|
||||
end
|
||||
|
||||
def send_response(response); end
|
||||
def send_notification(method, params = nil); end
|
||||
def open; end
|
||||
def close; end
|
||||
end
|
||||
|
||||
setup do
|
||||
@server = Server.new(
|
||||
name: "test_server",
|
||||
version: "1.0.0",
|
||||
)
|
||||
|
||||
@mock_transport = MockTransport.new(@server)
|
||||
|
||||
@server.handle({
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 1,
|
||||
params: {
|
||||
protocolVersion: "2025-11-25",
|
||||
capabilities: { roots: { listChanged: true } },
|
||||
clientInfo: { name: "test-client", version: "1.0" },
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
test "roots_list_changed_handler registers callback invoked when notification received" do
|
||||
callback_called = false
|
||||
|
||||
@server.roots_list_changed_handler do |_params|
|
||||
callback_called = true
|
||||
end
|
||||
|
||||
@server.handle({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/roots/list_changed",
|
||||
})
|
||||
|
||||
assert callback_called
|
||||
end
|
||||
|
||||
test "notifications/roots/list_changed is handled as no-op by default" do
|
||||
result = @server.handle({
|
||||
jsonrpc: "2.0",
|
||||
method: "notifications/roots/list_changed",
|
||||
})
|
||||
|
||||
assert_nil result
|
||||
end
|
||||
|
||||
test "init stores client capabilities" do
|
||||
server = Server.new(name: "test", version: "1.0")
|
||||
server.handle({
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 1,
|
||||
params: {
|
||||
protocolVersion: "2025-11-25",
|
||||
capabilities: { roots: { listChanged: true } },
|
||||
clientInfo: { name: "test-client", version: "1.0" },
|
||||
},
|
||||
})
|
||||
|
||||
assert_equal({ roots: { listChanged: true } }, server.client_capabilities)
|
||||
end
|
||||
|
||||
test "init stores nil when client sends no capabilities" do
|
||||
server = Server.new(name: "test", version: "1.0")
|
||||
server.handle({
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 1,
|
||||
params: {
|
||||
protocolVersion: "2025-11-25",
|
||||
clientInfo: { name: "test-client", version: "1.0" },
|
||||
},
|
||||
})
|
||||
|
||||
assert_nil server.client_capabilities
|
||||
end
|
||||
|
||||
test "list_roots uses per-session capabilities via ServerSession" do
|
||||
transport = MCP::Server::Transports::StreamableHTTPTransport.new(@server)
|
||||
|
||||
session_with_roots = ServerSession.new(server: @server, transport: transport, session_id: "s1")
|
||||
session_with_roots.store_client_info(client: { name: "capable" }, capabilities: { roots: {} })
|
||||
transport.instance_variable_get(:@sessions)["s1"] = { stream: nil, server_session: session_with_roots }
|
||||
|
||||
error_with_roots = assert_raises(RuntimeError) do
|
||||
session_with_roots.list_roots
|
||||
end
|
||||
assert_equal("No active stream for roots/list request.", error_with_roots.message)
|
||||
|
||||
session_without_roots = ServerSession.new(server: @server, transport: transport, session_id: "s2")
|
||||
session_without_roots.store_client_info(client: { name: "incapable" }, capabilities: {})
|
||||
|
||||
error = assert_raises(RuntimeError) do
|
||||
session_without_roots.list_roots
|
||||
end
|
||||
assert_equal("Client does not support roots.", error.message)
|
||||
end
|
||||
|
||||
test "ServerSession#client_capabilities falls back to server global capabilities" do
|
||||
transport = MCP::Server::Transports::StreamableHTTPTransport.new(@server)
|
||||
|
||||
session = ServerSession.new(server: @server, transport: transport, session_id: "s3")
|
||||
transport.instance_variable_get(:@sessions)["s3"] = { stream: nil, server_session: session }
|
||||
|
||||
error = assert_raises(RuntimeError) do
|
||||
session.list_roots
|
||||
end
|
||||
assert_equal("No active stream for roots/list request.", error.message)
|
||||
end
|
||||
|
||||
test "session init does not overwrite server global client_capabilities" do
|
||||
server = Server.new(name: "test", version: "1.0")
|
||||
MockTransport.new(server)
|
||||
|
||||
server.handle({
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 1,
|
||||
params: {
|
||||
protocolVersion: "2025-11-25",
|
||||
capabilities: { roots: {} },
|
||||
clientInfo: { name: "first-client", version: "1.0" },
|
||||
},
|
||||
})
|
||||
|
||||
assert_equal({ roots: {} }, server.client_capabilities)
|
||||
|
||||
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
|
||||
session = ServerSession.new(server: server, transport: transport, session_id: "s1")
|
||||
|
||||
server.handle(
|
||||
{
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 2,
|
||||
params: {
|
||||
protocolVersion: "2025-11-25",
|
||||
capabilities: {},
|
||||
clientInfo: { name: "second-client", version: "1.0" },
|
||||
},
|
||||
},
|
||||
session: session,
|
||||
)
|
||||
|
||||
assert_equal({ roots: {} }, server.client_capabilities)
|
||||
assert_equal({}, session.client_capabilities)
|
||||
end
|
||||
|
||||
test "ServerSession#list_roots uses session-scoped capabilities from HTTP init" do
|
||||
server = Server.new(name: "test", version: "1.0")
|
||||
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
|
||||
|
||||
session = ServerSession.new(server: server, transport: transport, session_id: "s1")
|
||||
server.handle(
|
||||
{
|
||||
jsonrpc: "2.0",
|
||||
method: "initialize",
|
||||
id: 1,
|
||||
params: {
|
||||
protocolVersion: "2025-11-25",
|
||||
capabilities: { roots: {} },
|
||||
clientInfo: { name: "http-client", version: "1.0" },
|
||||
},
|
||||
},
|
||||
session: session,
|
||||
)
|
||||
|
||||
transport.instance_variable_get(:@sessions)["s1"] = { stream: nil, server_session: session }
|
||||
error = assert_raises(RuntimeError) do
|
||||
session.list_roots
|
||||
end
|
||||
assert_equal("No active stream for roots/list request.", error.message)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue