Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/json_rpc_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def process_request(request, id_validation_pattern:, &method_finder)
end

begin
method = method_finder.call(method_name)
method = method_finder.call(method_name, id)

if method.nil?
return error_response(id: id, id_validation_pattern: id_validation_pattern, error: {
Expand Down
4 changes: 3 additions & 1 deletion lib/mcp/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module MCP
class Progress
def initialize(notification_target:, progress_token:)
def initialize(notification_target:, progress_token:, related_request_id: nil)
@notification_target = notification_target
@progress_token = progress_token
@related_request_id = related_request_id
end

def report(progress, total: nil, message: nil)
Expand All @@ -16,6 +17,7 @@ def report(progress, total: nil, message: nil)
progress: progress,
total: total,
message: message,
related_request_id: @related_request_id,
)
end
end
Expand Down
25 changes: 13 additions & 12 deletions lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ def initialize(
# When `nil`, progress and logging notifications from tool handlers are silently skipped.
# @return [Hash, nil] The JSON-RPC response, or `nil` for notifications.
def handle(request, session: nil)
JsonRpcHandler.handle(request) do |method|
handle_request(request, method, session: session)
JsonRpcHandler.handle(request) do |method, request_id|
handle_request(request, method, session: session, related_request_id: request_id)
end
end

Expand All @@ -140,8 +140,8 @@ def handle(request, session: nil)
# When `nil`, progress and logging notifications from tool handlers are silently skipped.
# @return [String, nil] The JSON-RPC response as JSON, or `nil` for notifications.
def handle_json(request, session: nil)
JsonRpcHandler.handle_json(request) do |method|
handle_request(request, method, session: session)
JsonRpcHandler.handle_json(request) do |method, request_id|
handle_request(request, method, session: session, related_request_id: request_id)
end
end

Expand Down Expand Up @@ -220,7 +220,8 @@ def create_sampling_message(
stop_sequences: nil,
metadata: nil,
tools: nil,
tool_choice: nil
tool_choice: nil,
related_request_id: nil
)
unless @transport
raise "Cannot send sampling request without a transport."
Expand Down Expand Up @@ -371,7 +372,7 @@ def schema_contains_ref?(schema)
end
end

def handle_request(request, method, session: nil)
def handle_request(request, method, session: nil, related_request_id: nil)
handler = @handlers[method]
unless handler
instrument_call("unsupported_method") do
Expand Down Expand Up @@ -399,7 +400,7 @@ def handle_request(request, method, session: nil)
when Methods::RESOURCES_TEMPLATES_LIST
{ resourceTemplates: @handlers[Methods::RESOURCES_TEMPLATES_LIST].call(params) }
when Methods::TOOLS_CALL
call_tool(params, session: session)
call_tool(params, session: session, related_request_id: related_request_id)
when Methods::COMPLETION_COMPLETE
complete(params)
when Methods::LOGGING_SET_LEVEL
Expand Down Expand Up @@ -499,7 +500,7 @@ def list_tools(request)
@tools.values.map(&:to_h)
end

def call_tool(request, session: nil)
def call_tool(request, session: nil, related_request_id: nil)
tool_name = request[:name]

tool = tools[tool_name]
Expand Down Expand Up @@ -531,7 +532,7 @@ def call_tool(request, session: nil)

progress_token = request.dig(:_meta, :progressToken)

call_tool_with_args(tool, arguments, server_context_with_meta(request), progress_token: progress_token, session: session)
call_tool_with_args(tool, arguments, server_context_with_meta(request), progress_token: progress_token, session: session, related_request_id: related_request_id)
rescue RequestHandlerError
raise
rescue => e
Expand Down Expand Up @@ -611,12 +612,12 @@ def accepts_server_context?(method_object)
parameters.any? { |type, name| type == :keyrest || name == :server_context }
end

def call_tool_with_args(tool, arguments, context, progress_token: nil, session: nil)
def call_tool_with_args(tool, arguments, context, progress_token: nil, session: nil, related_request_id: nil)
args = arguments&.transform_keys(&:to_sym) || {}

if accepts_server_context?(tool.method(:call))
progress = Progress.new(notification_target: session, progress_token: progress_token)
server_context = ServerContext.new(context, progress: progress, notification_target: session)
progress = Progress.new(notification_target: session, progress_token: progress_token, related_request_id: related_request_id)
server_context = ServerContext.new(context, progress: progress, notification_target: session, related_request_id: related_request_id)
tool.call(**args, server_context: server_context).to_h
else
tool.call(**args).to_h
Expand Down
Loading