ruby-sdk/test/mcp/server_progress_test.rb
Koichi ITO b89f6e315d Remove Server#notify_progress broadcast API
## Motivation and Context

`Server#notify_progress` broadcasts progress notifications to all connected clients.
Progress notifications are tied to a specific request's `progressToken` and have no
meaningful broadcast use case. Neither the Python SDK nor the TypeScript SDK provides
a server-level progress broadcast capability.

The MCP specification requires that progress notifications only reference tokens
provided in an active request:

> Progress notifications MUST only reference tokens that:
> - Were provided in an active request
> - Are associated with an in-progress operation

Ref: https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/progress

Broadcasting progress to all clients does not comply with this requirement,
since clients that did not originate the request have no matching `progressToken`.

The introduction of `ServerSession` for per-connection state (#275) made
this removal possible: `Progress` and `ServerContext` now use `notification_target:`
(a `ServerSession`) to route notifications to the originating session only.

### Changes

- Removed `Server#notify_progress` entirely. The method is no longer needed as
  `notification_target` in `call_tool_with_args` now passes `session` directly
  (nil when no session is available, in which case `Progress#report` and
  `ServerContext#notify_log_message` are no-ops).
- Removed `ServerContext#notify_progress` as well. Progress notifications should
  only be sent via `server_context.report_progress`, which enforces the correct
  `progressToken` from the originating request.
- Added nil guards to `Progress#report` and `ServerContext#notify_log_message`
  for when no session is available.
- Rewrote progress tests to use `ServerSession#handle` instead of
  `Server#handle`, reflecting that progress notifications are always session-scoped.
- Removed 4 tests that called `server.notify_progress` directly as a public broadcast API.
- Removed `notify_progress` from the README.md notification methods list.
- Removed the "Server-Side: Direct `notify_progress` Usage" section from the README.md.

Progress notifications should be sent via `server_context.report_progress` inside tool handlers,
which automatically scopes them to the originating client session.

## Breaking Changes

Progress notifications are scoped to a specific request via `progressToken` per the MCP specification,
so the broadcast behavior of `Server#notify_progress` was a spec violation.
This is treated as a bug fix and is made without a deprecation period.
This aligns the Ruby SDK with the Python and TypeScript SDKs, neither of which provides
a server-level progress broadcast API.

`Server#notify_progress` and `ServerContext#notify_progress` are no longer available.
Users should use `server_context.report_progress` inside tool handlers instead, which
provides session-scoped delivery with the correct `progressToken`.

This feature was only recently introduced in mcp 0.9.0:
https://github.com/modelcontextprotocol/ruby-sdk/releases/tag/v0.9.0

So an early release should help limit its impact on adoption.
2026-03-29 15:11:28 +09:00

318 lines
10 KiB
Ruby

# frozen_string_literal: true
require "test_helper"
module MCP
class ServerProgressTest < ActiveSupport::TestCase
class MockTransport < Transport
attr_reader :notifications
def initialize(server)
super
@notifications = []
end
def send_notification(method, params = nil)
@notifications << { method: method, params: params }
true
end
def send_response(response); end
def open; end
def close; end
def handle_request(request); end
end
# Tool without progress parameter.
class SimpleToolWithoutProgress < Tool
tool_name "simple_without_progress"
description "A tool that doesn't use progress"
input_schema(properties: { message: { type: "string" } }, required: ["message"])
class << self
def call(message:)
Tool::Response.new([{ type: "text", text: "SimpleToolWithoutProgress: #{message}" }])
end
end
end
# Tool with progress via server_context.
class ToolWithProgress < Tool
tool_name "tool_with_progress"
description "A tool that uses progress"
input_schema(properties: { message: { type: "string" } }, required: ["message"])
class << self
def call(message:, server_context:)
server_context.report_progress(50, total: 100, message: "halfway")
server_context.report_progress(100, total: 100, message: "done")
Tool::Response.new([{ type: "text", text: "ToolWithProgress: #{message}" }])
end
end
end
# Tool with server_context accessing both context data and progress.
class ToolWithContextAndProgress < Tool
tool_name "tool_with_context_and_progress"
description "A tool that uses both server_context and progress"
input_schema(properties: { message: { type: "string" } }, required: ["message"])
class << self
def call(message:, server_context:)
server_context.report_progress(100)
context_info = server_context[:user] || "none"
Tool::Response.new([{ type: "text", text: "ToolWithContextAndProgress: #{message} context=#{context_info}" }])
end
end
end
# Tool with **kwargs.
class ToolWithKwargs < Tool
tool_name "tool_with_kwargs"
description "A tool that uses **kwargs"
class << self
def call(**kwargs)
context = kwargs[:server_context]
context.report_progress(75)
Tool::Response.new([{ type: "text", text: "ToolWithKwargs: progress=#{context ? "present" : "absent"}" }])
end
end
end
setup do
@server = Server.new(
name: "test_server",
version: "1.0.0",
tools: [SimpleToolWithoutProgress, ToolWithProgress, ToolWithContextAndProgress, ToolWithKwargs],
server_context: { user: "test_user" },
)
@mock_transport = MockTransport.new(@server)
@server.transport = @mock_transport
@session = ServerSession.new(server: @server, transport: @mock_transport)
end
test "tool with progress parameter receives Progress instance and sends notifications via _meta.progressToken" do
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "tool_with_progress",
arguments: { message: "Hello" },
_meta: { progressToken: "token-abc" },
},
}
response = @session.handle(request)
assert response[:result]
assert_equal "ToolWithProgress: Hello", response[:result][:content][0][:text]
assert_equal 2, @mock_transport.notifications.size
first = @mock_transport.notifications[0]
assert_equal Methods::NOTIFICATIONS_PROGRESS, first[:method]
assert_equal "token-abc", first[:params]["progressToken"]
assert_equal 50, first[:params]["progress"]
assert_equal 100, first[:params]["total"]
assert_equal "halfway", first[:params]["message"]
second = @mock_transport.notifications[1]
assert_equal Methods::NOTIFICATIONS_PROGRESS, second[:method]
assert_equal "token-abc", second[:params]["progressToken"]
assert_equal 100, second[:params]["progress"]
assert_equal 100, second[:params]["total"]
assert_equal "done", second[:params]["message"]
end
test "tool without progress parameter works normally" do
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "simple_without_progress",
arguments: { message: "Hello" },
},
}
response = @session.handle(request)
assert response[:result]
assert_equal "SimpleToolWithoutProgress: Hello", response[:result][:content][0][:text]
assert_equal 0, @mock_transport.notifications.size
end
test "server_context.report_progress is a no-op when no progressToken in request" do
received_context = :unset
tool_class = Class.new(Tool) do
tool_name "progress_nil_tool"
define_singleton_method(:call) do |server_context:|
received_context = server_context
Tool::Response.new([{ type: "text", text: "done" }])
end
end
server = Server.new(
name: "test_server",
tools: [tool_class],
server_context: { user: "test" },
)
server.transport = @mock_transport
session = ServerSession.new(server: server, transport: @mock_transport)
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "progress_nil_tool",
arguments: {},
},
}
session.handle(request)
assert_instance_of ServerContext, received_context
assert_nothing_raised { received_context.report_progress(50) }
assert_equal 0, @mock_transport.notifications.size
end
test "tool with both server_context and progress receives both" do
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "tool_with_context_and_progress",
arguments: { message: "Hello" },
_meta: { progressToken: "token-xyz" },
},
}
response = @session.handle(request)
assert response[:result]
assert_equal "ToolWithContextAndProgress: Hello context=test_user", response[:result][:content][0][:text]
assert_equal 1, @mock_transport.notifications.size
notification = @mock_transport.notifications.first
assert_equal Methods::NOTIFICATIONS_PROGRESS, notification[:method]
assert_equal "token-xyz", notification[:params]["progressToken"]
assert_equal 100, notification[:params]["progress"]
end
test "tool with **kwargs receives Progress instance" do
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "tool_with_kwargs",
arguments: {},
_meta: { progressToken: "token-kwargs" },
},
}
response = @session.handle(request)
assert response[:result]
assert_equal "ToolWithKwargs: progress=present", response[:result][:content][0][:text]
assert_equal 1, @mock_transport.notifications.size
notification = @mock_transport.notifications.first
assert_equal Methods::NOTIFICATIONS_PROGRESS, notification[:method]
assert_equal "token-kwargs", notification[:params]["progressToken"]
assert_equal 75, notification[:params]["progress"]
end
test "block-defined tool with progress via server_context works" do
server = Server.new(name: "test_server")
server.transport = @mock_transport
server.define_tool(name: "block_tool") do |server_context:|
server_context.report_progress(42, total: 100)
Tool::Response.new([{ type: "text", text: "block_tool done" }])
end
session = ServerSession.new(server: server, transport: @mock_transport)
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "block_tool",
arguments: {},
_meta: { progressToken: "block-token" },
},
}
response = session.handle(request)
assert response[:result]
assert_equal "block_tool done", response[:result][:content][0][:text]
assert_equal 1, @mock_transport.notifications.size
notification = @mock_transport.notifications.first
assert_equal Methods::NOTIFICATIONS_PROGRESS, notification[:method]
assert_equal "block-token", notification[:params]["progressToken"]
assert_equal 42, notification[:params]["progress"]
assert_equal 100, notification[:params]["total"]
end
test "multiple progress notifications during tool execution" do
server = Server.new(name: "test_server")
server.transport = @mock_transport
server.define_tool(name: "multi_progress_tool") do |server_context:|
(1..5).each do |i|
server_context.report_progress(i * 20, total: 100)
end
Tool::Response.new([{ type: "text", text: "done" }])
end
session = ServerSession.new(server: server, transport: @mock_transport)
request = {
jsonrpc: "2.0",
id: 1,
method: "tools/call",
params: {
name: "multi_progress_tool",
arguments: {},
_meta: { progressToken: "multi-token" },
},
}
session.handle(request)
assert_equal 5, @mock_transport.notifications.size
@mock_transport.notifications.each_with_index do |n, i|
assert_equal Methods::NOTIFICATIONS_PROGRESS, n[:method]
assert_equal "multi-token", n[:params]["progressToken"]
assert_equal (i + 1) * 20, n[:params]["progress"]
assert_equal 100, n[:params]["total"]
end
end
test "incoming notifications/progress is handled as no-op" do
request = {
jsonrpc: "2.0",
method: "notifications/progress",
params: {
progressToken: "token-1",
progress: 50,
},
}
# Should not raise and should return nil (notification, no id).
result = @session.handle(request)
assert_nil result
end
end
end