Skip to content

Commit cc219a2

Browse files
authored
Merge pull request #458 from koic/feature_sampling_client
Support client-side sampling via `MCP::Client#on_sampling`
2 parents 7ebaf98 + f3d6da2 commit cc219a2

4 files changed

Lines changed: 156 additions & 0 deletions

File tree

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,44 @@ since servers deliver requests that are not tied to a client request on that str
23462346
a JSON-RPC `-32601` (method not found) error. To handle methods other than `elicitation/create`, register directly on the transport with
23472347
`http_transport.on_server_request("method/name") { |params| ... }`.
23482348

2349+
#### Server-to-Client Requests (Sampling)
2350+
2351+
Servers can also request an LLM completion from the client with [`sampling/createMessage`](https://modelcontextprotocol.io/specification/2025-11-25/client/sampling),
2352+
letting a server leverage the client's model access without its own API keys.
2353+
2354+
> MCP Sampling is deprecated as of protocol version `2026-07-28` (SEP-2577), while remaining fully supported under `2025-11-25`.
2355+
> Register this handler to interoperate with servers that still send sampling requests during the deprecation window;
2356+
> new servers should call LLM provider APIs directly.
2357+
2358+
Register a handler and advertise the capability on `connect`:
2359+
2360+
```ruby
2361+
client.connect(capabilities: { sampling: {} })
2362+
2363+
client.on_sampling do |params|
2364+
completion = my_llm.complete(params["messages"], max_tokens: params["maxTokens"])
2365+
{
2366+
role: "assistant",
2367+
content: { type: "text", text: completion.text },
2368+
model: completion.model,
2369+
stopReason: "endTurn",
2370+
}
2371+
end
2372+
```
2373+
2374+
For trust and safety, the spec recommends a human in the loop able to review, edit, or reject the request and the generated response.
2375+
To reject a request, raise `MCP::Client::ServerRequestError` with the spec's user-rejection code `-1`:
2376+
2377+
```ruby
2378+
client.on_sampling do |params|
2379+
raise MCP::Client::ServerRequestError.new("User rejected sampling request", code: -1) unless approved?(params)
2380+
2381+
generate_completion(params)
2382+
end
2383+
```
2384+
2385+
Use `capabilities: { sampling: { tools: {} } }` to receive tool-enabled sampling requests. Like elicitation, this uses the same standalone GET SSE listening stream.
2386+
23492387
#### HTTP Authorization
23502388

23512389
By default, the HTTP transport layer provides no authentication to the server, but you can provide custom headers if you need authentication. For example, to use Bearer token authentication:

lib/mcp/client.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,44 @@ def on_elicitation(&handler)
423423
transport.on_server_request(Methods::ELICITATION_CREATE, &handler)
424424
end
425425

426+
# Registers a handler for `sampling/createMessage` requests the server sends while one of this client's requests is in flight.
427+
# The handler receives the request `params` (`messages`, `maxTokens`, optionally `systemPrompt`, `modelPreferences`, `tools`,
428+
# `toolChoice`, ...; string keys) and must return a `CreateMessageResult`-shaped Hash:
429+
# `{ role: "assistant", content: { type: "text", text: "..." }, model: "...", stopReason: "..." }`.
430+
#
431+
# For trust and safety, the spec recommends a human in the loop able to review, edit, or reject the request and the generated response
432+
# before it is returned to the server. To reject, raise `ServerRequestError` with the spec's user-rejection code `-1`.
433+
#
434+
# Requires a transport that supports server-to-client requests (e.g. `MCP::Client::HTTP`); pass `capabilities: { sampling: {} }` to
435+
# `connect` (or `{ sampling: { tools: {} } }` to receive tool-enabled sampling requests) so the server knows it may send them.
436+
#
437+
# @example Forward the request to an LLM and return its completion
438+
#
439+
# client.on_sampling do |params|
440+
# raise MCP::Client::ServerRequestError.new("User rejected sampling request", code: -1) unless approved?(params)
441+
#
442+
# completion = my_llm.complete(params["messages"], max_tokens: params["maxTokens"])
443+
# {
444+
# role: "assistant",
445+
# content: { type: "text", text: completion.text },
446+
# model: completion.model,
447+
# stopReason: "endTurn",
448+
# }
449+
# end
450+
#
451+
# https://modelcontextprotocol.io/specification/2025-11-25/client/sampling
452+
#
453+
# @deprecated MCP Sampling (`sampling/createMessage`) is deprecated as of MCP protocol version 2026-07-28 (SEP-2577).
454+
# Register this handler only to interoperate with servers that still send sampling requests during the deprecation window;
455+
# new servers should call LLM provider APIs directly.
456+
def on_sampling(&handler)
457+
unless transport.respond_to?(:on_server_request)
458+
raise ArgumentError, "The transport does not support server-to-client requests"
459+
end
460+
461+
transport.on_server_request(Methods::SAMPLING_CREATE_MESSAGE, &handler)
462+
end
463+
426464
# Sends a `ping` request to the server to verify the connection is alive.
427465
# Per the MCP spec, the server responds with an empty result.
428466
#

test/mcp/client/http_test.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,67 @@ def test_send_request_dispatches_server_request_to_registered_handler
10201020
assert_equal("Please accept with defaults", received_params["message"])
10211021
end
10221022

1023+
def test_send_request_dispatches_sampling_request_to_registered_handler
1024+
request = {
1025+
jsonrpc: "2.0",
1026+
id: "test_id",
1027+
method: "tools/call",
1028+
params: { name: "ask_llm", arguments: {} },
1029+
}
1030+
1031+
sampling_request = {
1032+
jsonrpc: "2.0",
1033+
id: 0,
1034+
method: "sampling/createMessage",
1035+
params: {
1036+
messages: [{ role: "user", content: { type: "text", text: "Hi" } }],
1037+
maxTokens: 100,
1038+
},
1039+
}
1040+
tool_result = { jsonrpc: "2.0", id: "test_id", result: { content: [] } }
1041+
sse_body = "event: message\ndata: #{sampling_request.to_json}\n\n" \
1042+
"event: message\ndata: #{tool_result.to_json}\n\n"
1043+
1044+
stub_request(:post, url).with(
1045+
body: request.to_json,
1046+
).to_return(
1047+
status: 200,
1048+
headers: { "Content-Type" => "text/event-stream" },
1049+
body: sse_body,
1050+
)
1051+
1052+
expected_response = {
1053+
jsonrpc: "2.0",
1054+
id: 0,
1055+
result: {
1056+
role: "assistant",
1057+
content: { type: "text", text: "Hello there" },
1058+
model: "test-model",
1059+
stopReason: "endTurn",
1060+
},
1061+
}
1062+
response_stub = stub_request(:post, url).with(
1063+
body: expected_response.to_json,
1064+
).to_return(status: 202, body: "")
1065+
1066+
received_params = nil
1067+
client.on_server_request("sampling/createMessage") do |params|
1068+
received_params = params
1069+
{
1070+
role: "assistant",
1071+
content: { type: "text", text: "Hello there" },
1072+
model: "test-model",
1073+
stopReason: "endTurn",
1074+
}
1075+
end
1076+
1077+
response = client.send_request(request: request)
1078+
1079+
assert_equal({ "content" => [] }, response["result"])
1080+
assert_requested(response_stub)
1081+
assert_equal(100, received_params["maxTokens"])
1082+
end
1083+
10231084
def test_send_request_answers_unregistered_server_request_with_method_not_found
10241085
request = {
10251086
jsonrpc: "2.0",

test/mcp/client_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ def test_on_elicitation_raises_when_transport_does_not_support_server_requests
6666
assert_includes(error.message, "does not support server-to-client requests")
6767
end
6868

69+
def test_on_sampling_registers_handler_on_transport
70+
transport = mock
71+
handler = proc { { role: "assistant", content: { type: "text", text: "hi" } } }
72+
transport.expects(:on_server_request).with("sampling/createMessage")
73+
74+
Client.new(transport: transport).on_sampling(&handler)
75+
end
76+
77+
def test_on_sampling_raises_when_transport_does_not_support_server_requests
78+
transport = mock
79+
transport.stubs(:respond_to?).with(:on_server_request).returns(false)
80+
81+
error = assert_raises(ArgumentError) do
82+
Client.new(transport: transport).on_sampling { { role: "assistant", content: { type: "text", text: "hi" } } }
83+
end
84+
85+
assert_includes(error.message, "does not support server-to-client requests")
86+
end
87+
6988
def test_connected_delegates_to_transport_when_supported
7089
transport = mock
7190
transport.expects(:connected?).returns(true)

0 commit comments

Comments
 (0)