@kometolabs/mcp-analytics-receiver 0.0.0-stage → 0.1.1
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.
- package/Dockerfile +47 -0
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +344 -2
- package/dist/admin.d.ts +17 -0
- package/dist/admin.js +264 -0
- package/dist/admin.js.map +1 -0
- package/dist/cli.d.ts +16 -0
- package/dist/cli.js +199 -0
- package/dist/cli.js.map +1 -0
- package/dist/deploy/compose.yaml +121 -0
- package/dist/deploy/grafana/bootstrap-viewer.mjs +216 -0
- package/dist/deploy/grafana/dashboards/overview.json +1108 -0
- package/dist/deploy/grafana/provisioning/dashboards/overview.yaml +12 -0
- package/dist/deploy/grafana/provisioning/datasources/postgres.yaml +22 -0
- package/dist/errors.d.ts +8 -0
- package/dist/errors.js +23 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +13 -0
- package/dist/http.js +127 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +234 -0
- package/dist/index.js.map +1 -0
- package/dist/init.d.ts +44 -0
- package/dist/init.js +198 -0
- package/dist/init.js.map +1 -0
- package/dist/postgres.d.ts +21 -0
- package/dist/postgres.js +110 -0
- package/dist/postgres.js.map +1 -0
- package/dist/rejections.d.ts +13 -0
- package/dist/rejections.js +59 -0
- package/dist/rejections.js.map +1 -0
- package/dist/schema.d.ts +44 -0
- package/dist/schema.js +88 -0
- package/dist/schema.js.map +1 -0
- package/dist/sql/001-schema.sql +285 -0
- package/dist/sql/002-roles.sql +26 -0
- package/package.json +70 -3
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
-- Run as the owner of the isolated analytics database, not the application writer.
|
|
2
|
+
-- Safe to rerun: it creates a new schema or converges an earlier alpha schema
|
|
3
|
+
-- without deleting events, then records its version in schema_info. Reapply the
|
|
4
|
+
-- roles file afterwards (`mcp-analytics-admin migrate` applies both together).
|
|
5
|
+
CREATE SCHEMA IF NOT EXISTS mcp_analytics;
|
|
6
|
+
REVOKE ALL ON SCHEMA mcp_analytics FROM PUBLIC;
|
|
7
|
+
|
|
8
|
+
-- Schema version 2. Version 1 was the first, unversioned alpha. Refuse to run
|
|
9
|
+
-- over a newer schema, whose changes this file would partly undo.
|
|
10
|
+
DO $version$
|
|
11
|
+
BEGIN
|
|
12
|
+
-- Nested: PL/pgSQL plans a whole condition, so the inner query must only run
|
|
13
|
+
-- once the table exists.
|
|
14
|
+
IF to_regclass('mcp_analytics.schema_info') IS NOT NULL THEN
|
|
15
|
+
IF EXISTS (SELECT 1 FROM mcp_analytics.schema_info WHERE version > 2) THEN
|
|
16
|
+
RAISE EXCEPTION 'The mcp_analytics schema is newer than this file (version 2); use the matching release';
|
|
17
|
+
END IF;
|
|
18
|
+
END IF;
|
|
19
|
+
END
|
|
20
|
+
$version$;
|
|
21
|
+
|
|
22
|
+
CREATE TABLE IF NOT EXISTS mcp_analytics.events (
|
|
23
|
+
schema_version integer NOT NULL CHECK (schema_version = 1),
|
|
24
|
+
event_id uuid NOT NULL,
|
|
25
|
+
project_id text NOT NULL CHECK (project_id ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
26
|
+
server_id text NOT NULL CHECK (server_id ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
27
|
+
environment text NOT NULL CHECK (environment ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
28
|
+
occurred_at timestamptz NOT NULL CHECK (isfinite(occurred_at)),
|
|
29
|
+
received_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
30
|
+
kind text NOT NULL CHECK (kind IN ('tool_call', 'authorization', 'value_completed')),
|
|
31
|
+
tool_name text CHECK (tool_name ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
32
|
+
outcome text CHECK (outcome IN ('ok', 'tool_error', 'exception', 'input_required')),
|
|
33
|
+
duration_ms double precision CHECK (duration_ms >= 0 AND duration_ms < 'Infinity'::double precision),
|
|
34
|
+
subject_hash text CHECK (subject_hash ~ '^[a-f0-9]{64}$'),
|
|
35
|
+
identity_key_version text CHECK (identity_key_version ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
36
|
+
subject_type text CHECK (subject_type IN ('user', 'account', 'service')),
|
|
37
|
+
client_application text NOT NULL DEFAULT 'unknown' CHECK (client_application ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
38
|
+
source_tag text CHECK (source_tag ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
39
|
+
operation_hash text CHECK (operation_hash ~ '^[a-f0-9]{64}$'),
|
|
40
|
+
PRIMARY KEY (project_id, event_id),
|
|
41
|
+
CHECK (
|
|
42
|
+
(kind = 'tool_call' AND tool_name IS NOT NULL AND outcome IS NOT NULL AND duration_ms IS NOT NULL)
|
|
43
|
+
OR (kind <> 'tool_call' AND outcome IS NULL AND duration_ms IS NULL)
|
|
44
|
+
),
|
|
45
|
+
CHECK (
|
|
46
|
+
(subject_hash IS NULL AND identity_key_version IS NULL AND subject_type IS NULL)
|
|
47
|
+
OR (subject_hash IS NOT NULL AND identity_key_version IS NOT NULL AND subject_type IS NOT NULL)
|
|
48
|
+
)
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
CREATE INDEX IF NOT EXISTS events_project_time ON mcp_analytics.events (project_id, server_id, environment, occurred_at);
|
|
52
|
+
CREATE INDEX IF NOT EXISTS events_tool_time ON mcp_analytics.events (project_id, server_id, environment, tool_name, occurred_at) WHERE kind = 'tool_call';
|
|
53
|
+
CREATE INDEX IF NOT EXISTS events_subject_time ON mcp_analytics.events (project_id, server_id, environment, subject_hash, identity_key_version, subject_type, occurred_at) WHERE subject_hash IS NOT NULL;
|
|
54
|
+
|
|
55
|
+
-- Treat the value definition as immutable within a dataset in the alpha. Use a
|
|
56
|
+
-- new project/server/environment key when choosing a different definition.
|
|
57
|
+
-- There is no default value action: a dataset without a row reports value
|
|
58
|
+
-- metrics as unavailable. existing_user_days labels tool-call cohort entries
|
|
59
|
+
-- that begin within that many days of the start of retained identified history
|
|
60
|
+
-- as existing users rather than new first-observed cohorts.
|
|
61
|
+
CREATE TABLE IF NOT EXISTS mcp_analytics.project_settings (
|
|
62
|
+
project_id text NOT NULL CHECK (project_id ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
63
|
+
server_id text NOT NULL CHECK (server_id ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
64
|
+
environment text NOT NULL CHECK (environment ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
65
|
+
value_tool text CHECK (value_tool ~ '^[A-Za-z0-9_][A-Za-z0-9_.-]{0,63}$'),
|
|
66
|
+
value_mode text NOT NULL DEFAULT 'tool' CHECK (value_mode IN ('tool', 'application')),
|
|
67
|
+
existing_user_days integer NOT NULL DEFAULT 14 CHECK (existing_user_days BETWEEN 0 AND 365),
|
|
68
|
+
PRIMARY KEY (project_id, server_id, environment),
|
|
69
|
+
CONSTRAINT project_settings_value_definition CHECK (value_mode = 'application' OR value_tool IS NOT NULL)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
-- Converge an earlier alpha table: remove the demo value-action default and add
|
|
73
|
+
-- the existing-user window. Existing rows keep their explicit definitions.
|
|
74
|
+
ALTER TABLE mcp_analytics.project_settings
|
|
75
|
+
ALTER COLUMN value_tool DROP DEFAULT,
|
|
76
|
+
ALTER COLUMN value_tool DROP NOT NULL,
|
|
77
|
+
ADD COLUMN IF NOT EXISTS existing_user_days integer NOT NULL DEFAULT 14
|
|
78
|
+
CHECK (existing_user_days BETWEEN 0 AND 365);
|
|
79
|
+
DO $migration$
|
|
80
|
+
BEGIN
|
|
81
|
+
IF NOT EXISTS (
|
|
82
|
+
SELECT 1 FROM pg_catalog.pg_constraint
|
|
83
|
+
WHERE conrelid = 'mcp_analytics.project_settings'::regclass
|
|
84
|
+
AND conname = 'project_settings_value_definition'
|
|
85
|
+
) THEN
|
|
86
|
+
ALTER TABLE mcp_analytics.project_settings ADD CONSTRAINT project_settings_value_definition
|
|
87
|
+
CHECK (value_mode = 'application' OR value_tool IS NOT NULL);
|
|
88
|
+
END IF;
|
|
89
|
+
END
|
|
90
|
+
$migration$;
|
|
91
|
+
|
|
92
|
+
-- Deliberately list approved columns rather than exposing future table fields.
|
|
93
|
+
CREATE OR REPLACE VIEW mcp_analytics.grafana_events WITH (security_barrier = true) AS
|
|
94
|
+
SELECT schema_version, event_id, project_id, server_id, environment, occurred_at,
|
|
95
|
+
received_at, kind, tool_name, outcome, duration_ms, subject_hash,
|
|
96
|
+
identity_key_version, subject_type, client_application, source_tag, operation_hash
|
|
97
|
+
FROM mcp_analytics.events;
|
|
98
|
+
|
|
99
|
+
-- Lets the report state which value definition is in use.
|
|
100
|
+
CREATE OR REPLACE VIEW mcp_analytics.grafana_settings WITH (security_barrier = true) AS
|
|
101
|
+
SELECT project_id, server_id, environment, value_mode, value_tool, existing_user_days
|
|
102
|
+
FROM mcp_analytics.project_settings;
|
|
103
|
+
|
|
104
|
+
-- Define and restrict the functions atomically, including when this file is run
|
|
105
|
+
-- by psql without an outer transaction. Only the approved cohort columns leave
|
|
106
|
+
-- these owner-executed functions; callers retain no access to the base tables.
|
|
107
|
+
DO $migration$
|
|
108
|
+
DECLARE
|
|
109
|
+
procedure regprocedure;
|
|
110
|
+
BEGIN
|
|
111
|
+
-- CREATE OR REPLACE cannot change a result type. Recreate an earlier alpha
|
|
112
|
+
-- function together with its dependent view; the roles file restores grants.
|
|
113
|
+
FOREACH procedure IN ARRAY ARRAY[
|
|
114
|
+
to_regprocedure('mcp_analytics.subject_cohorts_at(timestamptz)'),
|
|
115
|
+
to_regprocedure('mcp_analytics.subject_cohorts_at(timestamptz, text[], text[], text[], timestamptz)')
|
|
116
|
+
] LOOP
|
|
117
|
+
IF procedure IS NOT NULL AND pg_catalog.pg_get_function_result(procedure) <> 'TABLE(project_id text, '
|
|
118
|
+
'server_id text, environment text, subject_hash text, identity_key_version text, subject_type text, '
|
|
119
|
+
'cohort_started_at timestamp with time zone, cohort_kind text, client_application text, source_tag text, '
|
|
120
|
+
'first_tool_at timestamp with time zone, activated_at timestamp with time zone, '
|
|
121
|
+
'returned_week_two_at timestamp with time zone, returned_week_two boolean, value_configured boolean)' THEN
|
|
122
|
+
DROP VIEW IF EXISTS mcp_analytics.subject_cohorts;
|
|
123
|
+
EXECUTE format('DROP FUNCTION %s', procedure);
|
|
124
|
+
END IF;
|
|
125
|
+
END LOOP;
|
|
126
|
+
-- The canonical cohort logic. Dashboards pass their selected datasets and
|
|
127
|
+
-- range start, so unrelated datasets never enter the entry scan and only
|
|
128
|
+
-- cohorts starting in the range get activation and return lookups. Every
|
|
129
|
+
-- filter is a required plain = ANY so it can use the event indexes; an
|
|
130
|
+
-- optional (NULL) filter would stop the planner from using them.
|
|
131
|
+
EXECUTE $definition$
|
|
132
|
+
CREATE OR REPLACE FUNCTION mcp_analytics.subject_cohorts_at(
|
|
133
|
+
cutoff_at timestamptz, projects text[], servers text[], environments text[], started_from timestamptz
|
|
134
|
+
)
|
|
135
|
+
RETURNS TABLE (
|
|
136
|
+
project_id text, server_id text, environment text, subject_hash text,
|
|
137
|
+
identity_key_version text, subject_type text, cohort_started_at timestamptz,
|
|
138
|
+
cohort_kind text, client_application text, source_tag text,
|
|
139
|
+
first_tool_at timestamptz, activated_at timestamptz,
|
|
140
|
+
returned_week_two_at timestamptz, returned_week_two boolean,
|
|
141
|
+
value_configured boolean
|
|
142
|
+
)
|
|
143
|
+
LANGUAGE sql STABLE STRICT SECURITY DEFINER
|
|
144
|
+
SET search_path = pg_catalog, pg_temp
|
|
145
|
+
AS $function$
|
|
146
|
+
WITH first_seen AS (
|
|
147
|
+
-- The earliest retained authorization or tool call starts the cohort. A hash
|
|
148
|
+
-- aggregate finds its timestamp without sorting every event by text keys.
|
|
149
|
+
SELECT project_id, server_id, environment, subject_hash, identity_key_version, subject_type,
|
|
150
|
+
MIN(occurred_at) AS cohort_started_at
|
|
151
|
+
FROM mcp_analytics.events
|
|
152
|
+
WHERE subject_hash IS NOT NULL AND kind IN ('authorization', 'tool_call')
|
|
153
|
+
AND occurred_at < cutoff_at
|
|
154
|
+
-- Entry and history are computed within a dataset, so dataset filters
|
|
155
|
+
-- change which cohorts are returned, never how they are classified.
|
|
156
|
+
AND project_id = ANY (projects) AND server_id = ANY (servers)
|
|
157
|
+
AND environment = ANY (environments)
|
|
158
|
+
GROUP BY project_id, server_id, environment, subject_hash, identity_key_version, subject_type
|
|
159
|
+
), entry AS (
|
|
160
|
+
-- On equal timestamps an authorization wins, then the lowest event ID, so a
|
|
161
|
+
-- later grant never re-dates a cohort and the entry labels are deterministic.
|
|
162
|
+
SELECT f.*, pick.entry_kind, pick.client_application, pick.source_tag
|
|
163
|
+
FROM first_seen f
|
|
164
|
+
CROSS JOIN LATERAL (
|
|
165
|
+
SELECT t.kind AS entry_kind, t.client_application, t.source_tag
|
|
166
|
+
FROM mcp_analytics.events t
|
|
167
|
+
WHERE t.project_id = f.project_id AND t.server_id = f.server_id AND t.environment = f.environment
|
|
168
|
+
AND t.subject_hash = f.subject_hash AND t.identity_key_version = f.identity_key_version
|
|
169
|
+
AND t.subject_type = f.subject_type AND t.occurred_at = f.cohort_started_at
|
|
170
|
+
AND t.kind IN ('authorization', 'tool_call')
|
|
171
|
+
ORDER BY CASE WHEN t.kind = 'authorization' THEN 0 ELSE 1 END, t.event_id
|
|
172
|
+
LIMIT 1
|
|
173
|
+
) pick
|
|
174
|
+
), history AS (
|
|
175
|
+
-- Retained identified history starts at the earliest entry in each identity
|
|
176
|
+
-- scope. It moves after retention purges and restarts after key rotation.
|
|
177
|
+
-- Entries before started_from still count here.
|
|
178
|
+
SELECT project_id, server_id, environment, identity_key_version,
|
|
179
|
+
MIN(cohort_started_at) AS history_started_at
|
|
180
|
+
FROM entry
|
|
181
|
+
GROUP BY project_id, server_id, environment, identity_key_version
|
|
182
|
+
), cohort AS (
|
|
183
|
+
SELECT e.project_id, e.server_id, e.environment, e.subject_hash, e.identity_key_version,
|
|
184
|
+
e.subject_type, e.cohort_started_at, e.client_application, e.source_tag,
|
|
185
|
+
CASE
|
|
186
|
+
WHEN e.entry_kind = 'authorization' THEN 'authorization'
|
|
187
|
+
-- Users active since installation or since the retained history began
|
|
188
|
+
-- are an existing population, not newly observed users.
|
|
189
|
+
WHEN e.cohort_started_at < h.history_started_at
|
|
190
|
+
+ COALESCE(s.existing_user_days, 14) * INTERVAL '1 day' THEN 'existing'
|
|
191
|
+
ELSE 'first_observed'
|
|
192
|
+
END AS cohort_kind,
|
|
193
|
+
s.value_tool, s.value_mode, (s.project_id IS NOT NULL) AS value_configured
|
|
194
|
+
FROM entry e
|
|
195
|
+
JOIN history h USING (project_id, server_id, environment, identity_key_version)
|
|
196
|
+
LEFT JOIN mcp_analytics.project_settings s
|
|
197
|
+
ON s.project_id = e.project_id AND s.server_id = e.server_id AND s.environment = e.environment
|
|
198
|
+
WHERE e.cohort_started_at >= started_from
|
|
199
|
+
)
|
|
200
|
+
SELECT c.project_id, c.server_id, c.environment, c.subject_hash, c.identity_key_version,
|
|
201
|
+
c.subject_type, c.cohort_started_at, c.cohort_kind, c.client_application, c.source_tag,
|
|
202
|
+
first_tool.first_tool_at, activation.activated_at,
|
|
203
|
+
repeat_use.returned_week_two_at,
|
|
204
|
+
(repeat_use.returned_week_two_at IS NOT NULL) AS returned_week_two,
|
|
205
|
+
c.value_configured
|
|
206
|
+
FROM cohort c
|
|
207
|
+
LEFT JOIN LATERAL (
|
|
208
|
+
SELECT MIN(t.occurred_at) AS first_tool_at
|
|
209
|
+
FROM mcp_analytics.events t
|
|
210
|
+
WHERE t.project_id = c.project_id AND t.server_id = c.server_id AND t.environment = c.environment
|
|
211
|
+
AND t.subject_hash = c.subject_hash AND t.identity_key_version = c.identity_key_version
|
|
212
|
+
AND t.subject_type = c.subject_type AND t.kind = 'tool_call'
|
|
213
|
+
AND t.occurred_at >= c.cohort_started_at
|
|
214
|
+
AND t.occurred_at < cutoff_at
|
|
215
|
+
) first_tool ON true
|
|
216
|
+
-- An unconfigured dataset has no value definition and never activates.
|
|
217
|
+
LEFT JOIN LATERAL (
|
|
218
|
+
SELECT MIN(v.occurred_at) AS activated_at
|
|
219
|
+
FROM mcp_analytics.events v
|
|
220
|
+
WHERE v.project_id = c.project_id AND v.server_id = c.server_id AND v.environment = c.environment
|
|
221
|
+
AND v.subject_hash = c.subject_hash AND v.identity_key_version = c.identity_key_version
|
|
222
|
+
AND v.subject_type = c.subject_type
|
|
223
|
+
AND v.occurred_at >= c.cohort_started_at AND v.occurred_at < c.cohort_started_at + INTERVAL '7 days'
|
|
224
|
+
AND v.occurred_at < cutoff_at
|
|
225
|
+
AND ((c.value_mode = 'tool' AND v.kind = 'tool_call' AND v.tool_name = c.value_tool AND v.outcome = 'ok')
|
|
226
|
+
OR (c.value_mode = 'application' AND v.kind = 'value_completed'))
|
|
227
|
+
) activation ON true
|
|
228
|
+
LEFT JOIN LATERAL (
|
|
229
|
+
SELECT MIN(v.occurred_at) AS returned_week_two_at
|
|
230
|
+
FROM mcp_analytics.events v
|
|
231
|
+
WHERE v.project_id = c.project_id AND v.server_id = c.server_id AND v.environment = c.environment
|
|
232
|
+
AND v.subject_hash = c.subject_hash AND v.identity_key_version = c.identity_key_version
|
|
233
|
+
AND v.subject_type = c.subject_type
|
|
234
|
+
AND v.occurred_at >= activation.activated_at + INTERVAL '7 days'
|
|
235
|
+
AND v.occurred_at < activation.activated_at + INTERVAL '14 days'
|
|
236
|
+
AND v.occurred_at < cutoff_at
|
|
237
|
+
AND ((c.value_mode = 'tool' AND v.kind = 'tool_call' AND v.tool_name = c.value_tool AND v.outcome = 'ok')
|
|
238
|
+
OR (c.value_mode = 'application' AND v.kind = 'value_completed'))
|
|
239
|
+
) repeat_use ON true;
|
|
240
|
+
$function$
|
|
241
|
+
$definition$;
|
|
242
|
+
REVOKE ALL ON FUNCTION mcp_analytics.subject_cohorts_at(timestamptz, text[], text[], text[], timestamptz) FROM PUBLIC;
|
|
243
|
+
-- Kept for compatibility: every dataset and cohort at the cutoff.
|
|
244
|
+
EXECUTE $definition$
|
|
245
|
+
CREATE OR REPLACE FUNCTION mcp_analytics.subject_cohorts_at(cutoff_at timestamptz)
|
|
246
|
+
RETURNS TABLE (
|
|
247
|
+
project_id text, server_id text, environment text, subject_hash text,
|
|
248
|
+
identity_key_version text, subject_type text, cohort_started_at timestamptz,
|
|
249
|
+
cohort_kind text, client_application text, source_tag text,
|
|
250
|
+
first_tool_at timestamptz, activated_at timestamptz,
|
|
251
|
+
returned_week_two_at timestamptz, returned_week_two boolean,
|
|
252
|
+
value_configured boolean
|
|
253
|
+
)
|
|
254
|
+
LANGUAGE sql STABLE STRICT SECURITY DEFINER
|
|
255
|
+
SET search_path = pg_catalog, pg_temp
|
|
256
|
+
AS $function$
|
|
257
|
+
SELECT c.*
|
|
258
|
+
FROM (
|
|
259
|
+
SELECT array_agg(DISTINCT project_id) AS projects, array_agg(DISTINCT server_id) AS servers,
|
|
260
|
+
array_agg(DISTINCT environment) AS environments
|
|
261
|
+
FROM mcp_analytics.events
|
|
262
|
+
WHERE subject_hash IS NOT NULL
|
|
263
|
+
) every_dataset
|
|
264
|
+
CROSS JOIN LATERAL mcp_analytics.subject_cohorts_at(cutoff_at, every_dataset.projects,
|
|
265
|
+
every_dataset.servers, every_dataset.environments, '-infinity'::timestamptz) c;
|
|
266
|
+
$function$
|
|
267
|
+
$definition$;
|
|
268
|
+
REVOKE ALL ON FUNCTION mcp_analytics.subject_cohorts_at(timestamptz) FROM PUBLIC;
|
|
269
|
+
-- Keep the existing current-state view/API. Historical reports call the same
|
|
270
|
+
-- canonical logic with their selected exclusive end time.
|
|
271
|
+
CREATE OR REPLACE VIEW mcp_analytics.subject_cohorts WITH (security_barrier = true) AS
|
|
272
|
+
SELECT * FROM mcp_analytics.subject_cohorts_at(CURRENT_TIMESTAMP);
|
|
273
|
+
END
|
|
274
|
+
$migration$;
|
|
275
|
+
|
|
276
|
+
-- One row: the version this file converged the schema to.
|
|
277
|
+
CREATE TABLE IF NOT EXISTS mcp_analytics.schema_info (
|
|
278
|
+
id boolean PRIMARY KEY DEFAULT true CHECK (id),
|
|
279
|
+
version integer NOT NULL CHECK (version > 0),
|
|
280
|
+
updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
281
|
+
);
|
|
282
|
+
INSERT INTO mcp_analytics.schema_info (id, version) VALUES (true, 2)
|
|
283
|
+
ON CONFLICT (id) DO UPDATE SET version = EXCLUDED.version, updated_at = CURRENT_TIMESTAMP;
|
|
284
|
+
|
|
285
|
+
REVOKE ALL ON ALL TABLES IN SCHEMA mcp_analytics FROM PUBLIC;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
-- The writer and reader roles and their grants. Run as the database owner after
|
|
2
|
+
-- 001-schema.sql; safe to rerun. Roles are created NOLOGIN: the operator enables
|
|
3
|
+
-- LOGIN and sets passwords outside this file (the demo's init script does so).
|
|
4
|
+
-- Never commit credentials here.
|
|
5
|
+
DO $$
|
|
6
|
+
BEGIN
|
|
7
|
+
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'mcp_writer') THEN
|
|
8
|
+
CREATE ROLE mcp_writer NOLOGIN;
|
|
9
|
+
END IF;
|
|
10
|
+
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'mcp_reader') THEN
|
|
11
|
+
CREATE ROLE mcp_reader NOLOGIN;
|
|
12
|
+
END IF;
|
|
13
|
+
END
|
|
14
|
+
$$;
|
|
15
|
+
|
|
16
|
+
GRANT USAGE ON SCHEMA mcp_analytics TO mcp_writer, mcp_reader;
|
|
17
|
+
REVOKE ALL ON ALL TABLES IN SCHEMA mcp_analytics FROM mcp_writer, mcp_reader;
|
|
18
|
+
GRANT INSERT ON mcp_analytics.events TO mcp_writer;
|
|
19
|
+
-- PostgreSQL checks the conflict-target columns for ON CONFLICT as well.
|
|
20
|
+
GRANT SELECT (project_id, event_id) ON mcp_analytics.events TO mcp_writer;
|
|
21
|
+
GRANT SELECT ON mcp_analytics.grafana_events, mcp_analytics.grafana_settings,
|
|
22
|
+
mcp_analytics.subject_cohorts TO mcp_reader;
|
|
23
|
+
REVOKE ALL ON FUNCTION mcp_analytics.subject_cohorts_at(timestamptz),
|
|
24
|
+
mcp_analytics.subject_cohorts_at(timestamptz, text[], text[], text[], timestamptz) FROM mcp_writer, mcp_reader;
|
|
25
|
+
GRANT EXECUTE ON FUNCTION mcp_analytics.subject_cohorts_at(timestamptz),
|
|
26
|
+
mcp_analytics.subject_cohorts_at(timestamptz, text[], text[], text[], timestamptz) TO mcp_reader;
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kometolabs/mcp-analytics-receiver",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Self-hosted MCP analytics backend: OTLP HTTP receiver, PostgreSQL storage and schema, admin CLI, and deployment setup",
|
|
5
|
+
"author": "Kometo Labs (https://kometo.co)",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"mcp",
|
|
9
|
+
"model-context-protocol",
|
|
10
|
+
"analytics",
|
|
11
|
+
"otlp",
|
|
12
|
+
"receiver",
|
|
13
|
+
"postgresql",
|
|
14
|
+
"grafana",
|
|
15
|
+
"self-hosted"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://github.com/kometolabs/mcp-analytics#readme",
|
|
18
|
+
"bugs": "https://github.com/kometolabs/mcp-analytics/issues",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/kometolabs/mcp-analytics.git",
|
|
22
|
+
"directory": "packages/receiver"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"provenance": true
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"bin": {
|
|
32
|
+
"mcp-analytics-receiver": "./dist/cli.js",
|
|
33
|
+
"mcp-analytics-admin": "./dist/admin.js"
|
|
34
|
+
},
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"import": "./dist/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./admin": {
|
|
41
|
+
"types": "./dist/admin.d.ts",
|
|
42
|
+
"import": "./dist/admin.js"
|
|
43
|
+
},
|
|
44
|
+
"./postgres": {
|
|
45
|
+
"types": "./dist/postgres.d.ts",
|
|
46
|
+
"import": "./dist/postgres.js"
|
|
47
|
+
},
|
|
48
|
+
"./schema.sql": "./dist/sql/001-schema.sql",
|
|
49
|
+
"./roles.sql": "./dist/sql/002-roles.sql"
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"README.md",
|
|
54
|
+
"Dockerfile",
|
|
55
|
+
"LICENSE",
|
|
56
|
+
"NOTICE"
|
|
57
|
+
],
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=22.18"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@kometolabs/mcp-analytics": "0.1.1",
|
|
63
|
+
"pg": "8.21.0"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@types/pg": "8.21.0"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "tsc -p tsconfig.json && node scripts/copy-assets.mjs",
|
|
70
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
71
|
+
"test": "node test/receiver.test.mjs && node test/admin.test.mjs && node test/postgres.test.mjs && node test/sql.test.mjs && node test/init.test.mjs"
|
|
72
|
+
}
|
|
6
73
|
}
|