Repository navigation
Expand file tree
/
Copy path_internal.py
More file actions
208 lines (167 loc) · 7.17 KB
/
Copy path_internal.py
File metadata and controls
208 lines (167 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Portions of this package are derived from MCPCat/mcpcat-typescript-sdk
# Copyright (c) 2025 MCPcat
# Licensed under the MIT License: https://github.com/MCPCat/mcpcat-typescript-sdk/blob/main/LICENSE
"""Per-server tracking state, the bounded identity LRU, and identity resolution.
Per-server state lives in a module-level ``weakref.WeakKeyDictionary`` keyed by
the server object, so state is isolated per server and garbage-collected with it
(the Python equivalent of the TS ``WeakMap``).
"""
from __future__ import annotations
import asyncio
import json
import weakref
from collections import OrderedDict
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import Any, Dict, Optional
from .logger import log
from ._sink import McpEventSink
from .types import MCPAnalyticsOptions, UserIdentity
class IdentityCache:
"""Bounded LRU of session identities, isolated per server so identities never
bleed across server instances."""
def __init__(self, max_size: int = 1000) -> None:
self._cache: "OrderedDict[str, UserIdentity]" = OrderedDict()
self._max_size = max_size
def get(self, session_id: str) -> Optional[UserIdentity]:
identity = self._cache.get(session_id)
if identity is None:
return None
self._cache.move_to_end(session_id)
return identity
def set(self, session_id: str, identity: UserIdentity) -> None:
if session_id in self._cache:
del self._cache[session_id]
elif len(self._cache) >= self._max_size:
self._cache.popitem(last=False)
self._cache[session_id] = identity
def has(self, session_id: str) -> bool:
return session_id in self._cache
def size(self) -> int:
return len(self._cache)
@dataclass
class MCPAnalyticsData:
"""All per-server tracking state."""
options: MCPAnalyticsOptions
sink: Optional[McpEventSink] = None
session_id: str = ""
session_source: str = "generated" # "generated" | "mcp" | "token"
last_mcp_session_id: Optional[str] = None
# Set once we've warned that an HTTP request resolved with no session id — the
# signature of a stateless server whose mint middleware never attached. Warned
# a single time per server so the log isn't flooded on every request.
warned_no_stateless_session: bool = False
last_activity: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
identified_sessions: IdentityCache = field(default_factory=IdentityCache)
tool_categories: Dict[str, str] = field(default_factory=dict)
tool_descriptions: Dict[str, str] = field(default_factory=dict)
# Bounded FIFO of sessions we've emitted $mcp_initialize for, so a long-lived
# server can't accumulate one entry per session forever.
initialized_sessions: "OrderedDict[str, None]" = field(default_factory=OrderedDict)
server_name: Optional[str] = None
server_version: Optional[str] = None
session_lock: asyncio.Lock = field(default_factory=asyncio.Lock)
def mark_session_initialized(self, session_id: str) -> None:
self.initialized_sessions[session_id] = None
while len(self.initialized_sessions) > _MAX_INITIALIZED_SESSIONS:
self.initialized_sessions.popitem(last=False)
_MAX_INITIALIZED_SESSIONS = 1000
_server_tracking: "weakref.WeakKeyDictionary[Any, MCPAnalyticsData]" = (
weakref.WeakKeyDictionary()
)
def get_server_tracking_data(server: Any) -> Optional[MCPAnalyticsData]:
return _server_tracking.get(server)
def set_server_tracking_data(server: Any, data: MCPAnalyticsData) -> None:
_server_tracking[server] = data
def are_identities_equal(a: UserIdentity, b: UserIdentity) -> bool:
if a.distinct_id != b.distinct_id:
return False
if json.dumps(a.groups or {}, sort_keys=True) != json.dumps(
b.groups or {}, sort_keys=True
):
return False
a_props = a.properties or {}
b_props = b.properties or {}
if set(a_props.keys()) != set(b_props.keys()):
return False
for key in a_props:
if json.dumps(a_props[key], sort_keys=True, default=str) != json.dumps(
b_props[key], sort_keys=True, default=str
):
return False
return True
def merge_identities(
previous: Optional[UserIdentity], nxt: UserIdentity
) -> UserIdentity:
if previous is None:
return nxt
return UserIdentity(
distinct_id=nxt.distinct_id,
properties={**(previous.properties or {}), **(nxt.properties or {})},
groups=nxt.groups if nxt.groups is not None else previous.groups,
)
async def _maybe_await(value: Any) -> Any:
if asyncio.iscoroutine(value) or asyncio.isfuture(value):
return await value
return value
async def handle_identify(
data: MCPAnalyticsData,
session_id: str,
request: Dict[str, Any],
extra: Optional[Dict[str, Any]] = None,
) -> Optional[Dict[str, Any]]:
"""Resolve the optional ``identify`` callback, dedupe against the identity
cache, and return an ``$identify`` event to emit only when the identity has
materially changed (otherwise ``None``)."""
if not data.options.identify:
return None
try:
identify = data.options.identify
if isinstance(identify, UserIdentity):
identity_result: Optional[UserIdentity] = identify
else:
identity_result = await _maybe_await(identify(request, extra))
if not identity_result:
log(
f"Warning: Supplied identify function returned null for session {session_id}"
)
return None
previous = data.identified_sessions.get(session_id)
merged = merge_identities(previous, identity_result)
has_changed = not (previous and are_identities_equal(previous, merged))
data.identified_sessions.set(session_id, merged)
if has_changed:
from ._event_types import MCPAnalyticsEventType
log(f"Identified session {session_id}")
return {
"session_id": session_id,
"resource_name": _get_request_resource_name(request),
"event_type": MCPAnalyticsEventType.IDENTIFY,
"parameters": {"request": request, "extra": extra},
"timestamp": datetime.now(timezone.utc),
}
except Exception as error: # noqa: BLE001
log(
f"Error: identify function threw while identifying session {session_id} - {error}"
)
return None
async def resolve_event_properties(
data: MCPAnalyticsData,
request: Dict[str, Any],
extra: Optional[Dict[str, Any]] = None,
) -> Optional[Dict[str, Any]]:
if not data.options.event_properties:
return None
try:
return await _maybe_await(data.options.event_properties(request, extra)) or None
except Exception as e: # noqa: BLE001
log(f"event_properties callback error: {e}")
return None
def _get_request_resource_name(request: Any) -> str:
if not isinstance(request, dict):
return "Unknown"
params = request.get("params")
if not isinstance(params, dict):
return "Unknown"
name = params.get("name")
return name if isinstance(name, str) else "Unknown"