diff --git a/docs/user/observability.md b/docs/user/observability.md index efb19cb0..036f7aef 100644 --- a/docs/user/observability.md +++ b/docs/user/observability.md @@ -168,6 +168,9 @@ A span has these attributes from its start: the server has no tool for. - `pharo.mcp.dispatch_path`: `direct`, or `via_tool_call` for a call `tool_call` made. +- `pharo.mcp.tool.advertised`, whether the server lists the tool in + `tools/list` ([advertised](building-on-mcp.md#shaping-the-servers-surface)) + or the agent had to find it; absent for a name the server has no tool for. - `session.id`, the session the call belongs to: the `session.id` entry of its W3C `baggage` when that reads as an id, else the server's own, a random id that starts at the server's first such call and is replaced after 30 diff --git a/src/MCP-OpenTelemetry-Tests/MCPOpenTelemetryObservabilityBackendTest.class.st b/src/MCP-OpenTelemetry-Tests/MCPOpenTelemetryObservabilityBackendTest.class.st index 72601969..5cf6c4e4 100644 --- a/src/MCP-OpenTelemetry-Tests/MCPOpenTelemetryObservabilityBackendTest.class.st +++ b/src/MCP-OpenTelemetry-Tests/MCPOpenTelemetryObservabilityBackendTest.class.st @@ -965,6 +965,29 @@ MCPOpenTelemetryObservabilityBackendTest >> testConfiguringABackendReconfiguresE self deny: OTTracerProvider current identicalTo: tracerProvider ] +{ #category : 'tests' } +MCPOpenTelemetryObservabilityBackendTest >> testEachCallSaysWhetherItsToolWasAdvertised [ + "Whether the server that answered lists the tool in tools/list, or the agent had to find it: an agent calls the tools it sees first more often, so their counts are read with this. A name no tool has says nothing." + + | mcp | + mcp := self mcp. + mcp unadvertiseTool: 'class_search'. + { + ('class_get' -> { ('name' -> 'Point') }). + ('class_search' -> { ('query' -> 'Point') }). + ('no_such_tool' -> { }) } doWithIndex: [ :call :index | + mcp server messageProcessor handleJSON: (STONJSON toString: { + ('jsonrpc' -> '2.0'). + ('id' -> index). + ('method' -> 'tools/call'). + ('params' -> { + ('name' -> call key). + ('arguments' -> call value asDictionary) } asDictionary) } asDictionary) ]. + self + assert: (exporter snapshots collect: [ :each | self attributeNamed: 'pharo.mcp.tool.advertised' in: each ] as: Array) + equals: { true. false. nil } +] + { #category : 'tests' } MCPOpenTelemetryObservabilityBackendTest >> testEachCallSaysWhoseToolItWentTo [ "mcp for MCP's own tools, other for another package's, unknown for a name no tool has." diff --git a/src/MCP-OpenTelemetry/MCPOpenTelemetryObservabilityBackend.class.st b/src/MCP-OpenTelemetry/MCPOpenTelemetryObservabilityBackend.class.st index 6f5282f4..d802d8ad 100644 --- a/src/MCP-OpenTelemetry/MCPOpenTelemetryObservabilityBackend.class.st +++ b/src/MCP-OpenTelemetry/MCPOpenTelemetryObservabilityBackend.class.st @@ -1186,7 +1186,7 @@ MCPOpenTelemetryObservabilityBackend >> spanOfRecord: aRecord [ { #category : 'private - spans' } MCPOpenTelemetryObservabilityBackend >> startAttributesFor: aRecord named: aToolName origin: anOrigin input: inputObject context: contextDictionary parent: aContext [ - "The attributes known when a call starts, set before the span starts so a sampler sees them. They follow the OpenTelemetry conventions for MCP where those name the fact, and say nothing the caller passed but argument names, those its tool declares apart from the others, the options its tool's schema offers that it chose, the session its trace context names, or else the server's own, and the client it says it is. Each fact is read on its own, so one that cannot be read leaves the others." + "The attributes known when a call starts, set before the span starts so a sampler sees them. They follow the OpenTelemetry conventions for MCP where those name the fact, and say nothing the caller passed but argument names, those its tool declares apart from the others, the options its tool's schema offers that it chose, the session its trace context names, or else the server's own, the client it says it is, and whether the server lists its tool in tools/list. Each fact is read on its own, so one that cannot be read leaves the others." | attributes tool | tool := contextDictionary at: #tool ifAbsent: [ nil ]. @@ -1199,6 +1199,13 @@ MCPOpenTelemetryObservabilityBackend >> startAttributesFor: aRecord named: aTool add: 'network.protocol.name' -> 'http'; add: 'pharo.mcp.tool.origin' -> anOrigin. self addAttributesFrom: [ { ('pharo.mcp.dispatch_path' -> aRecord dispatchPath) } ] to: attributes. + self + addAttributesFrom: [ + contextDictionary + at: #advertised + ifPresent: [ :advertised | { ('pharo.mcp.tool.advertised' -> advertised) } ] + ifAbsent: [ #( ) ] ] + to: attributes. self addAttributesFrom: [ { ('session.id' -> (self sessionIdFor: aContext)) } ] to: attributes. self addAttributesFrom: [ diff --git a/src/MCP/MCP.class.st b/src/MCP/MCP.class.st index 514572c4..339c2d86 100644 --- a/src/MCP/MCP.class.st +++ b/src/MCP/MCP.class.st @@ -645,13 +645,16 @@ MCP >> observabilityContextFor: aTool [ { #category : 'observability' } MCP >> observabilityContextFor: aTool parentRecord: aRecord [ - "What a backend may want to know about a call beyond its name and arguments: the tool, for its schema, the request's _meta, for the caller's trace context and identity, the client that last initialized, for a caller whose requests do not say who it is, and the record of the call that dispatched this one, if one did." - - ^ { - (#tool -> aTool). - (#meta -> (MCPRequestMeta value ifNil: [ Dictionary new ])). - (#client -> lastClientInfo). - (#parentRecord -> aRecord) } asDictionary + "What a backend may want to know about a call beyond its name and arguments: the tool, for its schema, whether tools/list shows it, the request's _meta, for the caller's trace context and identity, the client that last initialized, for a caller whose requests do not say who it is, and the record of the call that dispatched this one, if one did." + + | context | + context := { + (#tool -> aTool). + (#meta -> (MCPRequestMeta value ifNil: [ Dictionary new ])). + (#client -> lastClientInfo). + (#parentRecord -> aRecord) } asDictionary. + aTool ifNotNil: [ context at: #advertised put: (self toolRegistry isToolAdvertised: aTool) ]. + ^ context ] { #category : 'observability' }