@lovable.dev/mcp-js 2.1.0-rc.0 → 2.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.
Files changed (50) hide show
  1. package/README.md +1 -82
  2. package/dist/{authorize-BC4TM__8.d.cts → authorize-CzyL7nmF.d.cts} +1 -1
  3. package/dist/{authorize-DEsM-zTN.d.ts → authorize-Drz_eJ7O.d.ts} +1 -1
  4. package/dist/{base-BnlJz6TE.d.ts → base-COwtdWE8.d.ts} +2 -2
  5. package/dist/{base-4G2k3bMl.d.cts → base-DD4MCXZ6.d.cts} +2 -2
  6. package/dist/cli/extract-manifest.cjs +1 -1
  7. package/dist/cli/extract-manifest.js +1 -1
  8. package/dist/{cors-D4BjUpgU.js → cors-BaRKDl4r.js} +1 -1
  9. package/dist/{cors-0ZCwmwyK.cjs → cors-D-X0aP4O.cjs} +1 -1
  10. package/dist/errors-Bwd1L0Rf.js +100 -0
  11. package/dist/errors-Cr95rSkB.cjs +123 -0
  12. package/dist/index.cjs +147 -4
  13. package/dist/index.d.cts +4 -12
  14. package/dist/index.d.ts +4 -12
  15. package/dist/index.js +147 -3
  16. package/dist/{io-BEoJ_gb5.js → io-DG83CLKx.js} +2 -3
  17. package/dist/{io-Cz81GsrM.cjs → io-DZD6xmJQ.cjs} +2 -3
  18. package/dist/{io-9jGNgPfl.d.cts → io-Ds5MRSiK.d.cts} +1 -1
  19. package/dist/{io-BLZ8jYW6.d.ts → io-pl5npWDw.d.ts} +1 -1
  20. package/dist/{mcp-Q_rjpR99.cjs → mcp-Bx3LUn5O.cjs} +15 -92
  21. package/dist/{mcp-CuHyVOZK.js → mcp-DcmfehoI.js} +15 -92
  22. package/dist/{package-BAYb-3dX.js → package-BNFIhH0F.js} +1 -1
  23. package/dist/{package-DlouusYQ.cjs → package-CFVl3oIe.cjs} +1 -1
  24. package/dist/protocols/mcp/index.cjs +1 -1
  25. package/dist/protocols/mcp/index.d.cts +3 -3
  26. package/dist/protocols/mcp/index.d.ts +3 -3
  27. package/dist/protocols/mcp/index.js +1 -1
  28. package/dist/protocols/oauth-metadata.cjs +1 -1
  29. package/dist/protocols/oauth-metadata.d.cts +3 -3
  30. package/dist/protocols/oauth-metadata.d.ts +3 -3
  31. package/dist/protocols/oauth-metadata.js +1 -1
  32. package/dist/stacks/supabase/index.cjs +2 -2
  33. package/dist/stacks/supabase/index.d.cts +1 -1
  34. package/dist/stacks/supabase/index.d.ts +1 -1
  35. package/dist/stacks/supabase/index.js +2 -2
  36. package/dist/stacks/supabase/vite.cjs +1 -1
  37. package/dist/stacks/supabase/vite.d.cts +1 -1
  38. package/dist/stacks/supabase/vite.d.ts +1 -1
  39. package/dist/stacks/supabase/vite.js +1 -1
  40. package/dist/stacks/tanstack/index.cjs +2 -2
  41. package/dist/stacks/tanstack/index.d.cts +2 -2
  42. package/dist/stacks/tanstack/index.d.ts +2 -2
  43. package/dist/stacks/tanstack/index.js +2 -2
  44. package/dist/stacks/tanstack/vite.d.cts +1 -1
  45. package/dist/stacks/tanstack/vite.d.ts +1 -1
  46. package/dist/{types-S6J_QTXw.d.cts → types-B3_lX-L1.d.cts} +25 -60
  47. package/dist/{types-S6J_QTXw.d.ts → types-B3_lX-L1.d.ts} +25 -60
  48. package/package.json +2 -2
  49. package/dist/errors-CdrGi5aP.cjs +0 -309
  50. package/dist/errors-DTvfh8lb.js +0 -262
