Skip to content

Commit 4a75e10

Browse files
epinzurclaudecoderabbitai[bot]
authored
Add availableTools to agent span attributes for observability (#14550)
## Description This PR adds the `availableTools` attribute to agent tracing spans in the prepare-tools step of the modern agent loop. This ensures that observability integrations (such as Datadog LLM Observability) can correctly receive tool definitions on `AGENT_RUN` spans when using streaming methods. The change updates the agent span with the list of available tool names after tools are converted, making this information available for downstream observability platforms. ## Related Issue(s) <!-- Link to the issue(s) this PR addresses, using hashtag notation: Fixes #123 --> ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [ ] New feature (non-breaking change that adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [ ] Code refactoring - [ ] Performance improvement - [ ] Test update ## Checklist - [ ] I have made corresponding changes to the documentation (if applicable) - [X] I have added tests that prove my fix is effective or that my feature works - [ ] I have addressed all Coderabbit comments on this PR https://claude.ai/code/session_01RhF8cPu2ooEeY8s8rBJw1h <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Agent run tracing during streaming now includes available tool definitions in run spans. * **New Features** * Observability exporters now receive agent tool lists on agent run spans for improved trace visibility. * **Tests** * Integration tests updated to assert available tool identifiers are present in agent run spans. * **Chores** * Trace snapshots updated to reflect the included available tools. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 32c1b0a commit 4a75e10

11 files changed

Lines changed: 36 additions & 9 deletions

.changeset/happy-walls-follow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mastra/core': patch
3+
---
4+
5+
Fixed missing tool lists in agent traces for streaming runs. Exporters like Datadog LLM Observability now receive the tools available to the agent.

observability/mastra/src/__snapshots__/agent-tool-call-trace.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"entityId": "test-agent",
2424
"attributes": {
2525
"conversationId": "test-thread-id",
26-
"instructions": "You are a test agent"
26+
"instructions": "You are a test agent",
27+
"availableTools": ["calculator", "apiCall", "workflowExecutor"]
2728
},
2829
"metadata": {
2930
"runId": "<runId-1>",

observability/mastra/src/__snapshots__/agent-workflow-direct-trace.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"entityType": "agent",
2525
"entityId": "workflow-agent",
2626
"attributes": {
27-
"instructions": "You can execute workflows that exist in your config"
27+
"instructions": "You can execute workflows that exist in your config",
28+
"availableTools": ["workflow-simpleWorkflow"]
2829
},
2930
"metadata": {
3031
"runId": "<runId-1>"

observability/mastra/src/__snapshots__/agent-workflow-tool-trace.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"entityType": "agent",
2525
"entityId": "workflow-agent",
2626
"attributes": {
27-
"instructions": "You can execute workflows using the workflow executor tool"
27+
"instructions": "You can execute workflows using the workflow executor tool",
28+
"availableTools": ["workflowExecutor"]
2829
},
2930
"metadata": {
3031
"runId": "<runId-1>",

observability/mastra/src/__snapshots__/multi-step-text-accumulation-trace.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"entityType": "agent",
2424
"entityId": "multi-step-agent",
2525
"attributes": {
26-
"instructions": "You are a helpful calculator assistant that announces what you will do before doing it."
26+
"instructions": "You are a helpful calculator assistant that announces what you will do before doing it.",
27+
"availableTools": ["calculator"]
2728
},
2829
"metadata": {
2930
"runId": "<runId-1>"

observability/mastra/src/__snapshots__/tool-child-spans-trace.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"entityType": "agent",
2424
"entityId": "child-span-agent",
2525
"attributes": {
26-
"instructions": "You use tools that create child spans"
26+
"instructions": "You use tools that create child spans",
27+
"availableTools": ["childSpanTool"]
2728
},
2829
"metadata": {
2930
"runId": "<runId-1>"

observability/mastra/src/__snapshots__/tool-metadata-trace.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"entityType": "agent",
2323
"entityId": "metadata-agent",
2424
"attributes": {
25-
"instructions": "You use tools and add metadata"
25+
"instructions": "You use tools and add metadata",
26+
"availableTools": ["metadataTool"]
2627
},
2728
"metadata": {
2829
"runId": "<runId-1>"

observability/mastra/src/integration-tests.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,12 @@ describe('Tracing Integration Tests', () => {
823823
expect(llmGenerationSpan?.endTime).toBeDefined();
824824
expect(agentRunSpan?.endTime).toBeDefined();
825825
expect(llmGenerationSpan?.endTime!.getTime()).toBeLessThanOrEqual(agentRunSpan?.endTime!.getTime());
826+
827+
// Verify availableTools is populated on the AGENT_RUN span
828+
expect(agentRunSpan?.attributes?.availableTools).toBeDefined();
829+
expect(agentRunSpan?.attributes?.availableTools).toEqual(
830+
expect.arrayContaining(['calculator', 'apiCall', 'workflowExecutor']),
831+
);
826832
});
827833
},
828834
);

packages/core/src/agent/workflows/prepare-stream/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface CreatePrepareStreamWorkflowOptions<OUTPUT = undefined> {
2424
resourceId?: string;
2525
runId: string;
2626
requestContext: RequestContext;
27-
agentSpan: Span<SpanType.AGENT_RUN>;
27+
agentSpan?: Span<SpanType.AGENT_RUN>;
2828
methodType: AgentMethodType;
2929
instructions: SystemMessage;
3030
memoryConfig?: MemoryConfigInternal;

packages/core/src/agent/workflows/prepare-stream/map-results-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface MapResultsStepOptions<OUTPUT = undefined> {
2424
requestContext: RequestContext;
2525
memory?: MastraMemory;
2626
memoryConfig?: MemoryConfigInternal;
27-
agentSpan: Span<SpanType.AGENT_RUN>;
27+
agentSpan?: Span<SpanType.AGENT_RUN>;
2828
agentId: string;
2929
methodType: AgentMethodType;
3030
saveQueueManager?: SaveQueueManager;

0 commit comments

Comments
 (0)