@lovable.dev/mcp-js 0.15.0 → 0.15.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.
@@ -1,18 +1,16 @@
1
- import {
2
- createRecorderForRuntime,
3
- nowMs
4
- } from "./chunk-XQSTN54Y.js";
5
1
  import {
6
2
  ToolContext
7
- } from "./chunk-P3F324UC.js";
3
+ } from "./chunk-MA5H6PSF.js";
8
4
  import {
9
5
  JSON_HEADERS,
10
6
  assertRestResourceBinding,
11
7
  corsPreflightResponse,
8
+ createRecorderForRuntime,
12
9
  createRequestAuthorizer,
13
10
  methodNotAllowed,
11
+ nowMs,
14
12
  withCors
15
- } from "./chunk-G4XA7IJM.js";
13
+ } from "./chunk-YTOMGJV5.js";
16
14
 
17
15
  // src/protocols/rest/invoke-tool.ts
18
16
  import { getParseErrorMessage, objectFromShape, safeParseAsync } from "@modelcontextprotocol/sdk/server/zod-compat.js";
@@ -30,7 +28,7 @@ function isEmptyArgs(value) {
30
28
  }
31
29
  function createInvokeToolHandler(mcp, options = {}, recorder = createRecorderForRuntime(mcp)) {
32
30
  assertRestResourceBinding(mcp, options);
33
- const authorizer = createRequestAuthorizer(mcp, options);
31
+ const authorizer = createRequestAuthorizer(mcp, options, recorder);
34
32
  const handle = async (request, toolName) => {
35
33
  const authResult = await authorizer.authorize(request);
36
34
  if (!authResult.ok)
@@ -42,6 +42,38 @@ function describeError(err) {
42
42
  return { value: String(err) };
43
43
  }
44
44
 
45
+ // src/metrics/config.ts
46
+ var DEFAULT_METRICS_ENDPOINT = "https://api.lovable.dev/v1/app-mcp-usage";
47
+ var METRICS_API_KEY_ENV_VAR = "LOVABLE_API_KEY";
48
+ var METRICS_SAMPLE_RATE = 1;
49
+ var METRICS_FLUSH_INTERVAL_MS = 5e3;
50
+ var METRICS_MAX_BATCH_SIZE = 50;
51
+ function assertMetricsEndpoint(endpoint) {
52
+ let url;
53
+ try {
54
+ url = new URL(endpoint);
55
+ } catch {
56
+ throw new Error(`@lovable.dev/mcp-js: metrics.endpoint must be an absolute URL, got ${JSON.stringify(endpoint)}`);
57
+ }
58
+ if (url.protocol !== "https:" && url.protocol !== "http:") {
59
+ throw new Error(`@lovable.dev/mcp-js: metrics.endpoint must be http(s), got ${JSON.stringify(endpoint)}`);
60
+ }
61
+ }
62
+ function resolveMetricsConfig(config = true) {
63
+ const options = typeof config === "boolean" ? { enabled: config } : config;
64
+ const endpoint = options.endpoint ?? DEFAULT_METRICS_ENDPOINT;
65
+ assertMetricsEndpoint(endpoint);
66
+ return Object.freeze({
67
+ enabled: options.enabled ?? true,
68
+ endpoint,
69
+ headers: options.headers ?? {},
70
+ apiKeyEnvVar: METRICS_API_KEY_ENV_VAR,
71
+ sampleRate: METRICS_SAMPLE_RATE,
72
+ flushIntervalMs: METRICS_FLUSH_INTERVAL_MS,
73
+ maxBatchSize: METRICS_MAX_BATCH_SIZE
74
+ });
75
+ }
76
+
45
77
  // src/core/url.ts
46
78
  function trimTrailingSlash(value) {
47
79
  return value.replace(/\/+$/, "");
@@ -79,6 +111,8 @@ function parseSafeUrl(subject, raw, ErrorClass = Error) {
79
111
  }
80
112
 
81
113
  export {
114
+ DEFAULT_METRICS_ENDPOINT,
115
+ resolveMetricsConfig,
82
116
  trimTrailingSlash,
83
117
  parseSafeUrl,
84
118
  setLogLevel,
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "0.15.0";
2
+ var version = "0.15.1";
3
3
 
4
4
  export {
5
5
  version
@@ -7,11 +7,11 @@ import {
7
7
  oauthConfigurationErrorResponse,
8
8
  resolveProtectedResource,
9
9
  withCors
10
- } from "./chunk-G4XA7IJM.js";
10
+ } from "./chunk-YTOMGJV5.js";
11
11
  import {
12
12
  describeError,
13
13
  log
14
- } from "./chunk-QC3DXQTH.js";
14
+ } from "./chunk-DCWYK4CA.js";
15
15
 
16
16
  // src/protocols/oauth-metadata.ts
17
17
  function notFound() {
@@ -41,40 +41,6 @@ var ToolContext = class {
41
41
  }
42
42
  };
43
43
 
44
- // src/metrics/config.ts
45
- var DEFAULT_METRICS_ENDPOINT = "https://api.lovable.dev/v1/app-mcp-usage";
46
- var METRICS_API_KEY_ENV_VAR = "LOVABLE_API_KEY";
47
- var METRICS_SAMPLE_RATE = 1;
48
- var METRICS_FLUSH_INTERVAL_MS = 5e3;
49
- var METRICS_MAX_BATCH_SIZE = 50;
50
- function assertMetricsEndpoint(endpoint) {
51
- let url;
52
- try {
53
- url = new URL(endpoint);
54
- } catch {
55
- throw new Error(`@lovable.dev/mcp-js: metrics.endpoint must be an absolute URL, got ${JSON.stringify(endpoint)}`);
56
- }
57
- if (url.protocol !== "https:" && url.protocol !== "http:") {
58
- throw new Error(`@lovable.dev/mcp-js: metrics.endpoint must be http(s), got ${JSON.stringify(endpoint)}`);
59
- }
60
- }
61
- function resolveMetricsConfig(config = true) {
62
- const options = typeof config === "boolean" ? { enabled: config } : config;
63
- const endpoint = options.endpoint ?? DEFAULT_METRICS_ENDPOINT;
64
- assertMetricsEndpoint(endpoint);
65
- return Object.freeze({
66
- enabled: options.enabled ?? true,
67
- endpoint,
68
- headers: options.headers ?? {},
69
- apiKeyEnvVar: METRICS_API_KEY_ENV_VAR,
70
- sampleRate: METRICS_SAMPLE_RATE,
71
- flushIntervalMs: METRICS_FLUSH_INTERVAL_MS,
72
- maxBatchSize: METRICS_MAX_BATCH_SIZE
73
- });
74
- }
75
-
76
44
  export {
77
- DEFAULT_METRICS_ENDPOINT,
78
- resolveMetricsConfig,
79
45
  ToolContext
80
46
  };
@@ -1,11 +1,12 @@
1
1
  import {
2
2
  assertRestResourceBinding,
3
3
  corsPreflightResponse,
4
+ createRecorderForRuntime,
4
5
  createRequestAuthorizer,
5
6
  headResponse,
6
7
  methodNotAllowed,
7
8
  withCors
8
- } from "./chunk-G4XA7IJM.js";
9
+ } from "./chunk-YTOMGJV5.js";
9
10
 
10
11
  // src/protocols/rest/list-tools.ts
11
12
  import { objectFromShape } from "@modelcontextprotocol/sdk/server/zod-compat.js";
@@ -32,9 +33,9 @@ function buildMcpListing(mcp) {
32
33
  }))
