feat: run identify hook per event and drop session identity cache#30
Open
feat: run identify hook per event and drop session identity cache#30
Conversation
Collapses the stateful/stateless split in identify_session so the configured identify hook runs on every MCP request in both modes. This matches the TypeScript SDK's per-event semantics, lets customers refresh identity mid-session (token rotation, impersonation, role changes), and exposes the real per-request context to hooks that inspect it. Removes MCPCatData.identified_sessions entirely. Identity is now written directly onto each event by the handler that ran the hook, and a mcpcat:identify self-event is published alongside every successful identification. All hook invocation, validation, event construction, and publish calls are wrapped in a single try/except so identify can never break a tool call.
kashishhora
approved these changes
Apr 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
identify_sessionso the configured identify hook runs on every MCP request in both modes. Matches the TypeScript SDK and lets customers refresh identity mid-session (token rotation, impersonation, role changes).MCPCatData.identified_sessionsentirely. Identity is written directly onto each event by the handler that ran the hook;publish_event'sexclude_none=Truemerge preserves those fields.mcpcat:identifyself-event on every successful identification (one per normal event), so downstream analytics see a distinct identify marker per request.try/except—identify_sessioncan never raise into a tool call handler.get_session_infono longer carries identity fields; they are alwaysNone.Why
The previous stateful path ran the identify hook once per session and short-circuited all later requests, pinning identity to the first call's result and feeding hooks only the first request's context. Customers porting hooks between the Python and TypeScript SDKs hit surprising semantic differences. Unifying on per-event hook execution removes that asymmetry and makes identity reactive to per-request context.
Test plan
uv run pytest tests/— 349 passed, 2 unrelated skipstests/test_stateless.py,tests/test_event_capture_completeness.py,tests/test_session.pyidentified_sessionsanywhere insrc/ortests/identifyhook, and confirm the hook is invoked on every request andmcpcat:identifyevents appear 1:1 with normal events