Merge pull request #503 from koic/reject_modern_lifecycle_server_requests

Refuse server-to-client requests in the modern lifecycle per SEP-2575
This commit is contained in:
Koichi ITO 2026-08-13 23:27:36 +09:00 • committed by GitHub
commit cfd9a7b0ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View file

@ -479,6 +479,16 @@ module MCP
# so a client that never answers cannot park the calling thread for good. On expiry the peer is
# sent `notifications/cancelled` and `MCP::Server::RequestTimeoutError` is raised.
def send_request(method, params = nil, session_id: nil, related_request_id: nil, parent_cancellation: nil, server_session: nil, timeout: nil)
# The modern lifecycle (SEP-2575) forbids server-initiated JSON-RPC requests;
# multi round-trip `input_required` results (SEP-2322) replace them. A modern session has never reached
# the rest of this method anyway, since `handle_modern` mints its session without registering it in `@sessions`,
# but that is incidental: without the rule stated here a modern handler that reaches for elicitation
# or sampling is told "Session not found: <uuid>", which points at everything except the actual reason.
# `StdioTransport#send_request` refuses the same way.
if server_session&.era == :modern
raise "Server-initiated requests are not available in the modern lifecycle (SEP-2575)."
end
if @stateless
raise "Stateless mode does not support server-to-client requests."
end

View file

@ -2939,6 +2939,18 @@ module MCP
assert_equal("Stateless mode does not support server-to-client requests.", error.message)
end
test "send_request refuses to run at all in the modern lifecycle" do
# SEP-2575 removes server-initiated requests, so a modern handler reaching for elicitation or sampling
# is told why rather than being sent down a path that fails on an unrelated session lookup.
modern_session = ServerSession.new(server: @server, transport: @transport, session_id: "modern-1", era: :modern)
error = assert_raises(RuntimeError) do
@transport.send_request("elicitation/create", { message: "hi" }, session_id: "modern-1", server_session: modern_session)
end
assert_equal("Server-initiated requests are not available in the modern lifecycle (SEP-2575).", error.message)
end
test "send_request raises error when session_id is not provided" do
error = assert_raises(RuntimeError) do
@transport.send_request("sampling/createMessage", { "messages" => [] })