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

@ -145,6 +145,35 @@ module MCP
refute merged.validate_tool_call_arguments
end
test "defaults instrument_server_context to false" do
config = Configuration.new
refute_predicate config, :instrument_server_context?
end
test "accepts instrument_server_context in the constructor" do
config = Configuration.new(instrument_server_context: true)
assert_predicate config, :instrument_server_context?
end
test "raises ArgumentError when instrument_server_context is not a boolean value" do
config = Configuration.new
exception = assert_raises(ArgumentError) do
config.instrument_server_context = "true"
end
assert_equal("instrument_server_context must be a boolean", exception.message)
end
test "merge carries instrument_server_context from the other configuration" do
base = Configuration.new(instrument_server_context: true)
other = Configuration.new(instrument_server_context: false)
refute_predicate base.merge(other), :instrument_server_context?
assert_predicate other.merge(base), :instrument_server_context?
end
test "defaults validate_tool_call_results to false" do
config = Configuration.new
refute config.validate_tool_call_results

View file

@ -40,6 +40,79 @@ module MCP
end
end
# Mirrors `MCP::Server`, which exposes the user-defined hash through
# `attr_accessor :server_context`.
class SubjectWithServerContext < Subject
attr_accessor :server_context
def initialize(server_context)
super()
@server_context = server_context
end
end
test "#instrument_call omits the user-defined server_context by default" do
subject = SubjectWithServerContext.new({ user_id: 42 })
subject.instrumented_method
refute_includes subject.instrumentation_data_received.keys, :server_context
end
test "#instrument_call exposes the user-defined server_context when instrument_server_context is enabled" do
subject = SubjectWithServerContext.new({ user_id: 42 })
subject.configuration.instrument_server_context = true
subject.instrumented_method
assert_equal({ user_id: 42 }, subject.instrumentation_data_received[:server_context])
end
test "#instrument_call exposes the user-defined server_context to around_request" do
subject = SubjectWithServerContext.new({ user_id: 42 })
subject.configuration.instrument_server_context = true
seen = nil
subject.configuration.around_request = ->(data, &handler) {
seen = data[:server_context]
handler.call
}
subject.instrumented_method
assert_equal({ user_id: 42 }, seen)
end
test "#instrument_call reports the user-defined server_context as nil when the host does not set one" do
subject = SubjectWithServerContext.new(nil)
subject.configuration.instrument_server_context = true
subject.instrumented_method
assert_includes subject.instrumentation_data_received.keys, :server_context
assert_nil subject.instrumentation_data_received[:server_context]
end
test "#instrument_call does not confuse the reporter context with the user-defined server_context" do
# `instrument_call(server_context:)` is the exception-reporter context, a
# different value from the user-defined hash. Enabling the flag must
# surface the latter, not the former.
subject = SubjectWithServerContext.new({ user_id: 42 })
subject.configuration.instrument_server_context = true
subject.instrumented_method_with_server_context({ request: "reporter-context" })
assert_equal({ user_id: 42 }, subject.instrumentation_data_received[:server_context])
end
test "#instrument_call skips server_context for hosts without the reader even when enabled" do
subject = Subject.new
subject.configuration.instrument_server_context = true
subject.instrumented_method
refute_includes subject.instrumentation_data_received.keys, :server_context
end
test "#instrument_call adds the method name to the instrumentation data" do
subject = Subject.new