Remove Server#create_sampling_message direct call

## Motivation and Context

Sampling is a one-to-one request/response interaction between a server and a specific client.
`Server#create_sampling_message` does not scope requests to a specific session, which could
lead to unintended broadcasts on multi-client transports. `ServerSession#create_sampling_message`
should be used instead, as it routes the request to the correct client session.

This aligns with the Python SDK, which also exposes sampling only at the session level.

## How Has This Been Tested?

Existing tests updated to call `ServerSession#create_sampling_message` instead of
`Server#create_sampling_message`. All tests pass (`rake test`), RuboCop is clean,
and conformance tests pass.

## Breaking Changes

`Server#create_sampling_message` is removed without a deprecation warning.
The sampling feature was released recently and the method was not intended
to be the primary API for this feature.
This commit is contained in:
Koichi ITO 2026-04-12 16:46:40 +09:00
commit a235572777

View file

@ -42,21 +42,12 @@ module MCP
@mock_transport = MockTransport.new(@server)
# Simulate client initialization with sampling capability.
@server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
capabilities: { sampling: {} },
clientInfo: { name: "test-client", version: "1.0" },
},
})
@session = ServerSession.new(server: @server, transport: @mock_transport)
@session.store_client_info(client: { name: "test-client" }, capabilities: { sampling: {} })
end
test "create_sampling_message sends request with required params" do
result = @server.create_sampling_message(
result = @session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
)
@ -72,7 +63,7 @@ module MCP
end
test "create_sampling_message sends all optional params" do
@server.create_sampling_message(
@session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
system_prompt: "You are helpful",
@ -94,46 +85,11 @@ module MCP
assert_equal({ key: "value" }, params[:metadata])
end
test "create_sampling_message raises error when transport is not set" do
server_without_transport = Server.new(name: "test", version: "1.0")
# Initialize with sampling capability but no transport.
server_without_transport.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
capabilities: { sampling: {} },
clientInfo: { name: "test-client", version: "1.0" },
},
})
error = assert_raises(RuntimeError) do
server_without_transport.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
)
end
assert_equal("Cannot send sampling request without a transport.", error.message)
end
test "create_sampling_message raises error when client does not support sampling" do
# Re-initialize without sampling capability.
@server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 2,
params: {
protocolVersion: "2025-11-25",
capabilities: {},
clientInfo: { name: "test-client", version: "1.0" },
},
})
@session.store_client_info(client: { name: "test-client" }, capabilities: {})
error = assert_raises(RuntimeError) do
@server.create_sampling_message(
@session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
)
@ -144,7 +100,7 @@ module MCP
test "create_sampling_message raises error when tools used but client lacks sampling.tools" do
error = assert_raises(RuntimeError) do
@server.create_sampling_message(
@session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
tools: [{ name: "test_tool", inputSchema: { type: "object" } }],
@ -156,7 +112,7 @@ module MCP
test "create_sampling_message raises error when tool_choice used alone but client lacks sampling.tools" do
error = assert_raises(RuntimeError) do
@server.create_sampling_message(
@session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
tool_choice: { mode: "auto" },
@ -167,19 +123,9 @@ module MCP
end
test "create_sampling_message allows tools when client has sampling.tools capability" do
# Re-initialize with sampling.tools capability.
@server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 3,
params: {
protocolVersion: "2025-11-25",
capabilities: { sampling: { tools: {} } },
clientInfo: { name: "test-client", version: "1.0" },
},
})
@session.store_client_info(client: { name: "test-client" }, capabilities: { sampling: { tools: {} } })
result = @server.create_sampling_message(
result = @session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
tools: [{ name: "test_tool", inputSchema: { type: "object" } }],
@ -194,56 +140,6 @@ module MCP
assert_equal "Response from LLM", result[:content][:text]
end
test "init with sampling capability allows create_sampling_message" do
server = Server.new(name: "test", version: "1.0")
# Assigns server.transport via Transport#initialize, which create_sampling_message requires.
MockTransport.new(server)
server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
capabilities: { sampling: { tools: {} } },
clientInfo: { name: "test-client", version: "1.0" },
},
})
result = server.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
tools: [{ name: "t", inputSchema: { type: "object" } }],
)
assert_equal "assistant", result[:role]
end
test "init without capabilities rejects create_sampling_message" do
server = Server.new(name: "test", version: "1.0")
# Assigns server.transport via Transport#initialize, which create_sampling_message requires.
MockTransport.new(server)
server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
clientInfo: { name: "test-client", version: "1.0" },
},
})
error = assert_raises(RuntimeError) do
server.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
)
end
assert_equal("Client does not support sampling.", error.message)
end
test "create_sampling_message uses per-session capabilities via ServerSession" do
transport = MCP::Server::Transports::StreamableHTTPTransport.new(@server)
@ -276,11 +172,23 @@ module MCP
test "ServerSession#client_capabilities falls back to server global capabilities" do
transport = MCP::Server::Transports::StreamableHTTPTransport.new(@server)
# Initialize server with sampling capability.
@server.handle({
jsonrpc: "2.0",
method: "initialize",
id: 1,
params: {
protocolVersion: "2025-11-25",
capabilities: { sampling: {} },
clientInfo: { name: "test-client", version: "1.0" },
},
})
# Session without capabilities stored falls back to @server.client_capabilities.
session = ServerSession.new(server: @server, transport: transport, session_id: "s3")
transport.instance_variable_get(:@sessions)["s3"] = { stream: nil, server_session: session }
# Server was initialized with sampling capability in setup, so fallback should pass validation.
# Server was initialized with sampling capability, so fallback should pass validation.
error = assert_raises(RuntimeError) do
session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
@ -292,7 +200,7 @@ module MCP
test "session init does not overwrite server global client_capabilities" do
server = Server.new(name: "test", version: "1.0")
# Assigns server.transport via Transport#initialize, which create_sampling_message requires.
# Assigns server.transport via Transport#initialize.
MockTransport.new(server)
# Non-session init sets global capabilities.
@ -333,59 +241,8 @@ module MCP
assert_equal({}, session.client_capabilities)
end
test "Server#create_sampling_message does not see session-scoped capabilities from HTTP init" do
server = Server.new(name: "test", version: "1.0")
transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
# HTTP init stores capabilities on the session, not on the 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: { sampling: {} },
clientInfo: { name: "http-client", version: "1.0" },
},
},
session: session,
)
# Server-level API should not see session-scoped capabilities.
error = assert_raises(RuntimeError) do
server.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
)
end
assert_equal("Client does not support sampling.", error.message)
# Session-scoped API should work (fails at transport level, not capability).
transport.instance_variable_get(:@sessions)["s1"] = { stream: nil, server_session: session }
error = assert_raises(RuntimeError) do
session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
)
end
assert_equal("No active stream for sampling/createMessage request.", error.message)
end
test "Server#create_sampling_message accepts related_request_id without error" do
@server.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
related_request_id: "req-1",
)
request = @mock_transport.requests.first
assert_equal "sampling/createMessage", request[:method]
end
test "create_sampling_message omits nil optional params" do
@server.create_sampling_message(
@session.create_sampling_message(
messages: [{ role: "user", content: { type: "text", text: "Hello" } }],
max_tokens: 100,
system_prompt: nil,