MCP Agent Trace
Enables recording and analyzing AI agent execution traces, including event logging, metric computation, loop detection, and JSON export for debugging agent behavior.
README
MCP Agent Trace — Observability for AI Agent Loops
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
- AgentDoG — Diagnostic guardrails
- Azure SRE Agent — 35k incidents traced
- LangChain middleware
License
MIT — see LICENSE
Links
Freelance portfolio: https://ameobius-space.github.io/kwork-portfolio/
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。