@@ -33,7 +33,7 @@ def modern_protocol_version?(version)
3333 attr_writer :instrumentation_callback
3434
3535 def initialize ( exception_reporter : nil , around_request : nil , instrumentation_callback : nil , protocol_version : nil ,
36- validate_tool_call_arguments : true , validate_tool_call_results : false )
36+ validate_tool_call_arguments : true , validate_tool_call_results : false , instrument_server_context : false )
3737 @exception_reporter = exception_reporter
3838 @around_request = around_request
3939 @instrumentation_callback = instrumentation_callback
@@ -43,9 +43,11 @@ def initialize(exception_reporter: nil, around_request: nil, instrumentation_cal
4343 end
4444 validate_value_of_validate_tool_call_arguments! ( validate_tool_call_arguments )
4545 validate_value_of_validate_tool_call_results! ( validate_tool_call_results )
46+ validate_value_of_instrument_server_context! ( instrument_server_context )
4647
4748 @validate_tool_call_arguments = validate_tool_call_arguments
4849 @validate_tool_call_results = validate_tool_call_results
50+ @instrument_server_context = instrument_server_context
4951 end
5052
5153 def protocol_version = ( protocol_version )
@@ -60,6 +62,16 @@ def validate_tool_call_arguments=(validate_tool_call_arguments)
6062 @validate_tool_call_arguments = validate_tool_call_arguments
6163 end
6264
65+ # Opt in to exposing the user-defined `server_context` in the
66+ # `around_request` / `instrumentation_callback` data hash. Off by default:
67+ # the hash is application-supplied and may hold values a tracing backend
68+ # should not receive, so surfacing it has to be a deliberate choice.
69+ def instrument_server_context = ( instrument_server_context )
70+ validate_value_of_instrument_server_context! ( instrument_server_context )
71+
72+ @instrument_server_context = instrument_server_context
73+ end
74+
6375 def validate_tool_call_results = ( validate_tool_call_results )
6476 validate_value_of_validate_tool_call_results! ( validate_tool_call_results )
6577
@@ -107,6 +119,10 @@ def instrumentation_callback?
107119 attr_reader :validate_tool_call_arguments
108120 attr_reader :validate_tool_call_results
109121
122+ def instrument_server_context?
123+ !!@instrument_server_context
124+ end
125+
110126 def validate_tool_call_arguments?
111127 !!@validate_tool_call_arguments
112128 end
@@ -152,6 +168,7 @@ def merge(other)
152168 protocol_version : protocol_version ,
153169 validate_tool_call_arguments : validate_tool_call_arguments ,
154170 validate_tool_call_results : validate_tool_call_results ,
171+ instrument_server_context : other . instrument_server_context? ,
155172 )
156173 end
157174
@@ -176,6 +193,12 @@ def validate_value_of_validate_tool_call_results!(validate_tool_call_results)
176193 end
177194 end
178195
196+ def validate_value_of_instrument_server_context! ( instrument_server_context )
197+ unless instrument_server_context . is_a? ( TrueClass ) || instrument_server_context . is_a? ( FalseClass )
198+ raise ArgumentError , "instrument_server_context must be a boolean"
199+ end
200+ end
201+
179202 def default_exception_reporter
180203 @default_exception_reporter ||= -> ( exception , server_context ) { }
181204 end
0 commit comments