Merge pull request #493 from seunghan91/expose-server-context-in-instrumentation

Expose the user-defined server_context in instrumentation data
This commit is contained in:
Koichi ITO 2026-08-09 01:10:36 +09:00 • committed by GitHub
commit fee2710955
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 158 additions and 3 deletions

View file

@ -33,7 +33,7 @@ module MCP
attr_writer :instrumentation_callback
def initialize(exception_reporter: nil, around_request: nil, instrumentation_callback: nil, protocol_version: nil,
validate_tool_call_arguments: true, validate_tool_call_results: false)
validate_tool_call_arguments: true, validate_tool_call_results: false, instrument_server_context: false)
@exception_reporter = exception_reporter
@around_request = around_request
@instrumentation_callback = instrumentation_callback
@ -43,9 +43,11 @@ module MCP
end
validate_value_of_validate_tool_call_arguments!(validate_tool_call_arguments)
validate_value_of_validate_tool_call_results!(validate_tool_call_results)
validate_value_of_instrument_server_context!(instrument_server_context)
@validate_tool_call_arguments = validate_tool_call_arguments
@validate_tool_call_results = validate_tool_call_results
@instrument_server_context = instrument_server_context
end
def protocol_version=(protocol_version)
@ -60,6 +62,16 @@ module MCP
@validate_tool_call_arguments = validate_tool_call_arguments
end
# Opt in to exposing the user-defined `server_context` in the
# `around_request` / `instrumentation_callback` data hash. Off by default:
# the hash is application-supplied and may hold values a tracing backend
# should not receive, so surfacing it has to be a deliberate choice.
def instrument_server_context=(instrument_server_context)
validate_value_of_instrument_server_context!(instrument_server_context)
@instrument_server_context = instrument_server_context
end
def validate_tool_call_results=(validate_tool_call_results)
validate_value_of_validate_tool_call_results!(validate_tool_call_results)
@ -107,6 +119,10 @@ module MCP
attr_reader :validate_tool_call_arguments
attr_reader :validate_tool_call_results
def instrument_server_context?
!!@instrument_server_context
end
def validate_tool_call_arguments?
!!@validate_tool_call_arguments
end
@ -152,6 +168,7 @@ module MCP
protocol_version: protocol_version,
validate_tool_call_arguments: validate_tool_call_arguments,
validate_tool_call_results: validate_tool_call_results,
instrument_server_context: other.instrument_server_context?,
)
end
@ -176,6 +193,12 @@ module MCP
end
end
def validate_value_of_instrument_server_context!(instrument_server_context)
unless instrument_server_context.is_a?(TrueClass) || instrument_server_context.is_a?(FalseClass)
raise ArgumentError, "instrument_server_context must be a boolean"
end
end
def default_exception_reporter
@default_exception_reporter ||= ->(exception, server_context) {}
end

View file

@ -7,6 +7,12 @@ module MCP
begin
@instrumentation_data = {}
add_instrumentation_data(method: method)
# `self.` is required: the `server_context:` keyword above shadows the
# reader, and the value we want here is the user-defined hash passed to
# `Server.new`, not the per-call reporter context.
if configuration.instrument_server_context? && respond_to?(:server_context)
add_instrumentation_data(server_context: self.server_context)
end
result = configuration.around_request.call(@instrumentation_data, &block)