Capturing agent intent

Intent explains why an agent called a tool. Use it to understand the goals behind tool usage.

The SDK captures intent as a single property – $mcp_intent – that can come from one of two sources:

  1. A context argument the agent passes on every tool call. Captured with $mcp_intent_source = "context_parameter".
  2. A fallback callback you supply on instrument(). Captured with $mcp_intent_source = "inferred".

Explicit context always wins, except in Rust, where userIntent in the request's io.modelcontextprotocol/aiInvocation metadata overrides it and is captured with $mcp_intent_source = "client_metadata". If the agent passes a non-empty context, the fallback is not invoked.

The context argument

Intent capture is on by default. The SDK adds a context string to compatible tool schemas and removes it before your handler runs when it can confirm it injected the field. An application-owned context argument is preserved.

Calls can omit it without failing validation.

What the agent sees in the schema:

JSON
{
"type": "object",
"properties": {
"context": {
"type": "string",
"description": "Explain in 15-25 words, in third person, why this tool is called and how it supports the user's goal. For analytics only. ..."
},
"...": "your real tool arguments"
},
"required": ["context", "..."]
}

Your handler receives only your own arguments. The SDK removes context before the handler runs.

The full default prompt also tells the agent never to include personal data and to generalize people into roles such as "a user" or "the customer".

PostHog captures this value as $mcp_intent:

"Retrieving a customer's recent pageviews to triage a drop in conversion"

If the agent includes personal data anyway, the SDK replaces emails, phone numbers, IP addresses, card numbers, and US Social Security numbers with [redacted] before sending. See Privacy and redaction.

Customize the prompt

If you want to nudge the agent toward a specific style of context (use case, user goal, ticket id, etc.), pass an object:

instrument(server, posthog, {
context: {
description: "Describe the user's underlying goal in one sentence, not the tool you're calling.",
},
})

Disabling the injected argument

Set the context option to false to turn off the injected argument. See your language's installation page for the option name. Use an intent fallback if you still want intent. Model capture and conversation IDs have their own options, which you can disable separately.

The intent fallback callback

A client can omit the SDK-injected context argument. Without a fallback, the event has no $mcp_intent.

The SDK calls the fallback when the agent provides no context. It captures a non-empty result as $mcp_intent with $mcp_intent_source = "inferred".

The SDK does no inference of its own. Your callback supplies the intent.

Deterministic, per-tool

Use a synchronous callback when you can derive intent from the tool name and arguments:

instrument(server, posthog, {
intentFallback: (request) => {
const tool = request.params?.name
const args = request.params?.arguments ?? {}
if (tool === "search_events") return `Searching events for "${args.query}"`
return tool ? `Invoking ${tool}` : null
},
})

Using transport metadata

extra carries MCP transport details – useful when the agent's user-agent or auth context hints at intent:

TypeScript
import { getRequestHeaders } from "@posthog/mcp"
intentFallback: (request, extra) => {
const ua = getRequestHeaders(extra)?.["user-agent"]
return `${ua ?? "unknown client"} invoked ${request.params?.name}`
}

LLM-derived intent

An LLM call in intentFallback adds latency to each tool call that lacks context. Cache results where possible. Handle failures in the callback:

TypeScript
intentFallback: async (request) => {
try {
return await summariseIntent(request.params)
} catch {
return null // the SDK swallows null gracefully
}
}

Filtering on intent source

$mcp_intent_source is set to "context_parameter" or "inferred" only when an intent was captured. If neither a context argument nor a fallback result was available, both $mcp_intent and $mcp_intent_source are absent on the event.

If you want to know what fraction of your traffic is contextualized:

SQL
SELECT
properties.$mcp_intent_source AS source,
count() AS calls
FROM events
WHERE event = '$mcp_tool_call'
AND timestamp > now() - INTERVAL 7 DAY
GROUP BY source
ORDER BY calls DESC

A high share of inferred means most clients do not supply context. Review context.description or improve intentFallback.

Gotchas

`get_more_tools` reports its source as `context_parameter`

get_more_tools always reports $mcp_intent_source = "context_parameter". Filter it out of source-attribution queries.

Still have questions?

Was this page useful?