@@ -1,309 +0,0 @@
1
- const require_logger = require("./logger-CN1q-KNn.cjs");
2
- const require_schema = require("./schema-D3vClxb7.cjs");
3
- //#region src/core/define.ts
4
- function toolRequiresAppContext(tool) {
5
- return "requiresAppContext" in tool && tool.requiresAppContext === true;
6
- }
7
- function assertUniqueNames(mcp) {
8
- const seen = /* @__PURE__ */ new Set();
9
- for (const tool of mcp.tools) {
10
- if (seen.has(tool.name)) throw new Error(`@lovable.dev/mcp-js: duplicate tool name "${tool.name}"`);
11
- seen.add(tool.name);
12
- }
13
- }
14
- function assertAppContextConfiguration(mcp) {
15
- if (mcp.createAppContext && !mcp.auth) throw new Error(`@lovable.dev/mcp-js: createAppContext requires auth`);
16
- if (!mcp.createAppContext && mcp.tools.some((tool) => tool.enabled || toolRequiresAppContext(tool))) throw new Error(`@lovable.dev/mcp-js: app-context or conditional tools require createAppContext`);
17
- }
18
- function assertNonEmptyString(label, value) {
19
- if (value.trim() === "") throw new Error(`@lovable.dev/mcp-js: ${label} must not be empty`);
20
- if (value !== value.trim()) throw new Error(`@lovable.dev/mcp-js: ${label} must not have leading or trailing whitespace`);
21
- }
22
- function assertHttpsUrlField(name, value) {
23
- assertNonEmptyString(`auth.${name}`, value);
24
- require_logger.parseSafeUrl(`@lovable.dev/mcp-js: auth.${name}`, value);
25
- }
26
- function assertScope(scope) {
27
- if (scope.trim() === "" || /\s/.test(scope)) throw new Error(`@lovable.dev/mcp-js: OAuth scopes must be non-empty space-free tokens`);
28
- }
29
- function assertJwtAlgorithm(algorithm) {
30
- if (algorithm.trim() === "" || /\s/.test(algorithm)) throw new Error(`@lovable.dev/mcp-js: auth.algorithms must contain non-empty space-free tokens`);
31
- if (/^HS\d+$/i.test(algorithm) || algorithm.toLowerCase() === "none") throw new Error(`@lovable.dev/mcp-js: auth.algorithms cannot include "${algorithm}"; this verifier is JWKS-only`);
32
- }
33
- const MAX_CLOCK_TOLERANCE_SECONDS = 300;
34
- function assertClockToleranceSeconds(value) {
35
- if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > MAX_CLOCK_TOLERANCE_SECONDS) throw new Error(`@lovable.dev/mcp-js: auth.clockToleranceSeconds must be a non-negative number of seconds no greater than ${MAX_CLOCK_TOLERANCE_SECONDS}`);
36
- }
37
- function assertOAuthConfig(auth) {
38
- if (auth.type !== "oauth") {
39
- const authType = String(auth.type);
40
- throw new Error(`@lovable.dev/mcp-js: unsupported auth type "${authType}"`);
41
- }
42
- if (auth.issuer === void 0) throw new Error(`@lovable.dev/mcp-js: auth.issuer is required`);
43
- assertHttpsUrlField("issuer", auth.issuer);
44
- if (auth.jwksUri !== void 0) assertHttpsUrlField("jwksUri", auth.jwksUri);
45
- if (auth.resource !== void 0) assertHttpsUrlField("resource", auth.resource);
46
- if (auth.resourceName !== void 0) assertNonEmptyString("auth.resourceName", auth.resourceName);
47
- if (auth.resourceDocumentation !== void 0) assertHttpsUrlField("resourceDocumentation", auth.resourceDocumentation);
48
- if (auth.protectedResourceMetadataUrl !== void 0) assertHttpsUrlField("protectedResourceMetadataUrl", auth.protectedResourceMetadataUrl);
49
- if (auth.acceptedAudiences !== void 0) {
50
- if (!Array.isArray(auth.acceptedAudiences)) throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences must be an array`);
51
- if (auth.acceptedAudiences.length === 0) throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences must not be empty`);
52
- for (const audience of auth.acceptedAudiences) {
53
- if (typeof audience !== "string") throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences must contain strings`);
54
- assertNonEmptyString("auth.acceptedAudiences", audience);
55
- if (require_logger.trimTrailingSlash(audience) === "") throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences entries must not be only slashes`);
56
- }
57
- }
58
- if (auth.requireOAuthClientClaim !== void 0 && typeof auth.requireOAuthClientClaim !== "boolean") throw new Error(`@lovable.dev/mcp-js: auth.requireOAuthClientClaim must be a boolean`);
59
- if (auth.acceptResourceClaim !== void 0 && typeof auth.acceptResourceClaim !== "boolean") throw new Error(`@lovable.dev/mcp-js: auth.acceptResourceClaim must be a boolean`);
60
- if (auth.requiredScopes !== void 0) {
61
- if (!Array.isArray(auth.requiredScopes)) throw new Error(`@lovable.dev/mcp-js: auth.requiredScopes must be an array`);
62
- for (const scope of auth.requiredScopes) assertScope(scope);
63
- }
64
- if (auth.algorithms !== void 0) {
65
- if (!Array.isArray(auth.algorithms)) throw new Error(`@lovable.dev/mcp-js: auth.algorithms must be an array`);
66
- if (auth.algorithms.length === 0) throw new Error(`@lovable.dev/mcp-js: auth.algorithms must not be empty`);
67
- for (const algorithm of auth.algorithms) assertJwtAlgorithm(algorithm);
68
- }
69
- if (auth.accessTokenTyp !== void 0) {
70
- if (!Array.isArray(auth.accessTokenTyp)) throw new Error(`@lovable.dev/mcp-js: auth.accessTokenTyp must be an array`);
71
- if (auth.accessTokenTyp.length === 0) throw new Error(`@lovable.dev/mcp-js: auth.accessTokenTyp must not be empty`);
72
- for (const typ of auth.accessTokenTyp) {
73
- if (typeof typ !== "string") throw new Error(`@lovable.dev/mcp-js: auth.accessTokenTyp must contain strings`);
74
- assertNonEmptyString("auth.accessTokenTyp", typ);
75
- }
76
- }
77
- if (auth.clockToleranceSeconds !== void 0) assertClockToleranceSeconds(auth.clockToleranceSeconds);
78
- if (auth.resource === void 0 && auth.acceptedAudiences === void 0) throw new Error(`@lovable.dev/mcp-js: auth.resource or auth.acceptedAudiences is required`);
79
- }
80
- function freezeAuth(auth) {
81
- if (!auth) return;
82
- if (auth.acceptedAudiences) Object.freeze(auth.acceptedAudiences);
83
- if (auth.requiredScopes) Object.freeze(auth.requiredScopes);
84
- if (auth.algorithms) Object.freeze(auth.algorithms);
85
- if (auth.accessTokenTyp) Object.freeze(auth.accessTokenTyp);
86
- Object.freeze(auth);
87
- }
88
- function assertAllowedOrigins(allowedOrigins) {
89
- if (allowedOrigins === void 0 || allowedOrigins === "any") return;
90
- if (!Array.isArray(allowedOrigins)) throw new Error(`@lovable.dev/mcp-js: allowedOrigins must be "any" or an array of exact origins`);
91
- for (const origin of allowedOrigins) {
92
- if (typeof origin !== "string") throw new Error(`@lovable.dev/mcp-js: allowedOrigins must contain strings`);
93
- let parsed;
94
- try {
95
- parsed = new URL(origin);
96
- } catch {
97
- throw new Error(`@lovable.dev/mcp-js: allowedOrigins must contain valid exact origins`);
98
- }
99
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:" || parsed.origin !== origin) throw new Error(`@lovable.dev/mcp-js: allowedOrigins entry "${origin}" must be an exact http(s) origin without a path, query, fragment, or trailing slash`);
100
- }
101
- }
102
- function assertToolSchemas(mcp) {
103
- for (const tool of mcp.tools) for (const [name, definition, strategy] of [[
104
- "inputSchema",
105
- tool.inputSchema,
106
- "input"
107
- ], [
108
- "outputSchema",
109
- tool.outputSchema,
110
- "output"
111
- ]]) {
112
- if (!definition) continue;
113
- try {
114
- require_schema.jsonSchemaFromDefinition(definition, strategy);
115
- } catch (error) {
116
- const detail = error instanceof Error ? `: ${error.message}` : "";
117
- throw new Error(`@lovable.dev/mcp-js: tool "${tool.name}" ${name} cannot be represented as JSON Schema${detail}`);
118
- }
119
- }
120
- }
121
- /** Bind app context once while retaining input/output inference for every tool. */
122
- const defineStaticTool = (def) => def;
123
- const defineTool = Object.assign(defineStaticTool, { withAppContext: () => (def) => {
124
- return {
125
- ...def,
126
- requiresAppContext: true
127
- };
128
- } });
129
- /**
130
- * Declare the MCP server. `export default` the result from your MCP
131
- * entrypoint (default `lib/mcp/index.ts`); the Vite plugin reads it to
132
- * emit the framework-specific route(s) at build time.
133
- */
134
- function defineMcp(def) {
135
- assertNonEmptyString("name", def.name);
136
- assertNonEmptyString("title", def.title);
137
- assertNonEmptyString("version", def.version);
138
- assertUniqueNames(def);
139
- assertAppContextConfiguration(def);
140
- if (def.auth) assertOAuthConfig(def.auth);
141
- assertAllowedOrigins(def.allowedOrigins);
142
- assertToolSchemas(def);
143
- require_logger.resolveMetricsConfig(def.metrics);
144
- freezeAuth(def.auth);
145
- if (Array.isArray(def.allowedOrigins)) Object.freeze(def.allowedOrigins);
146
- Object.freeze(def.tools);
147
- for (const tool of def.tools) Object.freeze(tool);
148
- return def;
149
- }
150
- //#endregion
151
- //#region src/auth/context.ts
152
- const NEVER_ABORTED = new AbortController().signal;
153
- /** Verified caller identity exposed to the app-context factory.
154
- * `getToken()` is the sole bearer escape hatch. */
155
- var AuthContext = class {
156
- #auth;
157
- constructor(auth) {
158
- this.#auth = auth;
159
- }
160
- /** Whether the in-flight tool call carries a verified auth context. */
161
- isAuthenticated() {
162
- return this.#auth !== void 0;
163
- }
164
- /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
165
- getToken() {
166
- return this.#auth?.bearer.token;
167
- }
168
- /** The verified user id (the token `sub`), or `undefined`. */
169
- getUserId() {
170
- return this.#auth?.principal.sub;
171
- }
172
- /** The verified user email, or `undefined` when absent. */
173
- getUserEmail() {
174
- return this.#auth?.principal.email;
175
- }
176
- /** The verified OAuth `client_id`, or `undefined`. */
177
- getClientId() {
178
- return this.#auth?.principal.clientId;
179
- }
180
- /** The verified OAuth scopes, or `undefined` when unauthenticated. */
181
- getScopes() {
182
- return this.#auth?.principal.scopes;
183
- }
184
- /** The verified token issuer, or `undefined`. */
185
- getIssuer() {
186
- return this.#auth?.principal.issuer;
187
- }
188
- /**
189
- * The full verified JWT claims, or `undefined`. Use this for app/business
190
- * authorization on issuer-specific claims that have no dedicated accessor.
191
- */
192
- getClaims() {
193
- return this.#auth?.principal.claims;
194
- }
195
- };
196
- /** The per-request context passed to every tool handler as its second argument. */
197
- var ToolContext = class extends AuthContext {
198
- #appContext;
199
- #signal;
200
- #client;
201
- #sendProgress;
202
- constructor(auth, request = {}, appContext) {
203
- super(auth);
204
- this.#appContext = appContext;
205
- this.#signal = request.signal ?? NEVER_ABORTED;
206
- this.#client = request.client ?? { protocol: "legacy" };
207
- this.#sendProgress = request.sendProgress;
208
- }
209
- /** App-defined context created for this tool call. */
210
- get appContext() {
211
- return this.#appContext;
212
- }
213
- /** Aborts when the client cancels the request or disconnects. */
214
- get signal() {
215
- return this.#signal;
216
- }
217
- /** What the client declared about itself on this request. Unverified input. */
218
- get client() {
219
- return this.#client;
220
- }
221
- /** Send a progress notification for this call. A no-op when the client
222
- * didn't request progress (no `progressToken` on the request). */
223
- async progress(update) {
224
- await this.#sendProgress?.(update);
225
- }
226
- };
227
- //#endregion
228
- //#region src/core/errors.ts
229
- const TOOL_ERROR_BRAND = Symbol.for("@lovable.dev/mcp-js/tool-error");
230
- /** Error whose message is meant for the remote MCP caller; thrown from a handler
231
- * it becomes an `isError` result carrying exactly `message`. */
232
- var ToolError = class extends Error {
233
- constructor(message, options) {
234
- super(message, options);
235
- this.name = "ToolError";
236
- Object.defineProperty(this, TOOL_ERROR_BRAND, { value: true });
237
- }
238
- };
239
- function isToolError(err) {
240
- return err instanceof Error && Object.getOwnPropertyDescriptor(err, TOOL_ERROR_BRAND)?.value === true;
241
- }
242
- /** The caller-visible message of a thrown ToolError, or undefined for anything
243
- * else — including when inspecting the value itself throws (fail closed). */
244
- function resolveToolErrorMessage(err) {
245
- try {
246
- return isToolError(err) ? String(err.message) : void 0;
247
- } catch {
248
- return;
249
- }
250
- }
251
- const MAX_ERROR_SUMMARY = 500;
252
- /** Bounded name+message line for local logs; never for wire responses. */
253
- function errorSummary(err) {
254
- try {
255
- const text = err instanceof Error ? `${err.name}: ${err.message}` : String(err);
256
- return text.length > MAX_ERROR_SUMMARY ? `${text.slice(0, MAX_ERROR_SUMMARY)}…` : text;
257
- } catch {
258
- return "unprintable error";
259
- }
260
- }
261
- //#endregion
262
- Object.defineProperty(exports, "AuthContext", {
263
- enumerable: true,
264
- get: function() {
265
- return AuthContext;
266
- }
267
- });
268
- Object.defineProperty(exports, "ToolContext", {
269
- enumerable: true,
270
- get: function() {
271
- return ToolContext;
272
- }
273
- });
274
- Object.defineProperty(exports, "ToolError", {
275
- enumerable: true,
276
- get: function() {
277
- return ToolError;
278
- }
279
- });
280
- Object.defineProperty(exports, "defineMcp", {
281
- enumerable: true,
282
- get: function() {
283
- return defineMcp;
284
- }
285
- });
286
- Object.defineProperty(exports, "defineTool", {
287
- enumerable: true,
288
- get: function() {
289
- return defineTool;
290
- }
291
- });
292
- Object.defineProperty(exports, "errorSummary", {
293
- enumerable: true,
294
- get: function() {
295
- return errorSummary;
296
- }
297
- });
298
- Object.defineProperty(exports, "resolveToolErrorMessage", {
299
- enumerable: true,
300
- get: function() {
301
- return resolveToolErrorMessage;
302
- }
303
- });
304
- Object.defineProperty(exports, "toolRequiresAppContext", {
305
- enumerable: true,
306
- get: function() {
307
- return toolRequiresAppContext;
308
- }
309
- });
@@ -1,262 +0,0 @@
1
- import { o as parseSafeUrl, s as trimTrailingSlash, u as resolveMetricsConfig } from "./logger-Ctv5xUSD.js";
2
- import { t as jsonSchemaFromDefinition } from "./schema-CW1jKvio.js";
3
- //#region src/core/define.ts
4
- function toolRequiresAppContext(tool) {
5
- return "requiresAppContext" in tool && tool.requiresAppContext === true;
6
- }
7
- function assertUniqueNames(mcp) {
8
- const seen = /* @__PURE__ */ new Set();
9
- for (const tool of mcp.tools) {
10
- if (seen.has(tool.name)) throw new Error(`@lovable.dev/mcp-js: duplicate tool name "${tool.name}"`);
11
- seen.add(tool.name);
12
- }
13
- }
14
- function assertAppContextConfiguration(mcp) {
15
- if (mcp.createAppContext && !mcp.auth) throw new Error(`@lovable.dev/mcp-js: createAppContext requires auth`);
16
- if (!mcp.createAppContext && mcp.tools.some((tool) => tool.enabled || toolRequiresAppContext(tool))) throw new Error(`@lovable.dev/mcp-js: app-context or conditional tools require createAppContext`);
17
- }
18
- function assertNonEmptyString(label, value) {
19
- if (value.trim() === "") throw new Error(`@lovable.dev/mcp-js: ${label} must not be empty`);
20
- if (value !== value.trim()) throw new Error(`@lovable.dev/mcp-js: ${label} must not have leading or trailing whitespace`);
21
- }
22
- function assertHttpsUrlField(name, value) {
23
- assertNonEmptyString(`auth.${name}`, value);
24
- parseSafeUrl(`@lovable.dev/mcp-js: auth.${name}`, value);
25
- }
26
- function assertScope(scope) {
27
- if (scope.trim() === "" || /\s/.test(scope)) throw new Error(`@lovable.dev/mcp-js: OAuth scopes must be non-empty space-free tokens`);
28
- }
29
- function assertJwtAlgorithm(algorithm) {
30
- if (algorithm.trim() === "" || /\s/.test(algorithm)) throw new Error(`@lovable.dev/mcp-js: auth.algorithms must contain non-empty space-free tokens`);
31
- if (/^HS\d+$/i.test(algorithm) || algorithm.toLowerCase() === "none") throw new Error(`@lovable.dev/mcp-js: auth.algorithms cannot include "${algorithm}"; this verifier is JWKS-only`);
32
- }
33
- const MAX_CLOCK_TOLERANCE_SECONDS = 300;
34
- function assertClockToleranceSeconds(value) {
35
- if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > MAX_CLOCK_TOLERANCE_SECONDS) throw new Error(`@lovable.dev/mcp-js: auth.clockToleranceSeconds must be a non-negative number of seconds no greater than ${MAX_CLOCK_TOLERANCE_SECONDS}`);
36
- }
37
- function assertOAuthConfig(auth) {
38
- if (auth.type !== "oauth") {
39
- const authType = String(auth.type);
40
- throw new Error(`@lovable.dev/mcp-js: unsupported auth type "${authType}"`);
41
- }
42
- if (auth.issuer === void 0) throw new Error(`@lovable.dev/mcp-js: auth.issuer is required`);
43
- assertHttpsUrlField("issuer", auth.issuer);
44
- if (auth.jwksUri !== void 0) assertHttpsUrlField("jwksUri", auth.jwksUri);
45
- if (auth.resource !== void 0) assertHttpsUrlField("resource", auth.resource);
46
- if (auth.resourceName !== void 0) assertNonEmptyString("auth.resourceName", auth.resourceName);
47
- if (auth.resourceDocumentation !== void 0) assertHttpsUrlField("resourceDocumentation", auth.resourceDocumentation);
48
- if (auth.protectedResourceMetadataUrl !== void 0) assertHttpsUrlField("protectedResourceMetadataUrl", auth.protectedResourceMetadataUrl);
49
- if (auth.acceptedAudiences !== void 0) {
50
- if (!Array.isArray(auth.acceptedAudiences)) throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences must be an array`);
51
- if (auth.acceptedAudiences.length === 0) throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences must not be empty`);
52
- for (const audience of auth.acceptedAudiences) {
53
- if (typeof audience !== "string") throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences must contain strings`);
54
- assertNonEmptyString("auth.acceptedAudiences", audience);
55
- if (trimTrailingSlash(audience) === "") throw new Error(`@lovable.dev/mcp-js: auth.acceptedAudiences entries must not be only slashes`);
56
- }
57
- }
58
- if (auth.requireOAuthClientClaim !== void 0 && typeof auth.requireOAuthClientClaim !== "boolean") throw new Error(`@lovable.dev/mcp-js: auth.requireOAuthClientClaim must be a boolean`);
59
- if (auth.acceptResourceClaim !== void 0 && typeof auth.acceptResourceClaim !== "boolean") throw new Error(`@lovable.dev/mcp-js: auth.acceptResourceClaim must be a boolean`);
60
- if (auth.requiredScopes !== void 0) {
61
- if (!Array.isArray(auth.requiredScopes)) throw new Error(`@lovable.dev/mcp-js: auth.requiredScopes must be an array`);
62
- for (const scope of auth.requiredScopes) assertScope(scope);
63
- }
64
- if (auth.algorithms !== void 0) {
65
- if (!Array.isArray(auth.algorithms)) throw new Error(`@lovable.dev/mcp-js: auth.algorithms must be an array`);
66
- if (auth.algorithms.length === 0) throw new Error(`@lovable.dev/mcp-js: auth.algorithms must not be empty`);
67
- for (const algorithm of auth.algorithms) assertJwtAlgorithm(algorithm);
68
- }
69
- if (auth.accessTokenTyp !== void 0) {
70
- if (!Array.isArray(auth.accessTokenTyp)) throw new Error(`@lovable.dev/mcp-js: auth.accessTokenTyp must be an array`);
71
- if (auth.accessTokenTyp.length === 0) throw new Error(`@lovable.dev/mcp-js: auth.accessTokenTyp must not be empty`);
72
- for (const typ of auth.accessTokenTyp) {
73
- if (typeof typ !== "string") throw new Error(`@lovable.dev/mcp-js: auth.accessTokenTyp must contain strings`);
74
- assertNonEmptyString("auth.accessTokenTyp", typ);
75
- }
76
- }
77
- if (auth.clockToleranceSeconds !== void 0) assertClockToleranceSeconds(auth.clockToleranceSeconds);
78
- if (auth.resource === void 0 && auth.acceptedAudiences === void 0) throw new Error(`@lovable.dev/mcp-js: auth.resource or auth.acceptedAudiences is required`);
79
- }
80
- function freezeAuth(auth) {
81
- if (!auth) return;
82
- if (auth.acceptedAudiences) Object.freeze(auth.acceptedAudiences);
83
- if (auth.requiredScopes) Object.freeze(auth.requiredScopes);
84
- if (auth.algorithms) Object.freeze(auth.algorithms);
85
- if (auth.accessTokenTyp) Object.freeze(auth.accessTokenTyp);
86
- Object.freeze(auth);
87
- }
88
- function assertAllowedOrigins(allowedOrigins) {
89
- if (allowedOrigins === void 0 || allowedOrigins === "any") return;
90
- if (!Array.isArray(allowedOrigins)) throw new Error(`@lovable.dev/mcp-js: allowedOrigins must be "any" or an array of exact origins`);
91
- for (const origin of allowedOrigins) {
92
- if (typeof origin !== "string") throw new Error(`@lovable.dev/mcp-js: allowedOrigins must contain strings`);
93
- let parsed;
94
- try {
95
- parsed = new URL(origin);
96
- } catch {
97
- throw new Error(`@lovable.dev/mcp-js: allowedOrigins must contain valid exact origins`);
98
- }
99
- if (parsed.protocol !== "http:" && parsed.protocol !== "https:" || parsed.origin !== origin) throw new Error(`@lovable.dev/mcp-js: allowedOrigins entry "${origin}" must be an exact http(s) origin without a path, query, fragment, or trailing slash`);
100
- }
101
- }
102
- function assertToolSchemas(mcp) {
103
- for (const tool of mcp.tools) for (const [name, definition, strategy] of [[
104
- "inputSchema",
105
- tool.inputSchema,
106
- "input"
107
- ], [
108
- "outputSchema",
109
- tool.outputSchema,
110
- "output"
111
- ]]) {
112
- if (!definition) continue;
113
- try {
114
- jsonSchemaFromDefinition(definition, strategy);
115
- } catch (error) {
116
- const detail = error instanceof Error ? `: ${error.message}` : "";
117
- throw new Error(`@lovable.dev/mcp-js: tool "${tool.name}" ${name} cannot be represented as JSON Schema${detail}`);
118
- }
119
- }
120
- }
121
- /** Bind app context once while retaining input/output inference for every tool. */
122
- const defineStaticTool = (def) => def;
123
- const defineTool = Object.assign(defineStaticTool, { withAppContext: () => (def) => {
124
- return {
125
- ...def,
126
- requiresAppContext: true
127
- };
128
- } });
129
- /**
130
- * Declare the MCP server. `export default` the result from your MCP
131
- * entrypoint (default `lib/mcp/index.ts`); the Vite plugin reads it to
132
- * emit the framework-specific route(s) at build time.
133
- */
134
- function defineMcp(def) {
135
- assertNonEmptyString("name", def.name);
136
- assertNonEmptyString("title", def.title);
137
- assertNonEmptyString("version", def.version);
138
- assertUniqueNames(def);
139
- assertAppContextConfiguration(def);
140
- if (def.auth) assertOAuthConfig(def.auth);
141
- assertAllowedOrigins(def.allowedOrigins);
142
- assertToolSchemas(def);
143
- resolveMetricsConfig(def.metrics);
144
- freezeAuth(def.auth);
145
- if (Array.isArray(def.allowedOrigins)) Object.freeze(def.allowedOrigins);
146
- Object.freeze(def.tools);
147
- for (const tool of def.tools) Object.freeze(tool);
148
- return def;
149
- }
150
- //#endregion
151
- //#region src/auth/context.ts
152
- const NEVER_ABORTED = new AbortController().signal;
153
- /** Verified caller identity exposed to the app-context factory.
154
- * `getToken()` is the sole bearer escape hatch. */
155
- var AuthContext = class {
156
- #auth;
157
- constructor(auth) {
158
- this.#auth = auth;
159
- }
160
- /** Whether the in-flight tool call carries a verified auth context. */
161
- isAuthenticated() {
162
- return this.#auth !== void 0;
163
- }
164
- /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
165
- getToken() {
166
- return this.#auth?.bearer.token;
167
- }
168
- /** The verified user id (the token `sub`), or `undefined`. */
169
- getUserId() {
170
- return this.#auth?.principal.sub;
171
- }
172
- /** The verified user email, or `undefined` when absent. */
173
- getUserEmail() {
174
- return this.#auth?.principal.email;
175
- }
176
- /** The verified OAuth `client_id`, or `undefined`. */
177
- getClientId() {
178
- return this.#auth?.principal.clientId;
179
- }
180
- /** The verified OAuth scopes, or `undefined` when unauthenticated. */
181
- getScopes() {
182
- return this.#auth?.principal.scopes;
183
- }
184
- /** The verified token issuer, or `undefined`. */
185
- getIssuer() {
186
- return this.#auth?.principal.issuer;
187
- }
188
- /**
189
- * The full verified JWT claims, or `undefined`. Use this for app/business
190
- * authorization on issuer-specific claims that have no dedicated accessor.
191
- */
192
- getClaims() {
193
- return this.#auth?.principal.claims;
194
- }
195
- };
196
- /** The per-request context passed to every tool handler as its second argument. */
197
- var ToolContext = class extends AuthContext {
198
- #appContext;
199
- #signal;
200
- #client;
201
- #sendProgress;
202
- constructor(auth, request = {}, appContext) {
203
- super(auth);
204
- this.#appContext = appContext;
205
- this.#signal = request.signal ?? NEVER_ABORTED;
206
- this.#client = request.client ?? { protocol: "legacy" };
207
- this.#sendProgress = request.sendProgress;
208
- }
209
- /** App-defined context created for this tool call. */
210
- get appContext() {
211
- return this.#appContext;
212
- }
213
- /** Aborts when the client cancels the request or disconnects. */
214
- get signal() {
215
- return this.#signal;
216
- }
217
- /** What the client declared about itself on this request. Unverified input. */
218
- get client() {
219
- return this.#client;
220
- }
221
- /** Send a progress notification for this call. A no-op when the client
222
- * didn't request progress (no `progressToken` on the request). */
223
- async progress(update) {
224
- await this.#sendProgress?.(update);
225
- }
226
- };
227
- //#endregion
228
- //#region src/core/errors.ts
229
- const TOOL_ERROR_BRAND = Symbol.for("@lovable.dev/mcp-js/tool-error");
230
- /** Error whose message is meant for the remote MCP caller; thrown from a handler
231
- * it becomes an `isError` result carrying exactly `message`. */
232
- var ToolError = class extends Error {
233
- constructor(message, options) {
234
- super(message, options);
235
- this.name = "ToolError";
236
- Object.defineProperty(this, TOOL_ERROR_BRAND, { value: true });
237
- }
238
- };
239
- function isToolError(err) {
240
- return err instanceof Error && Object.getOwnPropertyDescriptor(err, TOOL_ERROR_BRAND)?.value === true;
241
- }
242
- /** The caller-visible message of a thrown ToolError, or undefined for anything
243
- * else — including when inspecting the value itself throws (fail closed). */
244
- function resolveToolErrorMessage(err) {
245
- try {
246
- return isToolError(err) ? String(err.message) : void 0;
247
- } catch {
248
- return;
249
- }
250
- }
251
- const MAX_ERROR_SUMMARY = 500;
252
- /** Bounded name+message line for local logs; never for wire responses. */
253
- function errorSummary(err) {
254
- try {
255
- const text = err instanceof Error ? `${err.name}: ${err.message}` : String(err);
256
- return text.length > MAX_ERROR_SUMMARY ? `${text.slice(0, MAX_ERROR_SUMMARY)}…` : text;
257
- } catch {
258
- return "unprintable error";
259
- }
260
- }
261
- //#endregion
262
- export { ToolContext as a, toolRequiresAppContext as c, AuthContext as i, errorSummary as n, defineMcp as o, resolveToolErrorMessage as r, defineTool as s, ToolError as t };