Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contents/docs/mcp-analytics/installation/python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ from posthog.mcp import PostHogMcpStatelessSessionMiddleware
app.add_middleware(PostHogMcpStatelessSessionMiddleware)
```

### Resolve tool arguments on fresh low-level servers

A fresh low-level `Server` has not served `tools/list`. A strict tool can reject the `context`, `llm_model`, or `conversation_id` arguments that PostHog added.

Use `resolve_original_tool` to return the original tool descriptor from your registry. The callback must return the descriptor from before PostHog adds its arguments:

```python
instrument(server, posthog, MCPAnalyticsOptions(
resolve_original_tool=lambda tool_name: tools_by_name.get(tool_name),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix before merge: This sample hits the bug in PostHog/posthog-python#1009, so it should land after that fix. Small one on the TypeScript page: the table's return type should be { inputSchema } | undefined.

))
```

The SDK removes only PostHog-owned arguments. A listing served on the same server instance has priority over this callback.

## Configuration

Pass options as `MCPAnalyticsOptions`:
Expand Down Expand Up @@ -143,6 +157,7 @@ instrument(server, posthog, MCPAnalyticsOptions(
| `before_send` | – | `(event) -> event \| None`. Change or drop each event before it's sent. See [Privacy](/docs/mcp-analytics/privacy). |
| `event_properties` | – | `(request, extra) -> dict`. Adds [properties](/docs/mcp-analytics/custom-events) to every event. |
| `server_build` | – | An immutable build ID, such as a Git SHA, sent as `$mcp_server_build`. 1 to 256 characters. |
| `resolve_original_tool` | – | `(tool_name) -> tool \| None`. Resolves argument ownership on a fresh low-level server. See [Resolve tool arguments on fresh low-level servers](#resolve-tool-arguments-on-fresh-low-level-servers). |
| `logger` | no-op | `(message: str) -> None`. A stdio-safe sink for SDK warnings. |

On raw low-level servers and standalone FastMCP, the SDK advertises the injected `context` and `llm_model` arguments as optional, not required. On standalone FastMCP with `mcp` 1.x, middleware that overrides tool listing or dispatch turns off `llm_model` injection. Model capture from client metadata still works.
Expand Down
18 changes: 18 additions & 0 deletions contents/docs/mcp-analytics/installation/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ if (body?.method === "initialize" && !req.headers[MCP_SESSION_HEADER]) {
}
```

### Resolve tool arguments on fresh low-level servers

A fresh low-level `Server` has not served `tools/list`, so it does not know which `context` and `llm_model` arguments the SDK added. A strict input schema can reject these arguments before the tool runs.

Use `resolveOriginalTool` to return each tool's original input schema from your registry. The callback must return the schema from before PostHog adds its arguments:

```ts
instrument(server, posthog, {
resolveOriginalTool: (toolName) => {
const tool = toolsByName.get(toolName)
return tool ? { inputSchema: tool.inputSchema } : undefined
},
})
```

The SDK strips only arguments that it added. It reads a Zod schema as the MCP SDK advertises it. If your server advertises a custom JSON Schema, return that schema instead.

## Configuration

`instrument(server, posthog, options?)` takes these options:
Expand All @@ -251,6 +268,7 @@ if (body?.method === "initialize" && !req.headers[MCP_SESSION_HEADER]) {
| `serverBuild` | – | An immutable build ID, such as a Git SHA, sent as `$mcp_server_build`. 1 to 256 characters. |
| `shouldRecordInputKey` | declared names | Decides which argument names `$mcp_input_keys` records. Other names become one `[redacted]` entry. |
| `resolveInputAliases` | – | `(toolName) => aliases`. Lists alternative argument names, recorded as `$mcp_input_aliases_used`. |
| `resolveOriginalTool` | – | `(toolName) => { inputSchema } \| undefined`. Resolves input ownership on a fresh low-level server. See [Resolve tool arguments on fresh low-level servers](#resolve-tool-arguments-on-fresh-low-level-servers). |
| `logger` | no-op | `(message) => void`. A stdio-safe sink for SDK warnings. |

## Custom dispatchers
Expand Down
Loading