MCP for Coding Agents

TrailAI ships an MCP (Model Context Protocol) server that lets coding agents — Claude Code, Cursor, Windsurf, or any MCP-capable client — stream their work into TrailAI as traces. The agent authenticates with your TrailAI API keys, and every run shows up in the dashboard exactly like an SDK-instrumented app:

What the agent logs Where it appears
A run (trail_start_run → trail_finish_run) Observations → Tracing (full span tree) and Analytics → Agents
Steps and tool calls (trail_log_step) The trace tree, plus the Agents → Tools table
Model calls (trail_log_llm_call) LLM analytics — tokens, cost, model breakdown — and the span I/O panel
Failures (trail_log_exception) Observations → Exceptions

Install

Published as trailai-mcp on npm — you don't need this repository. npx fetches and runs it on demand, so the config below is the entire install.

Connect your agent

Generate a key pair under Settings → API Keys, then register the server.

Claude Code

claude mcp add trail \
  -e TRAIL_URL=https://api.trailai.dev/api/sdk/v1 \
  -e TRAIL_PUBLIC_KEY=pk-... \
  -e TRAIL_SECRET_KEY=sk-... \
  -e TRAIL_APP=claude-code \
  -- npx -y trailai-mcp

Cursor / Windsurf — add to .cursor/mcp.json (or the equivalent):

{
  "mcpServers": {
    "trail": {
      "command": "npx",
      "args": ["-y", "trailai-mcp"],
      "env": {
        "TRAIL_URL": "https://api.trailai.dev/api/sdk/v1",
        "TRAIL_PUBLIC_KEY": "pk-...",
        "TRAIL_SECRET_KEY": "sk-...",
        "TRAIL_APP": "cursor"
      }
    }
  }
}

Contributing to the server itself, or running an unpublished change, build from source instead — see mcp/README.md in the repository — and swap command/args above for node and the absolute path to mcp/dist/index.js.

Tell the agent to use it

MCP tools are only called when the agent decides to — so add an instruction to your project rules file (CLAUDE.md, .cursorrules, or similar):

At the start of every task, call trail_start_run with a short task name. Log each meaningful action with trail_log_step (kind tool for commands and file edits, task for planning), every model call with trail_log_llm_call, and failures with trail_log_exception. When the task completes, call trail_finish_run with a one-line summary.

Tool reference

Tool Purpose Key arguments
trail_start_run Begin a traced run; returns run_id name, agent
trail_log_step Record a task or tool step run_id, name, kind, input, output, status
trail_log_llm_call Record an LLM request run_id, model, prompt, completion, token counts, cost_usd
trail_log_exception Record a failure run_id, message, type
trail_finish_run Close the run and emit the root span run_id, status, summary

status accepts "ok" or "error"; synonyms like success, failed, failure, pass, passed (any case) are normalized to those two values.

How it works

Each tool call becomes an OpenTelemetry span posted to TrailAI's standard ingest endpoint with the same gen_ai.* semantics the Python SDK emits — agent runs use gen_ai.operation.name = agent, steps execute_task / execute_tool, and model calls chat with usage attributes. Authentication uses the same X-Public-Key / X-Secret-Key headers as the SDK, so keys are managed in one place and a bad key surfaces as a visible tool error in the agent instead of silently dropping traces.