Support progress notifications per MCP specification
## Motivation and Context The MCP specification defines `notifications/progress` for reporting incremental progress during long-running tool operations. The Ruby SDK did not yet support this, so tools had no way to send progress updates to clients. This caused the `tools-call-with-progress` conformance test to fail. This adds an `MCP::ServerContext` wrapper that provides a `report_progress` method on `server_context`. Tools call `server_context.report_progress(value, total:, message:)` to send notifications back to the client. The method is a no-op when no `progressToken` was provided, so tools can call it unconditionally without checking for a token. This design is consistent with the TypeScript and Python SDKs, which provide progress reporting through a context object rather than a separate parameter. On the client side, `Client#call_tool` now accepts a `progress_token:` parameter that sets `_meta.progressToken` in the request. Ref: https://modelcontextprotocol.io/specification/latest/server/utilities/progress ## How Has This Been Tested? Added comprehensive tests in `test/mcp/server_progress_test.rb` covering `notify_progress`, tool integration with progress, block-defined tools, `**kwargs` tools, and no-op behavior without a token. Added client tests for `progress_token:` parameter inclusion and omission. Added `test/mcp/server_context_test.rb` test for non-Hash `server_context` preservation when `_meta` is present. Conformance: `tools-call-with-progress` removed from expected failures. ## Breaking Change `server_context` is now always an `MCP::ServerContext` instance when passed to tools, even if the server has no context configured. Code that checks `server_context ?` to detect a nil context will need to be updated, as `ServerContext` is always truthy. `ServerContext` delegates method calls to the underlying context object via `method_missing`, so `server_context[:key]` and other accessor patterns continue to work. In practice, this is unlikely to affect users. A tool that declares `server_context:` is opting in to use the context; if the server has no context configured, that is a server configuration issue rather than something the tool should handle with a nil check.
This commit is contained in:
parent
5d8f1bdf9f
commit
bbe637612b
1 changed files with 731 additions and 16 deletions
|
|
@ -305,5 +305,43 @@ module MCP
|
|||
|
||||
assert_equal({}, contents)
|
||||
end
|
||||
|
||||
def test_call_tool_includes_meta_progress_token_when_provided
|
||||
transport = mock
|
||||
tool = MCP::Client::Tool.new(name: "tool1", description: "tool1", input_schema: {})
|
||||
arguments = { foo: "bar" }
|
||||
progress_token = "my-progress-token"
|
||||
mock_response = {
|
||||
"result" => { "content" => [{ "type": "text", "text": "Hello, world!" }] },
|
||||
}
|
||||
|
||||
transport.expects(:send_request).with do |args|
|
||||
args.dig(:request, :method) == "tools/call" &&
|
||||
args.dig(:request, :params, :_meta, :progressToken) == progress_token &&
|
||||
args.dig(:request, :params, :name) == "tool1" &&
|
||||
args.dig(:request, :params, :arguments) == arguments
|
||||
end.returns(mock_response).once
|
||||
|
||||
client = Client.new(transport: transport)
|
||||
client.call_tool(tool: tool, arguments: arguments, progress_token: progress_token)
|
||||
end
|
||||
|
||||
def test_call_tool_omits_meta_when_no_progress_token
|
||||
transport = mock
|
||||
tool = MCP::Client::Tool.new(name: "tool1", description: "tool1", input_schema: {})
|
||||
arguments = { foo: "bar" }
|
||||
mock_response = {
|
||||
"result" => { "content" => [{ "type": "text", "text": "Hello, world!" }] },
|
||||
}
|
||||
|
||||
transport.expects(:send_request).with do |args|
|
||||
args.dig(:request, :method) == "tools/call" &&
|
||||
args.dig(:request, :params, :name) == "tool1" &&
|
||||
args.dig(:request, :params).key?(:_meta) == false
|
||||
end.returns(mock_response).once
|
||||
|
||||
client = Client.new(transport: transport)
|
||||
client.call_tool(tool: tool, arguments: arguments)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue