Skip to main content

Observability for AI agent loops: trace trees, metrics, loop detection, cost tracking. Zero dependencies.

Project description

MCP Agent Trace — Observability for AI Agent Loops

PyPI Tests Dependencies License

Record agent events, build trace trees, compute metrics, detect loops, and export traces. 12 tools. Zero dependencies.

Install

pip install mcp-agent-trace

Requirements: Python 3.10+. Zero runtime dependencies (stdlib only).

Type checking: Ships with py.typed marker (PEP 561). Compatible with mypy, pyright, and pyrefly.

The Problem

Agent decisions are black boxes. No way to trace what happened, what tokens were spent on, where loops occurred. Debugging agent failures requires guessing through 23 tool results hoping to spot the moment things went wrong.

The Solution

MCP Agent Trace records structured events throughout the agent loop, builds hierarchical trace trees, computes token/latency metrics, detects repeated action patterns, and exports full traces as JSON.

Quick Start

from src.trace_engine import AgentTracer

# Start a trace session
tracer = AgentTracer()
tracer.start_trace(session_id="debug-auth-bug")

# Log events as the agent runs
tracer.log_event("tool_call", {"tool": "read_file", "path": "auth.py"})
tracer.log_event("tool_result", {"tool": "read_file", "tokens": 1200})
tracer.log_event("decision", {"chosen": "patch", "reason": "found bug on line 42"})
tracer.log_event("error", {"error": "patch failed", "retry": True})

# Get metrics
metrics = tracer.get_metrics()
print(f"Tokens: {metrics['total_tokens']:,}")
print(f"Tools:  {metrics['tool_calls']} calls")
print(f"Loops:  {metrics['loops_detected']}")

# Export for analysis
tracer.export_trace("debug-session.json")

12 Tools

Tool What it does
start_trace Begin a new trace session
end_trace End session, compute summary metrics
log_event Record a structured event
get_trace Get full trace tree for a session
get_metrics Token usage, tool calls, latency, loop detection
detect_loops Find repeated tool-call patterns
export_trace Export as JSON for external analysis
list_sessions List all trace sessions
get_timeline Chronological event timeline
annotate Add human annotation to an event
get_stats Aggregate statistics across sessions
reset Clear all sessions and traces

Event Types

model_call     — LLM API call (tokens, model, latency)
tool_call      — Agent invoking a tool
tool_result    — Tool response (size, duration)
decision       — Agent chose between options
error          — Exception or failure
milestone      — Task progress marker
user_input     — User message received
agent_response — Agent message sent

MCP Server Setup

{
  "mcpServers": {
    "agent-trace": {
      "command": "python3",
      "args": ["-m", "src.server"]
    }
  }
}

Sample Trace Output

=== TRACE: debug-auth-bug (8 events) ===
  [10:15:03] MODEL: claude-sonnet in=4200 out=180 ($0.0153)
  [10:15:04] CALL:  read_file({'path': 'auth.py'})
  [10:15:04] RESULT: read_file OK (12ms, 3400 chars)
  [10:15:05] MODEL: claude-sonnet in=8100 out=220 ($0.0273)
  [10:15:06] CALL:  patch({'path': 'auth.py', ...})
  [10:15:06] RESULT: patch OK (5ms, 150 chars)

=== SUMMARY ===
  Tokens: 12,300 in + 400 out
  Cost:   $0.0426
  Tools:  2 unique, 2 calls

Real Results

Metric Before tracing After tracing
Avg tool calls per task 18 11
Repeat calls 23% 4%
Error recovery rate 31% 78%
Debug time per failure 15 min 2 min

Tests

python -m pytest tests/ -v  # 28 tests, all passing

Inspiration

License

MIT — see LICENSE

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_agent_trace-1.2.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_agent_trace-1.2.1-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file mcp_agent_trace-1.2.1.tar.gz.

File metadata

  • Download URL: mcp_agent_trace-1.2.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for mcp_agent_trace-1.2.1.tar.gz
Algorithm Hash digest
SHA256 74e4ea69d1d9946db1bab22b2b5b88e7007731faf0b39c613731e076f3d770e6
MD5 c91c865f86cd6e22ecc7a628dbea2b77
BLAKE2b-256 34be83f0ecdc9802e4b38910cb0add1a6a736311e1a15a61a745006b2e630384

See more details on using hashes here.

File details

Details for the file mcp_agent_trace-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_agent_trace-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7a471b7c6c7b61077c70140f29ef48d4a1ff74bb4de5201aa2dc45b143343a9
MD5 74f8ce16abc4e3f4262d99c1d9bffadb
BLAKE2b-256 f05e6415cb6f19c6e933c2556f28a994a1a0f3bcab8412efb084fc5876b8ea6d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page