33
34
  };
34
35
  }
35
- function createListToolsHandler(mcp, options = {}) {
36
+ function createListToolsHandler(mcp, options = {}, recorder = createRecorderForRuntime(mcp)) {
36
37
  assertRestResourceBinding(mcp, options);
37
- const authorizer = createRequestAuthorizer(mcp, options);
38
+ const authorizer = createRequestAuthorizer(mcp, options, recorder);
38
39
  const handle = async (request) => {
39
40
  const authResult = await authorizer.authorize(request);
40
41
  if (!authResult.ok)
@@ -1,19 +1,17 @@
1
- import {
2
- createRecorderForRuntime,
3
- nowMs
4
- } from "./chunk-XQSTN54Y.js";
5
1
  import {
6
2
  ToolContext
7
- } from "./chunk-P3F324UC.js";
3
+ } from "./chunk-MA5H6PSF.js";
8
4
  import {
9
5
  corsPreflightResponse,
6
+ createRecorderForRuntime,
10
7
  createRequestAuthorizer,
8
+ nowMs,
11
9
  withCors
12
- } from "./chunk-G4XA7IJM.js";
10
+ } from "./chunk-YTOMGJV5.js";
13
11
  import {
14
12
  describeError,
15
13
  log
16
- } from "./chunk-QC3DXQTH.js";
14
+ } from "./chunk-DCWYK4CA.js";
17
15
 
