Log Trace
log_traceLog agent execution traces with spans, tool calls, latency, and metrics for observability and evaluation in AI workflows.
Instructions
Log an agent execution trace with spans, tool calls, and metrics
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| agent_name | Yes | Name of the agent | |
| framework | No | Agent framework name | |
| input | No | Agent input text | |
| output | No | Agent output text | |
| tool_calls | No | Tool calls made during execution | |
| latency_ms | No | Total execution time in milliseconds | |
| token_usage | No | Token usage breakdown | |
| cost_usd | No | Total cost in USD | |
| metadata | No | Arbitrary metadata | |
| spans | No | Detailed execution spans | |
| timestamp | No | Trace timestamp (ISO 8601) |
Implementation Reference
- src/tools/log-trace.ts:59-92 (handler)The handler function for the log_trace tool which processes the input arguments, generates identifiers, constructs the trace object, and stores it using the provided storage adapter.
async (args) => { const traceId = generateTraceId(); const timestamp = args.timestamp ?? new Date().toISOString(); const trace = { trace_id: traceId, agent_name: args.agent_name, framework: args.framework, input: args.input, output: args.output, tool_calls: args.tool_calls, latency_ms: args.latency_ms, token_usage: args.token_usage, cost_usd: args.cost_usd, metadata: args.metadata as Record<string, unknown> | undefined, timestamp, spans: args.spans?.map((s) => ({ ...s, span_id: s.span_id ?? generateSpanId(), trace_id: traceId, })), }; await storage.insertTrace(trace); return { content: [ { type: 'text' as const, text: JSON.stringify({ trace_id: traceId, status: 'stored' }), }, ], }; }, - src/tools/log-trace.ts:37-49 (schema)Zod-based input schema definition for the log_trace tool.
const inputSchema = { agent_name: z.string().describe('Name of the agent'), framework: z.string().optional().describe('Agent framework name'), input: z.string().optional().describe('Agent input text'), output: z.string().optional().describe('Agent output text'), tool_calls: z.array(ToolCallSchema).optional().describe('Tool calls made during execution'), latency_ms: z.number().optional().describe('Total execution time in milliseconds'), token_usage: TokenUsageSchema.optional().describe('Token usage breakdown'), cost_usd: z.number().optional().describe('Total cost in USD'), metadata: z.record(z.unknown()).optional().describe('Arbitrary metadata'), spans: z.array(SpanSchema).optional().describe('Detailed execution spans'), timestamp: z.string().optional().describe('Trace timestamp (ISO 8601)'), }; - src/tools/log-trace.ts:51-94 (registration)Registration function that binds the log_trace tool to the McpServer instance.
export function registerLogTraceTool(server: McpServer, storage: IStorageAdapter): void { server.registerTool( 'log_trace', { title: 'Log Trace', description: 'Log an agent execution trace with spans, tool calls, and metrics', inputSchema, }, async (args) => { const traceId = generateTraceId(); const timestamp = args.timestamp ?? new Date().toISOString(); const trace = { trace_id: traceId, agent_name: args.agent_name, framework: args.framework, input: args.input, output: args.output, tool_calls: args.tool_calls, latency_ms: args.latency_ms, token_usage: args.token_usage, cost_usd: args.cost_usd, metadata: args.metadata as Record<string, unknown> | undefined, timestamp, spans: args.spans?.map((s) => ({ ...s, span_id: s.span_id ?? generateSpanId(), trace_id: traceId, })), }; await storage.insertTrace(trace); return { content: [ { type: 'text' as const, text: JSON.stringify({ trace_id: traceId, status: 'stored' }), }, ], }; }, ); }