Skip to content

MCP Integration

AgentLens ships as an MCP server (@agentlensai/mcp) that agents add to their tool configuration. This is the primary integration method — it requires zero code changes in your agent.

How It Works

  1. Your agent connects to the AgentLens MCP server via stdio transport
  2. The MCP server exposes 5 tools that the agent can call
  3. Events are sent to the AgentLens API server over HTTP
  4. Events appear in the dashboard in real time
Agent  ──MCP stdio──►  @agentlensai/mcp  ──HTTP──►  @agentlensai/server
                        (5 tools)                    (API + Dashboard)

MCP Tools

agentlens_session_start

Start a new monitoring session. Call this at the beginning of an agent workflow.

ParameterTypeRequiredDescription
agentIdstringUnique identifier for the agent
agentNamestringHuman-readable agent name
tagsstring[]Tags for categorizing the session

Returns: { sessionId: string }

agentlens_log_event

Log an event within a session. Use this for tool calls, errors, custom events, and cost tracking.

ParameterTypeRequiredDescription
sessionIdstringSession ID from session_start
agentIdstringAgent identifier
eventTypestringOne of the 18 event types
severitystringdebug / info / warn / error / critical (default: info)
payloadobjectEvent-specific payload (see Event Types below)
metadataobjectAdditional metadata

agentlens_session_end

End a monitoring session. Call this when the agent workflow completes.

ParameterTypeRequiredDescription
sessionIdstringSession ID to end
reasonstringcompleted / error / timeout / manual
summarystringHuman-readable summary

agentlens_log_llm_call

Log a complete LLM interaction (request + response) in a single call. Internally emits two paired events (llm_call + llm_response) with a shared callId.

ParameterTypeRequiredDescription
sessionIdstringActive session ID
providerstringProvider name (e.g., openai, anthropic)
modelstringModel identifier
messagesarrayPrompt messages ([{ role, content }])
systemPromptstringSystem prompt (if separate)
completionstring | nullModel response content
toolCallsarrayTool calls requested by the model
finishReasonstringstop, length, tool_use, content_filter, or error
usageobject{ inputTokens, outputTokens, totalTokens }
costUsdnumberCost in USD
latencyMsnumberLatency in milliseconds
parametersobjectModel parameters (temperature, maxTokens, etc.)
toolsarrayTool definitions provided to the model

Returns: { callId: string, eventsLogged: 2 }

See the LLM Call Tracking guide for detailed usage and provider examples.

agentlens_query_events

Query events for agent self-inspection. Agents can review their own recent activity.

ParameterTypeRequiredDescription
sessionIdstringFilter by session
eventTypestringFilter by event type
limitnumberMax results (default: 10)

Event Types

Each event type has a specific payload schema:

Tool Events

json
// tool_call
{
  "toolName": "search_database",
  "callId": "call_01",
  "arguments": { "query": "user records" },
  "serverName": "my-mcp-server"
}

// tool_response
{
  "callId": "call_01",
  "toolName": "search_database",
  "result": { "count": 42 },
  "durationMs": 342
}

// tool_error
{
  "callId": "call_01",
  "toolName": "search_database",
  "error": "Connection timeout",
  "errorCode": "ETIMEOUT",
  "durationMs": 5000
}

Cost Tracking

json
// cost_tracked
{
  "provider": "anthropic",
  "model": "claude-sonnet-4-20250514",
  "inputTokens": 1500,
  "outputTokens": 800,
  "totalTokens": 2300,
  "costUsd": 0.0092,
  "trigger": "search_database"
}

Custom Events

json
// custom
{
  "type": "user_feedback",
  "data": { "rating": 5, "comment": "Helpful response" }
}

Buffering & Reliability

The MCP server buffers events locally and flushes to the API in batches:

  • Batch size: Up to 100 events per flush
  • Flush interval: Every 1 second
  • Buffer capacity: Up to 10,000 events if the API is unreachable
  • Recovery: Buffered events are flushed automatically when the API reconnects

This means your agent continues working even if the AgentLens server is temporarily down.

Released under the MIT License.