18
16
  // src/protocols/mcp/protocol.ts
19
17
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -43,7 +41,7 @@ function adaptToolToSdkCallback(tool, auth, recorder) {
43
41
  };
44
42
  }
45
43
  function createMcpProtocolHandler(mcp, options = {}, recorder = createRecorderForRuntime(mcp)) {
46
- const authorizer = createRequestAuthorizer(mcp, options);
44
+ const authorizer = createRequestAuthorizer(mcp, options, recorder);
47
45
  const handle = async (request) => {
48
46
  const authResult = await authorizer.authorize(request);
49
47
  if (!authResult.ok)
@@ -1,12 +1,17 @@
1
1
  import {
2
+ DEFAULT_METRICS_ENDPOINT,
2
3
  describeError,
3
4
  log,
4
5
  parseSafeUrl,
6
+ resolveMetricsConfig,
5
7
  trimTrailingSlash
6
- } from "./chunk-QC3DXQTH.js";
8
+ } from "./chunk-DCWYK4CA.js";
7
9
  import {
8
10
  OAUTH_PROTECTED_RESOURCE_METADATA_PATH
9
11
  } from "./chunk-6DXGZZA4.js";
12
+ import {
13
+ version
14
+ } from "./chunk-GIHCQT6L.js";
10
15
 
11
16
  // src/core/http.ts
12
17
  var JSON_HEADERS = { "Content-Type": "application/json" };
@@ -44,6 +49,179 @@ function resolveResourcePath(resourcePath, request) {
44
49
  return resourcePath;
45
50
  }
46
51
 
52
+ // src/metrics/otlp.ts
53
+ var SCOPE_NAME = "@lovable.dev/mcp-js";
54
+ var EVENT_NAME = "mcp.tool.invocation";
55
+ var SEVERITY_INFO = 9;
56
+ function strAttr(key, value) {
57
+ return { key, value: { stringValue: value } };
58
+ }
59
+ function intAttr(key, value) {
60
+ return { key, value: { intValue: String(Math.round(value)) } };
61
+ }
62
+ function toLogRecord(rec) {
63
+ const attributes = [
64
+ strAttr("event.name", EVENT_NAME),
65
+ strAttr("mcp.method", rec.method),
66
+ strAttr("mcp.outcome", rec.outcome),
67
+ intAttr("mcp.duration_ms", rec.durationMs)
68
+ ];
69
+ if (rec.tool !== null)
70
+ attributes.push(strAttr("mcp.tool", rec.tool));
71
+ if (rec.reqBytes !== void 0)
72
+ attributes.push(intAttr("mcp.request_size_bytes", rec.reqBytes));
73
+ if (rec.resBytes !== void 0)
74
+ attributes.push(intAttr("mcp.response_size_bytes", rec.resBytes));
75
+ return {
76
+ timeUnixNano: rec.timeUnixNano,
77
+ observedTimeUnixNano: rec.timeUnixNano,
78
+ severityNumber: SEVERITY_INFO,
79
+ severityText: "INFO",
80
+ body: { stringValue: EVENT_NAME },
81
+ attributes
82
+ };
83
+ }
84
+ function buildLogsPayload(records, server) {
85
+ return JSON.stringify({
86
+ resourceLogs: [
87
+ {
88
+ resource: {
89
+ attributes: [
90
+ strAttr("service.name", server.name),
91
+ strAttr("service.version", server.version),
92
+ strAttr("telemetry.sdk.name", SCOPE_NAME),
93
+ strAttr("telemetry.sdk.version", version),
94
+ strAttr("telemetry.sdk.language", "webjs")
95
+ ]
96
+ },
97
+ scopeLogs: [
98
+ {
99
+ scope: { name: SCOPE_NAME, version },
100
+ logRecords: records.map(toLogRecord)
101
+ }
102
+ ]
103
+ }
104
+ ]
105
+ });
106
+ }
107
+ function nowUnixNano() {
108
+ return `${Date.now()}000000`;
109
+ }
110
+
111
+ // src/metrics/recorder.ts
112
+ function nowMs() {
113
+ return typeof performance !== "undefined" && typeof performance.now === "function" ? performance.now() : Date.now();
114
+ }
115
+ var NOOP_RECORDER = {
116
+ record() {
117
+ },
118
+ async flush() {
119
+ }
120
+ };
121
+ function createNoopRecorder() {
122
+ return NOOP_RECORDER;
123
+ }
124
+ function readRuntimeEnv(name) {
125
+ try {
126
+ const denoEnv = globalThis.Deno?.env;
127
+ const value = denoEnv?.get?.(name);
128
+ if (value)
129
+ return value;
130
+ } catch {
131
+ }
132
+ try {
133
+ if (typeof process !== "undefined") {
134
+ const value = process.env?.[name];
135
+ if (value)
136
+ return value;
137
+ }
138
+ } catch {
139
+ }
140
+ return void 0;
141
+ }
142
+ function probeWaitUntil() {
143
+ const fn = globalThis.EdgeRuntime?.waitUntil;
144
+ return typeof fn === "function" ? fn.bind(globalThis.EdgeRuntime) : void 0;
145
+ }
146
+ function createMetricsRecorder(ctx, deps = {}) {
147
+ const { config, server } = ctx;
148
+ if (!config.enabled)
149
+ return NOOP_RECORDER;
150
+ const doFetch = deps.fetch ?? globalThis.fetch;
151
+ if (!doFetch)
152
+ return NOOP_RECORDER;
153
+ const usesLovableEndpoint = config.endpoint === DEFAULT_METRICS_ENDPOINT;
154
+ const apiKey = usesLovableEndpoint ? (deps.getApiKey ?? (() => readRuntimeEnv(config.apiKeyEnvVar)))() : void 0;
155
+ if (usesLovableEndpoint && !apiKey) {
156
+ log.debug("metrics.disabled_no_api_key", { envVar: config.apiKeyEnvVar });
157
+ return NOOP_RECORDER;
158
+ }
159
+ const waitUntil = deps.waitUntil ?? probeWaitUntil();
160
+ const buffer = [];
161
+ let timer;
162
+ const schedule = (p) => {
163
+ if (waitUntil) {
164
+ try {
165
+ waitUntil(p);
166
+ return;
167
+ } catch {
168
+ }
169
+ }
170
+ void p.catch(() => {
171
+ });
172
+ };
173
+ const headers = {};
174
+ if (!usesLovableEndpoint) {
175
+ for (const [key, value] of Object.entries(config.headers)) {
176
+ if (key.toLowerCase() !== "content-type")
177
+ headers[key] = value;
178
+ }
179
+ }
180
+ headers["content-type"] = "application/json";
181
+ if (apiKey)
182
+ headers.authorization = `Bearer ${apiKey}`;
183
+ const flush = async () => {
184
+ if (buffer.length === 0)
185
+ return;
186
+ const records = buffer.splice(0, buffer.length);
187
+ try {
188
+ const res = await doFetch(config.endpoint, {
189
+ method: "POST",
190
+ headers,
191
+ body: buildLogsPayload(records, server),
192
+ keepalive: true
193
+ });
194
+ if (!res.ok)
195
+ log.debug("metrics.flush_rejected", { status: res.status });
196
+ } catch (err) {
197
+ log.debug("metrics.flush_failed", describeError(err));
198
+ }
199
+ };
200
+ const ensureTimer = () => {
201
+ if (timer !== void 0)
202
+ return;
203
+ timer = setInterval(() => schedule(flush()), config.flushIntervalMs);
204
+ timer.unref?.();
205
+ };
206
+ return {
207
+ record(ev) {
208
+ if (config.sampleRate < 1 && Math.random() >= config.sampleRate)
209
+ return;
210
+ buffer.push({ ...ev, timeUnixNano: nowUnixNano() });
211
+ ensureTimer();
212
+ if (buffer.length >= config.maxBatchSize)
213
+ schedule(flush());
214
+ },
215
+ flush
216
+ };
217
+ }
218
+ function createRecorderForRuntime(mcp) {
219
+ const config = resolveMetricsConfig(mcp.metrics);
220
+ if (!config.enabled)
221
+ return createNoopRecorder();
222
+ return createMetricsRecorder({ config, server: { name: mcp.name, version: mcp.version } });
223
+ }
224
+
47
225
  // src/core/promise.ts
48
226
  function cachedPromise(load, label) {
49
227
  let settled = false;
@@ -409,12 +587,13 @@ function assertRequiredScopes(auth, context) {
409
587
  throw new OAuthTokenError(403, "insufficient_scope", "Additional OAuth scope is required");
410
588
  }
411
589
  }
412
- function createRequestAuthorizer(mcp, options = {}) {
590
+ function createRequestAuthorizer(mcp, options = {}, recorder) {
413
591
  const runtime = getOAuthRuntime(mcp, options);
414
592
  return {
415
593
  async authorize(request) {
416
594
  if (runtime.kind === "unconfigured")
417
595
  return { ok: true };
596
+ const startedAt = nowMs();
418
597
  const token = parseBearerToken(request);
419
598
  if (!token) {
420
599
  log.info("auth.no_bearer_token", { outcome: "401" });
@@ -427,6 +606,12 @@ function createRequestAuthorizer(mcp, options = {}) {
427
606
  } catch (err) {
428
607
  if (err instanceof OAuthConfigurationError) {
429
608
  log.error("auth.config_error", { ...describeError(err), outcome: "500" });
609
+ recorder?.record({
610
+ tool: null,
611
+ method: "authorize",
612
+ outcome: "auth_config_error",
613
+ durationMs: nowMs() - startedAt
614
+ });
430
615
  return { ok: false, response: oauthConfigurationErrorResponse() };
431
616
  }
432
617
  if (err instanceof OAuthTokenError) {
@@ -484,6 +669,8 @@ export {
484
669
  JSON_HEADERS,
485
670
  headResponse,
486
671
  methodNotAllowed,
672
+ nowMs,
673
+ createRecorderForRuntime,
487
674
  resolveProtectedResource,
488
675
  assertResourcePathShape,
489
676
  oauthConfigurationErrorResponse,
@@ -13,7 +13,7 @@ function isFileMissing(err) {
13
13
  }
14
14
 
15
15
  // package.json
16
- var version = "0.15.0";
16
+ var version = "0.15.1";
17
17
 
18
18
  // src/protocols/rest/list-tools.ts
19
19
  var import_zod_compat = require("@modelcontextprotocol/sdk/server/zod-compat.js");
@@ -1,16 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  buildMcpListing
4
- } from "../chunk-VNRQPA4K.js";
5
- import "../chunk-G4XA7IJM.js";
6
- import "../chunk-QC3DXQTH.js";
4
+ } from "../chunk-QT5L4CS3.js";
5
+ import "../chunk-YTOMGJV5.js";
6
+ import "../chunk-DCWYK4CA.js";
7
7
  import "../chunk-6DXGZZA4.js";
8
8
  import {
9
9
  isFileMissing
10
10
  } from "../chunk-Y3ZFPEQH.js";
11
11
  import {
12
12
  version
13
- } from "../chunk-LWHVXXKQ.js";
13
+ } from "../chunk-GIHCQT6L.js";
14
14
 
15
15
  // src/manifest/io.ts
16
16
  import { randomUUID } from "crypto";
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import {
2
- ToolContext,
3
- resolveMetricsConfig
4
- } from "./chunk-P3F324UC.js";
2
+ ToolContext
3
+ } from "./chunk-MA5H6PSF.js";
5
4
  import {
6
5
  parseSafeUrl,
6
+ resolveMetricsConfig,
7
7
  setLogLevel
8
- } from "./chunk-QC3DXQTH.js";
8
+ } from "./chunk-DCWYK4CA.js";
9
9
 
10
10
  // src/core/define.ts
11
11
  function assertUniqueNames(mcp) {