@kometolabs/mcp-analytics 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,172 @@
1
+ import { type OtlpSinkOptions } from './otlp.js';
2
+ export type { OtlpSinkOptions } from './otlp.js';
3
+ export type SubjectType = 'user' | 'account' | 'service';
4
+ export type Outcome = 'ok' | 'tool_error' | 'exception' | 'input_required';
5
+ export type EventKind = 'tool_call' | 'authorization' | 'value_completed';
6
+ export interface Subject {
7
+ issuer: string;
8
+ tenant?: string;
9
+ id: string;
10
+ type?: SubjectType;
11
+ }
12
+ export interface EventContext {
13
+ /** A verified subject, or a plain user ID (issuer: the project, type: user). */
14
+ subject?: Subject | string;
15
+ clientApplication?: string;
16
+ sourceTag?: string;
17
+ operationId?: string;
18
+ }
19
+ export interface AnalyticsEvent {
20
+ schemaVersion: 1;
21
+ eventId: string;
22
+ projectId: string;
23
+ serverId: string;
24
+ environment: string;
25
+ occurredAt: string;
26
+ kind: EventKind;
27
+ toolName?: string;
28
+ outcome?: Outcome;
29
+ durationMs?: number;
30
+ subjectHash?: string;
31
+ identityKeyVersion?: string;
32
+ subjectType?: SubjectType;
33
+ clientApplication?: string;
34
+ sourceTag?: string;
35
+ operationHash?: string;
36
+ }
37
+ export type EventSink = (events: readonly AnalyticsEvent[]) => Promise<void>;
38
+ /**
39
+ * Delivery and configuration signals. They never contain submitted values,
40
+ * identities, tool names, or response bodies: only a code, an event count, and
41
+ * an HTTP status where one was received.
42
+ */
43
+ export type DiagnosticCode = 'authentication_failed' | 'project_rejected' | 'delivery_rejected' | 'delivery_failed' | 'queue_full' | 'unknown_tool' | 'unknown_client' | 'invalid_context' | 'identity_unavailable' | 'closed';
44
+ export interface Diagnostic {
45
+ readonly code: DiagnosticCode;
46
+ readonly events: number;
47
+ readonly status?: number;
48
+ }
49
+ /** Context, or a resolver for it. A resolver may be async; it runs while the handler executes. */
50
+ export type ContextInput = EventContext | (() => EventContext | undefined | Promise<EventContext | undefined>);
51
+ interface AnalyticsOptions {
52
+ /** Falls back to the project in the connection string, then MCP_ANALYTICS_PROJECT_ID. */
53
+ projectId?: string;
54
+ /** Falls back to MCP_ANALYTICS_SERVER_ID, then `default`. Set it when one project has several servers. */
55
+ serverId?: string;
56
+ /** Falls back to MCP_ANALYTICS_ENVIRONMENT, then NODE_ENV, then `production`. */
57
+ environment?: string;
58
+ /**
59
+ * Falls back to MCP_ANALYTICS_IDENTITY_SECRET. Without a secret, subject and
60
+ * operation hashes are omitted and `identity_unavailable` is reported.
61
+ */
62
+ identitySecret?: string;
63
+ identityKeyVersion?: string;
64
+ /** Tool aliases; `observe()` adds registered tools automatically. */
65
+ tools?: readonly string[];
66
+ /**
67
+ * Client labels to record. Defaults to DEFAULT_CLIENTS. A configured label
68
+ * that matches a built-in family keeps that family's reported names.
69
+ */
70
+ clients?: readonly string[];
71
+ /** Maps observed client names (matched ignoring case) to configured clients; overrides built-in names. */
72
+ clientAliases?: Readonly<Record<string, string>>;
73
+ sources?: readonly string[];
74
+ maxQueueEvents?: number;
75
+ maxQueueBytes?: number;
76
+ batchSize?: number;
77
+ flushIntervalMs?: number;
78
+ maxRetries?: number;
79
+ retryDelayMs?: number;
80
+ maxRetryAgeMs?: number;
81
+ sinkTimeoutMs?: number;
82
+ flushTimeoutMs?: number;
83
+ now?: () => Date;
84
+ /**
85
+ * Receives delivery and configuration diagnostics asynchronously. Errors it
86
+ * throws are ignored. Without it, the SDK emits one process warning per
87
+ * configuration-class code; pass `() => {}` to silence them.
88
+ */
89
+ onDiagnostic?: (diagnostic: Diagnostic) => void;
90
+ /**
91
+ * Prints one stderr line per recorded event (kind, tool, outcome, duration,
92
+ * client, and a hash prefix or `-` for the user) and per delivery result.
93
+ * Never prints arguments, results, or identities. Falls back to MCP_ANALYTICS_DEBUG.
94
+ */
95
+ debug?: boolean;
96
+ /** Replaces process.env for the MCP_ANALYTICS_* fallbacks, mainly in tests. */
97
+ env?: Readonly<Record<string, string | undefined>>;
98
+ }
99
+ export type AnalyticsConfig = AnalyticsOptions & ({
100
+ /**
101
+ * Connection string `https://<write-key>@<host>[:port][/base-path]/<project-id>`,
102
+ * which supplies the endpoint, write key, and project. Falls back to
103
+ * MCP_ANALYTICS_URL. Explicit endpoint, writeKey, and projectId options win.
104
+ */
105
+ url?: string;
106
+ /** OTLP logs URL or base URL; `/v1/logs` is appended unless present. Falls back to the connection string, then MCP_ANALYTICS_ENDPOINT. */
107
+ endpoint?: string;
108
+ /** Falls back to the connection string, then MCP_ANALYTICS_WRITE_KEY. */
109
+ writeKey?: string;
110
+ requestTimeoutMs?: number;
111
+ sink?: never;
112
+ } | {
113
+ sink: EventSink;
114
+ url?: never;
115
+ endpoint?: never;
116
+ writeKey?: never;
117
+ requestTimeoutMs?: never;
118
+ });
119
+ /** Standard OTLP export, also available as an explicit sink for advanced use. */
120
+ export declare function createOtlpSink(options: OtlpSinkOptions): EventSink;
121
+ export interface AnalyticsStats {
122
+ queued: number;
123
+ queuedBytes: number;
124
+ sent: number;
125
+ dropped: number;
126
+ retries: number;
127
+ sinkErrors: number;
128
+ /** Tool names outside the `tools` allowlist, recorded as __unknown_tool__. */
129
+ unknownTools: number;
130
+ /** Client labels outside the `clients` allowlist, recorded as unknown. */
131
+ unknownClients: number;
132
+ }
133
+ export interface FlushOptions {
134
+ timeoutMs?: number;
135
+ }
136
+ export interface Analytics {
137
+ observeTool<T>(toolName: string, handler: () => T | Promise<T>, context?: ContextInput): Promise<T>;
138
+ recordMilestone(kind: 'authorization' | 'value_completed', context?: ContextInput, options?: {
139
+ toolName?: string;
140
+ }): void;
141
+ /**
142
+ * Adds static tool aliases to the allowlist, for example at registration.
143
+ * Never pass names received from clients.
144
+ */
145
+ allowTools(names: readonly string[]): void;
146
+ flush(options?: FlushOptions): Promise<void>;
147
+ close(options?: FlushOptions): Promise<void>;
148
+ getStats(): AnalyticsStats;
149
+ }
150
+ /** Returns a detached, immutable allowlisted event; rejection messages never echo data. */
151
+ export declare function validateEvent(value: unknown): AnalyticsEvent;
152
+ /** The tuple is hashed locally; the secret and original subject never enter an event. */
153
+ export declare function hashSubject(secret: string, projectId: string, subject: Subject): string;
154
+ /** Common spellings of an enabled flag; anything else, including unset, is off. */
155
+ export declare function enabledFlag(value: string | undefined): boolean;
156
+ /** Writes one diagnostic line to stderr, which stdio MCP servers keep free of protocol messages. */
157
+ export declare function debugLine(text: string): void;
158
+ interface InstanceInfo {
159
+ readonly projectId: string;
160
+ readonly serverId: string;
161
+ readonly environment: string;
162
+ readonly identity: boolean;
163
+ /** The resolved OTLP delivery; absent with a custom sink. */
164
+ readonly delivery?: {
165
+ readonly endpoint: string;
166
+ readonly writeKey: string;
167
+ };
168
+ }
169
+ /** @internal The project and whether identity hashing is enabled, for instances made by createAnalytics. */
170
+ export declare function instanceInfo(analytics: Analytics): InstanceInfo | undefined;
171
+ export declare function createAnalytics(config?: AnalyticsConfig): Analytics;
172
+ //# sourceMappingURL=analytics.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analytics.d.ts","sourceRoot":"","sources":["../src/analytics.ts"],"names":[],"mappings":"AAGA,OAAO,EAA+E,KAAK,eAAe,EAAE,MAAM,WAAW,CAAC;AAE9H,YAAY,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEjD,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;AACzD,MAAM,MAAM,OAAO,GAAG,IAAI,GAAG,YAAY,GAAG,WAAW,GAAG,gBAAgB,CAAC;AAC3E,MAAM,MAAM,SAAS,GAAG,WAAW,GAAG,eAAe,GAAG,iBAAiB,CAAC;AAE1E,MAAM,WAAW,OAAO;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,WAAW,CAAC;CACpB;AAED,MAAM,WAAW,YAAY;IAC3B,gFAAgF;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