Capturing agent intent
Contents
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:
- A
contextargument the agent passes on every tool call. Captured with$mcp_intent_source = "context_parameter". - A fallback callback you supply on
instrument(). Captured with$mcp_intent_source = "inferred".
Explicit context always wins. 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.
TypeScript advertises the injected field as required. Python makes it optional on raw low-level servers and standalone FastMCP. Calls can omit the injected field without failing validation.
What the agent sees in the schema:
What your handler receives:
PostHog captures this value as $mcp_intent:
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:
Disabling the injected argument
Set context: false to disable the injected intent argument. Use intentFallback if you still want to capture intent. Model capture and conversation IDs have separate options. Disable those options to stop their schema changes.
The intentFallback callback
A client can omit the SDK-injected context argument. Without a fallback, the event has no $mcp_intent.
The SDK calls intentFallback 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. It doesn't call an LLM. It doesn't inspect your tool arguments. It doesn't cache results. Whatever logic you want goes in your callback.
Deterministic, per-tool
Use a synchronous callback when you can derive intent from the tool name and arguments:
Using transport metadata
extra carries MCP transport details – useful when the agent's user-agent or auth context hints at intent:
getRequestHeaders reads headers on both MCP SDK majors – see MCP SDK v2.
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:
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:
A high share of inferred means most clients do not supply context. Review context.description or improve intentFallback.
Gotchas
The virtual get_more_tools tool (enabled by reportMissing: true) always reports $mcp_intent_source = "context_parameter", even though the SDK is what defined the schema. Defensible – the agent did type a string – but filter it out of source-attribution queries if the number matters.
Omitting the SDK-injected context argument doesn't fail validation. Your tool's own required arguments still apply. Use intentFallback to capture intent when the agent omits context.
You do not need a fallback if your internal client always supplies context. Add one if clients can omit this argument.