@lovable.dev/mcp-js 0.5.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 (36) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +386 -0
  3. package/dist/authorize-DpTEIFvs.d.ts +9 -0
  4. package/dist/chunk-6DXGZZA4.js +6 -0
  5. package/dist/chunk-DDF63QWG.js +80 -0
  6. package/dist/chunk-GLG5RZGE.js +66 -0
  7. package/dist/chunk-MA5H6PSF.js +46 -0
  8. package/dist/chunk-QA3FWDUV.js +40 -0
  9. package/dist/chunk-VD6CS7Y6.js +144 -0
  10. package/dist/chunk-XEDRJFAR.js +371 -0
  11. package/dist/index.cjs +271 -0
  12. package/dist/index.d.cts +56 -0
  13. package/dist/index.d.ts +56 -0
  14. package/dist/index.js +172 -0
  15. package/dist/protocols/mcp/index.cjs +505 -0
  16. package/dist/protocols/mcp/index.d.cts +14 -0
  17. package/dist/protocols/mcp/index.d.ts +14 -0
  18. package/dist/protocols/mcp/index.js +10 -0
  19. package/dist/protocols/oauth-metadata.cjs +390 -0
  20. package/dist/protocols/oauth-metadata.d.cts +8 -0
  21. package/dist/protocols/oauth-metadata.d.ts +8 -0
  22. package/dist/protocols/oauth-metadata.js +9 -0
  23. package/dist/protocols/rest/index.cjs +599 -0
  24. package/dist/protocols/rest/index.d.cts +11 -0
  25. package/dist/protocols/rest/index.d.ts +11 -0
  26. package/dist/protocols/rest/index.js +12 -0
  27. package/dist/stacks/tanstack/index.cjs +743 -0
  28. package/dist/stacks/tanstack/index.d.cts +38 -0
  29. package/dist/stacks/tanstack/index.d.ts +38 -0
  30. package/dist/stacks/tanstack/index.js +38 -0
  31. package/dist/stacks/tanstack/vite.cjs +291 -0
  32. package/dist/stacks/tanstack/vite.d.cts +64 -0
  33. package/dist/stacks/tanstack/vite.d.ts +64 -0
  34. package/dist/stacks/tanstack/vite.js +263 -0
  35. package/dist/types-zMlr1mOK.d.ts +270 -0
  36. package/package.json +101 -0
@@ -0,0 +1,743 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/stacks/tanstack/index.ts
21
+ var tanstack_exports = {};
22
+ __export(tanstack_exports, {
23
+ createTanStackInvokeToolHandler: () => createTanStackInvokeToolHandler,
24
+ createTanStackListToolsHandler: () => createTanStackListToolsHandler,
25
+ createTanStackMcpHandler: () => createTanStackMcpHandler,
26
+ createTanStackOAuthProtectedResourceMetadataHandler: () => createTanStackOAuthProtectedResourceMetadataHandler
27
+ });
28
+ module.exports = __toCommonJS(tanstack_exports);
29
+
30
+ // src/protocols/mcp/protocol.ts
31
+ var import_mcp = require("@modelcontextprotocol/sdk/server/mcp.js");
32
+ var import_webStandardStreamableHttp = require("@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js");
33
+
34
+ // src/core/http.ts
35
+ var JSON_HEADERS = { "Content-Type": "application/json" };
36
+ function headResponse(response) {
37
+ return new Response(null, { status: response.status, statusText: response.statusText, headers: response.headers });
38
+ }
39
+ function methodNotAllowed(allow) {
40
+ return new Response(JSON.stringify({ error: "method not allowed" }), {
41
+ status: 405,
42
+ headers: { ...JSON_HEADERS, Allow: allow }
43
+ });
44
+ }
45
+
46
+ // src/core/promise.ts
47
+ function cachedPromise(load) {
48
+ let settled = false;
49
+ let value;
50
+ return async () => {
51
+ if (settled)
52
+ return value;
53
+ const loaded = await load();
54
+ settled = true;
55
+ value = loaded;
56
+ return loaded;
57
+ };
58
+ }
59
+
60
+ // src/core/url.ts
61
+ function trimTrailingSlash(value) {
62
+ return value.replace(/\/+$/, "");
63
+ }
64
+ function isLocalHTTPHost(hostname) {
65
+ return hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]";
66
+ }
67
+ function urlSafetyProblem(url) {
68
+ const isAllowedHTTP = url.protocol === "http:" && isLocalHTTPHost(url.hostname);
69
+ if (url.protocol !== "https:" && !isAllowedHTTP) {
70
+ return "must use https://, except localhost development URLs";
71
+ }
72
+ if (url.username || url.password) {
73
+ return "must not include credentials";
74
+ }
75
+ if (url.search || url.hash) {
76
+ return "must not include query or fragment";
77
+ }
78
+ return void 0;
79
+ }
80
+
81
+ // src/core/validation.ts
82
+ function parseSafeUrl(subject, raw, ErrorClass = Error) {
83
+ let url;
84
+ try {
85
+ url = new URL(raw);
86
+ } catch {
87
+ throw new ErrorClass(`${subject} must be an absolute URL`);
88
+ }
89
+ const problem = urlSafetyProblem(url);
90
+ if (problem) {
91
+ throw new ErrorClass(`${subject} ${problem}`);
92
+ }
93
+ return url;
94
+ }
95
+
96
+ // src/auth/discovery.ts
97
+ var OAuthConfigurationError = class extends Error {
98
+ constructor(message) {
99
+ super(message);
100
+ this.name = "OAuthConfigurationError";
101
+ }
102
+ };
103
+ var METADATA_FETCH_TIMEOUT_MS = 5e3;
104
+ function issuerPath(url) {
105
+ const path = trimTrailingSlash(url.pathname);
106
+ return path === "" ? void 0 : path;
107
+ }
108
+ function pathInsertedOAuthMetadataUrls(url, path) {
109
+ return [
110
+ `${url.origin}/.well-known/oauth-authorization-server${path}`,
111
+ `${url.origin}/.well-known/openid-configuration${path}`
112
+ ];
113
+ }
114
+ function oauthMetadataUrlsForIssuer(issuer) {
115
+ const normalizedIssuer = trimTrailingSlash(issuer);
116
+ const url = new URL(normalizedIssuer);
117
+ const path = issuerPath(url);
118
+ if (!path) {
119
+ return [
120
+ `${normalizedIssuer}/.well-known/oauth-authorization-server`,
121
+ `${normalizedIssuer}/.well-known/openid-configuration`
122
+ ];
123
+ }
124
+ return [...pathInsertedOAuthMetadataUrls(url, path), `${normalizedIssuer}/.well-known/openid-configuration`];
125
+ }
126
+ async function fetchFirstValidOAuthServerMetadata(metadataUrls, expectedIssuer) {
127
+ const errors = [];
128
+ for (const url of metadataUrls) {
129
+ try {
130
+ return await fetchOAuthServerMetadata(url, expectedIssuer);
131
+ } catch (err) {
132
+ errors.push(`${url}: ${err instanceof Error ? err.message : String(err)}`);
133
+ }
134
+ }
135
+ throw new Error(`failed to discover OAuth server metadata (${errors.join("; ")})`);
136
+ }
137
+ async function fetchOAuthServerMetadata(url, expectedIssuer) {
138
+ const response = await fetch(url, { signal: AbortSignal.timeout(METADATA_FETCH_TIMEOUT_MS), redirect: "error" });
139
+ if (!response.ok) {
140
+ throw new Error(String(response.status));
141
+ }
142
+ const json = await response.json();
143
+ if (typeof json.issuer !== "string") {
144
+ throw new Error("missing issuer");
145
+ }
146
+ parseSafeUrl("discovered issuer", json.issuer);
147
+ if (trimTrailingSlash(json.issuer) !== expectedIssuer) {
148
+ throw new Error("issuer mismatch");
149
+ }
150
+ if (typeof json.jwks_uri !== "string") {
151
+ throw new Error("missing jwks_uri");
152
+ }
153
+ parseSafeUrl("discovered jwks_uri", json.jwks_uri);
154
+ return { issuer: json.issuer, jwks_uri: json.jwks_uri };
155
+ }
156
+ async function fetchIssuerOAuthServerMetadata(issuer) {
157
+ try {
158
+ return await fetchFirstValidOAuthServerMetadata(oauthMetadataUrlsForIssuer(issuer), issuer);
159
+ } catch (err) {
160
+ throw new OAuthConfigurationError(
161
+ `OAuth issuer discovery failed: ${err instanceof Error ? err.message : String(err)}`
162
+ );
163
+ }
164
+ }
165
+ function createOAuthDiscoveryResolver(auth) {
166
+ const configuredIssuer = trimTrailingSlash(auth.issuer);
167
+ const oauthServerMetadata = cachedPromise(() => fetchIssuerOAuthServerMetadata(configuredIssuer));
168
+ return {
169
+ resolveIssuer: async () => configuredIssuer,
170
+ resolveJwksUri: async () => auth.jwksUri ?? (await oauthServerMetadata()).jwks_uri
171
+ };
172
+ }
173
+
174
+ // src/auth/metadata-path.ts
175
+ var OAUTH_PROTECTED_RESOURCE_METADATA_PATH = "/.well-known/oauth-protected-resource";
176
+
177
+ // src/auth/resource.ts
178
+ function resolveProtectedResource(auth, request, options) {
179
+ if (auth.resource)
180
+ return trimTrailingSlash(auth.resource);
181
+ const path = resolveResourcePath(options.resourcePath, request);
182
+ const resourceURL = new URL(path, request.url);
183
+ resourceURL.search = "";
184
+ resourceURL.hash = "";
185
+ return trimTrailingSlash(resourceURL.toString());
186
+ }
187
+ function assertResourcePathShape(resourcePath) {
188
+ if (!resourcePath.startsWith("/") || resourcePath.startsWith("//") || resourcePath.includes("\\") || /(^|\/)\.\.(\/|$)/.test(resourcePath)) {
189
+ throw new Error(
190
+ `@lovable.dev/mcp-js: resourcePath must be an absolute path beginning with "/" without ".." segments or backslashes (got ${JSON.stringify(resourcePath)})`
191
+ );
192
+ }
193
+ }
194
+ function resolveResourcePath(resourcePath, request) {
195
+ if (resourcePath === void 0)
196
+ return new URL(request.url).pathname;
197
+ assertResourcePathShape(resourcePath);
198
+ return resourcePath;
199
+ }
200
+
201
+ // src/auth/verifier.ts
202
+ var import_jose = require("jose");
203
+
204
+ // src/auth/claims.ts
205
+ function readString(value) {
206
+ return typeof value === "string" ? value : void 0;
207
+ }
208
+ function splitScopes(value) {
209
+ if (typeof value === "string")
210
+ return value.split(/\s+/).filter(Boolean);
211
+ if (Array.isArray(value))
212
+ return value.filter((entry) => typeof entry === "string" && entry.length > 0);
213
+ return [];
214
+ }
215
+ function stringClaim(claims, name) {
216
+ return readString(claims[name]);
217
+ }
218
+
219
+ // src/auth/verifier.ts
220
+ var DEFAULT_JWT_ALGORITHMS = ["RS256", "RS384", "RS512", "ES256", "ES384", "ES512", "EdDSA"];
221
+ var DEFAULT_CLOCK_TOLERANCE_SECONDS = 30;
222
+ var OAuthTokenError = class extends Error {
223
+ constructor(status, oauthError, message) {
224
+ super(message);
225
+ this.status = status;
226
+ this.oauthError = oauthError;
227
+ this.name = "OAuthTokenError";
228
+ }
229
+ };
230
+ function resolveAcceptedAudiences(auth, resource) {
231
+ return auth.acceptedAudiences ?? [resource];
232
+ }
233
+ function isJwksFetchFailure(err) {
234
+ if (err instanceof import_jose.errors.JWKSTimeout || err instanceof import_jose.errors.JWKSInvalid)
235
+ return true;
236
+ if (!(err instanceof import_jose.errors.JOSEError))
237
+ return err instanceof Error;
238
+ return err.code === "ERR_JOSE_GENERIC";
239
+ }
240
+ async function verifyJwtClaims(token, keySet, issuer, audience, auth) {
241
+ try {
242
+ const { payload } = await (0, import_jose.jwtVerify)(token, keySet, {
243
+ // `issuer` is trimmed of any trailing slash; accept both forms so a token whose
244
+ // `iss` carries the slash the AS publishes still verifies.
245
+ issuer: [issuer, `${issuer}/`],
246
+ audience: [...audience],
247
+ algorithms: auth.algorithms ? [...auth.algorithms] : DEFAULT_JWT_ALGORITHMS,
248
+ requiredClaims: ["sub", "exp"],
249
+ clockTolerance: auth.clockToleranceSeconds ?? DEFAULT_CLOCK_TOLERANCE_SECONDS
250
+ });
251
+ return payload;
252
+ } catch (err) {
253
+ if (isJwksFetchFailure(err)) {
254
+ throw new OAuthConfigurationError(`JWKS fetch failed: ${err instanceof Error ? err.message : String(err)}`);
255
+ }
256
+ throw err;
257
+ }
258
+ }
259
+ function assertNonEmptySubject(claims) {
260
+ const sub = claims["sub"];
261
+ if (typeof sub !== "string" || sub.trim() === "") {
262
+ throw new OAuthTokenError(401, "invalid_token", "token subject claim must be a non-empty string");
263
+ }
264
+ }
265
+ function assertOAuthClientClaim(auth, clientId) {
266
+ if (auth.requireOAuthClientClaim !== false && !clientId) {
267
+ throw new OAuthTokenError(401, "invalid_token", "OAuth client claim is required");
268
+ }
269
+ }
270
+ function makeBearer(token) {
271
+ return Object.defineProperty({}, "token", { value: token, enumerable: false });
272
+ }
273
+ function buildMcpAuthContext(args) {
274
+ const { claims } = args;
275
+ return {
276
+ type: "oauth",
277
+ principal: {
278
+ claims,
279
+ issuer: args.issuer,
280
+ resource: args.resource,
281
+ acceptedAudiences: args.acceptedAudiences,
282
+ scopes: splitScopes(claims.scope),
283
+ sub: stringClaim(claims, "sub"),
284
+ email: stringClaim(claims, "email"),
285
+ clientId: stringClaim(claims, "client_id") ?? stringClaim(claims, "azp")
286
+ },
287
+ bearer: makeBearer(args.token)
288
+ };
289
+ }
290
+ function createOAuthTokenVerifier(auth, discovery) {
291
+ const loadRemoteJwksKeySet = cachedPromise(
292
+ () => discovery.resolveJwksUri().then((jwksURI) => (0, import_jose.createRemoteJWKSet)(new URL(jwksURI)))
293
+ );
294
+ return async (token, request, options) => {
295
+ const resource = resolveProtectedResource(auth, request, options);
296
+ const issuer = await discovery.resolveIssuer();
297
+ const acceptedAudiences = resolveAcceptedAudiences(auth, resource);
298
+ const claims = await verifyJwtClaims(token, await loadRemoteJwksKeySet(), issuer, acceptedAudiences, auth);
299
+ assertNonEmptySubject(claims);
300
+ const context = buildMcpAuthContext({ token, claims, issuer, resource, acceptedAudiences });
301
+ assertOAuthClientClaim(auth, context.principal.clientId);
302
+ return context;
303
+ };
304
+ }
305
+
306
+ // src/auth/authorize.ts
307
+ function quoteAuthenticateParam(value) {
308
+ const sanitized = value.replace(/[\u0000-\u001F\u007F]/g, "");
309
+ return `"${sanitized.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
310
+ }
311
+ function resolveProtectedResourceMetadataUrl(auth, request) {
312
+ if (auth.protectedResourceMetadataUrl !== void 0)
313
+ return auth.protectedResourceMetadataUrl;
314
+ const base = auth.resource ?? request.url;
315
+ return new URL(OAUTH_PROTECTED_RESOURCE_METADATA_PATH, base).toString();
316
+ }
317
+ function wwwAuthenticateHeader(auth, request, params = {}) {
318
+ const values = [
319
+ `realm=${quoteAuthenticateParam("mcp")}`,
320
+ `resource_metadata=${quoteAuthenticateParam(resolveProtectedResourceMetadataUrl(auth, request))}`
321
+ ];
322
+ if (auth.requiredScopes && auth.requiredScopes.length > 0) {
323
+ values.push(`scope=${quoteAuthenticateParam(auth.requiredScopes.join(" "))}`);
324
+ }
325
+ if (params.error)
326
+ values.push(`error=${quoteAuthenticateParam(params.error)}`);
327
+ if (params.errorDescription)
328
+ values.push(`error_description=${quoteAuthenticateParam(params.errorDescription)}`);
329
+ return `Bearer ${values.join(", ")}`;
330
+ }
331
+ function challengeResponse(auth, request, status, oauthError, errorDescription) {
332
+ const headers = new Headers(JSON_HEADERS);
333
+ headers.set("WWW-Authenticate", wwwAuthenticateHeader(auth, request, { error: oauthError, errorDescription }));
334
+ headers.set("Cache-Control", "no-store");
335
+ return new Response(JSON.stringify({ error: status === 403 ? "forbidden" : "unauthorized" }), {
336
+ status,
337
+ headers
338
+ });
339
+ }
340
+ function oauthConfigurationErrorResponse() {
341
+ return new Response(JSON.stringify({ error: "oauth configuration error" }), {
342
+ status: 500,
343
+ headers: { ...JSON_HEADERS, "Cache-Control": "no-store" }
344
+ });
345
+ }
346
+ function parseBearerToken(request) {
347
+ const header = request.headers.get("Authorization");
348
+ if (!header)
349
+ return void 0;
350
+ const match = /^Bearer\s+(.+)$/i.exec(header.trim());
351
+ if (!match)
352
+ return void 0;
353
+ const token = match[1].trim();
354
+ if (token === "" || /\s/.test(token))
355
+ return void 0;
356
+ return token;
357
+ }
358
+ function getOAuthRuntime(mcp, options = {}) {
359
+ if (options.resourcePath !== void 0)
360
+ assertResourcePathShape(options.resourcePath);
361
+ const stableOptions = { resourcePath: options.resourcePath };
362
+ const auth = mcp.auth?.type === "oauth" ? mcp.auth : void 0;
363
+ if (!auth)
364
+ return { kind: "unconfigured", options: stableOptions };
365
+ const discovery = createOAuthDiscoveryResolver(auth);
366
+ return {
367
+ kind: "configured",
368
+ auth,
369
+ discovery,
370
+ verify: createOAuthTokenVerifier(auth, discovery),
371
+ options: stableOptions
372
+ };
373
+ }
374
+ function assertRestResourceBinding(mcp, options = {}) {
375
+ const auth = mcp.auth?.type === "oauth" ? mcp.auth : void 0;
376
+ if (auth && auth.resource === void 0 && options.resourcePath === void 0) {
377
+ throw new Error(
378
+ `@lovable.dev/mcp-js: REST companion handlers require auth.resource or a resourcePath so principal.resource binds to the public MCP route, not the internal /.mcp/* request path`
379
+ );
380
+ }
381
+ }
382
+ function missingRequiredScopes(auth, scopes) {
383
+ const requiredScopes = auth.requiredScopes ?? [];
384
+ if (requiredScopes.length === 0)
385
+ return [];
386
+ const granted = new Set(scopes);
387
+ return requiredScopes.filter((scope) => !granted.has(scope));
388
+ }
389
+ function assertRequiredScopes(auth, context) {
390
+ if (missingRequiredScopes(auth, context.principal.scopes).length > 0) {
391
+ throw new OAuthTokenError(403, "insufficient_scope", "Additional OAuth scope is required");
392
+ }
393
+ }
394
+ function createRequestAuthorizer(mcp, options = {}) {
395
+ const runtime = getOAuthRuntime(mcp, options);
396
+ return {
397
+ async authorize(request) {
398
+ if (runtime.kind === "unconfigured")
399
+ return { ok: true };
400
+ const token = parseBearerToken(request);
401
+ if (!token)
402
+ return { ok: false, response: challengeResponse(runtime.auth, request, 401) };
403
+ try {
404
+ const auth = await runtime.verify(token, request, runtime.options);
405
+ assertRequiredScopes(runtime.auth, auth);
406
+ return { ok: true, auth };
407
+ } catch (err) {
408
+ if (err instanceof OAuthConfigurationError) {
409
+ return { ok: false, response: oauthConfigurationErrorResponse() };
410
+ }
411
+ if (err instanceof OAuthTokenError) {
412
+ return {
413
+ ok: false,
414
+ response: challengeResponse(runtime.auth, request, err.status, err.oauthError, err.message)
415
+ };
416
+ }
417
+ return {
418
+ ok: false,
419
+ response: challengeResponse(runtime.auth, request, 401, "invalid_token", "Invalid access token")
420
+ };
421
+ }
422
+ }
423
+ };
424
+ }
425
+
426
+ // src/auth/context.ts
427
+ var ToolContext = class {
428
+ #auth;
429
+ constructor(auth) {
430
+ this.#auth = auth;
431
+ }
432
+ /** Whether the in-flight tool call carries a verified auth context. */
433
+ isAuthenticated() {
434
+ return this.#auth !== void 0;
435
+ }
436
+ /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
437
+ getToken() {
438
+ return this.#auth?.bearer.token;
439
+ }
440
+ /** The verified user id (the token `sub`), or `undefined`. */
441
+ getUserId() {
442
+ return this.#auth?.principal.sub;
443
+ }
444
+ /** The verified user email, or `undefined` when absent. */
445
+ getUserEmail() {
446
+ return this.#auth?.principal.email;
447
+ }
448
+ /** The verified OAuth `client_id`, or `undefined`. */
449
+ getClientId() {
450
+ return this.#auth?.principal.clientId;
451
+ }
452
+ /** The verified OAuth scopes, or `undefined` when unauthenticated. */
453
+ getScopes() {
454
+ return this.#auth?.principal.scopes;
455
+ }
456
+ /** The verified token issuer, or `undefined`. */
457
+ getIssuer() {
458
+ return this.#auth?.principal.issuer;
459
+ }
460
+ /**
461
+ * The full verified JWT claims, or `undefined`. Use this for app/business
462
+ * authorization on issuer-specific claims that have no dedicated accessor.
463
+ */
464
+ getClaims() {
465
+ return this.#auth?.principal.claims;
466
+ }
467
+ };
468
+
469
+ // src/protocols/mcp/protocol.ts
470
+ function adaptToolToSdkCallback(tool, auth) {
471
+ return async (first) => {
472
+ const args = tool.inputSchema ? first ?? {} : {};
473
+ let result;
474
+ try {
475
+ result = await tool.handler(args, new ToolContext(auth));
476
+ } catch {
477
+ return { content: [{ type: "text", text: "tool execution failed" }], isError: true };
478
+ }
479
+ if (result == null) {
480
+ return { content: [{ type: "text", text: `tool "${tool.name}" returned no result` }], isError: true };
481
+ }
482
+ return { content: result.content ?? [], structuredContent: result.structuredContent, isError: result.isError };
483
+ };
484
+ }
485
+ function createMcpProtocolHandler(mcp, options = {}) {
486
+ const authorizer = createRequestAuthorizer(mcp, options);
487
+ return async (request) => {
488
+ const authResult = await authorizer.authorize(request);
489
+ if (!authResult.ok)
490
+ return authResult.response;
491
+ try {
492
+ const server = new import_mcp.McpServer(
493
+ { name: mcp.name, version: mcp.version, title: mcp.title },
494
+ { instructions: mcp.instructions }
495
+ );
496
+ for (const tool of mcp.tools) {
497
+ server.registerTool(
498
+ tool.name,
499
+ {
500
+ title: tool.title,
501
+ description: tool.description,
502
+ inputSchema: tool.inputSchema,
503
+ outputSchema: tool.outputSchema,
504
+ annotations: tool.annotations
505
+ },
506
+ adaptToolToSdkCallback(tool, authResult.auth)
507
+ );
508
+ }
509
+ const transport = new import_webStandardStreamableHttp.WebStandardStreamableHTTPServerTransport({
510
+ sessionIdGenerator: void 0
511
+ });
512
+ await server.connect(transport);
513
+ return await transport.handleRequest(request);
514
+ } catch {
515
+ return Response.json(
516
+ { jsonrpc: "2.0", id: null, error: { code: -32603, message: "internal error" } },
517
+ { status: 500 }
518
+ );
519
+ }
520
+ };
521
+ }
522
+
523
+ // src/protocols/oauth-metadata.ts
524
+ var CORS_ORIGIN = { "Access-Control-Allow-Origin": "*" };
525
+ function withCors(response) {
526
+ response.headers.set("Access-Control-Allow-Origin", "*");
527
+ return response;
528
+ }
529
+ function notFound() {
530
+ return new Response(JSON.stringify({ error: "not found" }), {
531
+ status: 404,
532
+ // `no-store` so a 404 (OAuth unconfigured) isn't heuristically cached and
533
+ // then served past a later deploy that enables OAuth and starts returning 200.
534
+ headers: { ...JSON_HEADERS, ...CORS_ORIGIN, "Cache-Control": "no-store" }
535
+ });
536
+ }
537
+ async function buildProtectedResourceMetadata(mcp, auth, request, options, discovery) {
538
+ const issuer = await discovery.resolveIssuer();
539
+ const body = {
540
+ resource: resolveProtectedResource(auth, request, options),
541
+ authorization_servers: [issuer],
542
+ bearer_methods_supported: ["header"],
543
+ resource_name: auth.resourceName ?? mcp.title,
544
+ resource_documentation: auth.resourceDocumentation
545
+ };
546
+ if (auth.requiredScopes && auth.requiredScopes.length > 0)
547
+ body.scopes_supported = auth.requiredScopes;
548
+ return body;
549
+ }
550
+ function createOAuthProtectedResourceMetadataHandler(mcp, options = {}) {
551
+ const runtime = getOAuthRuntime(mcp, options);
552
+ if (runtime.kind === "configured" && runtime.auth.protectedResourceMetadataUrl === void 0 && runtime.auth.resource === void 0 && runtime.options.resourcePath === void 0) {
553
+ throw new Error(
554
+ `@lovable.dev/mcp-js: auth.resource or a resourcePath is required so the protected-resource metadata doesn't advertise the well-known URL as the resource`
555
+ );
556
+ }
557
+ return async (request) => {
558
+ if (runtime.kind !== "configured" || runtime.auth.protectedResourceMetadataUrl !== void 0)
559
+ return notFound();
560
+ if (request.method === "OPTIONS") {
561
+ return new Response(null, {
562
+ status: 204,
563
+ headers: { ...CORS_ORIGIN, "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS" }
564
+ });
565
+ }
566
+ if (request.method !== "GET" && request.method !== "HEAD")
567
+ return withCors(methodNotAllowed("GET, HEAD, OPTIONS"));
568
+ const headers = {
569
+ ...JSON_HEADERS,
570
+ ...CORS_ORIGIN,
571
+ "Cache-Control": "public, max-age=300",
572
+ Vary: "Host"
573
+ };
574
+ try {
575
+ const metadata = await buildProtectedResourceMetadata(
576
+ mcp,
577
+ runtime.auth,
578
+ request,
579
+ runtime.options,
580
+ runtime.discovery
581
+ );
582
+ const response = Response.json(metadata, { headers });
583
+ return request.method === "HEAD" ? headResponse(response) : response;
584
+ } catch {
585
+ const response = withCors(oauthConfigurationErrorResponse());
586
+ return request.method === "HEAD" ? headResponse(response) : response;
587
+ }
588
+ };
589
+ }
590
+
591
+ // src/protocols/rest/list-tools.ts
592
+ var import_zod_compat = require("@modelcontextprotocol/sdk/server/zod-compat.js");
593
+ var import_zod_json_schema_compat = require("@modelcontextprotocol/sdk/server/zod-json-schema-compat.js");
594
+ function shapeToJsonSchema(shape) {
595
+ if (!shape)
596
+ return null;
597
+ try {
598
+ return (0, import_zod_json_schema_compat.toJsonSchemaCompat)((0, import_zod_compat.objectFromShape)(shape));
599
+ } catch {
600
+ return null;
601
+ }
602
+ }
603
+ function createListToolsHandler(mcp, options = {}) {
604
+ assertRestResourceBinding(mcp, options);
605
+ const authorizer = createRequestAuthorizer(mcp, options);
606
+ return async (request) => {
607
+ const authResult = await authorizer.authorize(request);
608
+ if (!authResult.ok)
609
+ return authResult.response;
610
+ if (request.method !== "GET" && request.method !== "HEAD")
611
+ return methodNotAllowed("GET, HEAD");
612
+ const body = {
613
+ server: { name: mcp.name, version: mcp.version, title: mcp.title },
614
+ tools: mcp.tools.map((tool) => ({
615
+ name: tool.name,
616
+ title: tool.title,
617
+ description: tool.description,
618
+ annotations: tool.annotations,
619
+ inputSchema: shapeToJsonSchema(tool.inputSchema),
620
+ outputSchema: shapeToJsonSchema(tool.outputSchema)
621
+ }))
622
+ };
623
+ const response = Response.json(body);
624
+ return request.method === "HEAD" ? headResponse(response) : response;
625
+ };
626
+ }
627
+
628
+ // src/protocols/rest/invoke-tool.ts
629
+ var import_zod_compat2 = require("@modelcontextprotocol/sdk/server/zod-compat.js");
630
+ var MAX_REFLECTED_TOOL_NAME = 256;
631
+ function safeReflectName(name) {
632
+ const text = String(name);
633
+ return text.length > MAX_REFLECTED_TOOL_NAME ? `${text.slice(0, MAX_REFLECTED_TOOL_NAME)}\u2026` : text;
634
+ }
635
+ function isEmptyArgs(value) {
636
+ if (value == null)
637
+ return true;
638
+ if (typeof value !== "object" || Array.isArray(value))
639
+ return false;
640
+ return Object.keys(value).length === 0;
641
+ }
642
+ function createInvokeToolHandler(mcp, options = {}) {
643
+ assertRestResourceBinding(mcp, options);
644
+ const authorizer = createRequestAuthorizer(mcp, options);
645
+ return async (request, toolName) => {
646
+ const authResult = await authorizer.authorize(request);
647
+ if (!authResult.ok)
648
+ return authResult.response;
649
+ if (request.method !== "POST")
650
+ return methodNotAllowed("POST");
651
+ const tool = mcp.tools.find((t) => t.name === toolName);
652
+ if (!tool) {
653
+ return new Response(JSON.stringify({ error: `unknown tool: ${safeReflectName(toolName)}` }), {
654
+ status: 404,
655
+ headers: JSON_HEADERS
656
+ });
657
+ }
658
+ let rawArgs = {};
659
+ const text = await request.text();
660
+ if (text) {
661
+ try {
662
+ rawArgs = JSON.parse(text);
663
+ } catch {
664
+ return new Response(JSON.stringify({ error: "invalid JSON body" }), {
665
+ status: 400,
666
+ headers: JSON_HEADERS
667
+ });
668
+ }
669
+ }
670
+ let args = rawArgs;
671
+ if (tool.inputSchema) {
672
+ try {
673
+ const schema = (0, import_zod_compat2.objectFromShape)(tool.inputSchema);
674
+ const parsed = await (0, import_zod_compat2.safeParseAsync)(schema, rawArgs);
675
+ if (!parsed.success) {
676
+ return new Response(
677
+ JSON.stringify({
678
+ error: "validation failed",
679
+ details: (0, import_zod_compat2.getParseErrorMessage)(parsed.error)
680
+ }),
681
+ { status: 400, headers: JSON_HEADERS }
682
+ );
683
+ }
684
+ args = parsed.data;
685
+ } catch {
686
+ return new Response(JSON.stringify({ error: "schema error", tool: toolName }), {
687
+ status: 500,
688
+ headers: JSON_HEADERS
689
+ });
690
+ }
691
+ } else if (!isEmptyArgs(rawArgs)) {
692
+ return new Response(JSON.stringify({ error: "tool has no inputSchema; expected empty body" }), {
693
+ status: 400,
694
+ headers: JSON_HEADERS
695
+ });
696
+ }
697
+ let result;
698
+ try {
699
+ result = await tool.handler(args, new ToolContext(authResult.auth));
700
+ } catch {
701
+ return new Response(JSON.stringify({ error: "handler threw", tool: toolName }), {
702
+ status: 500,
703
+ headers: JSON_HEADERS
704
+ });
705
+ }
706
+ if (result == null) {
707
+ return new Response(JSON.stringify({ error: `tool "${toolName}" returned no result` }), {
708
+ status: 500,
709
+ headers: JSON_HEADERS
710
+ });
711
+ }
712
+ return Response.json({
713
+ content: result.content ?? [],
714
+ structuredContent: result.structuredContent,
715
+ isError: result.isError
716
+ });
717
+ };
718
+ }
719
+
720
+ // src/stacks/tanstack/handlers.ts
721
+ function createTanStackMcpHandler(mcp, options = {}) {
722
+ const handler = createMcpProtocolHandler(mcp, options);
723
+ return ({ request }) => handler(request);
724
+ }
725
+ function createTanStackListToolsHandler(mcp, options = {}) {
726
+ const handler = createListToolsHandler(mcp, options);
727
+ return ({ request }) => handler(request);
728
+ }
729
+ function createTanStackInvokeToolHandler(mcp, options = {}) {
730
+ const handler = createInvokeToolHandler(mcp, options);
731
+ return ({ request, params }) => handler(request, params.tool);
732
+ }
733
+ function createTanStackOAuthProtectedResourceMetadataHandler(mcp, options = {}) {
734
+ const handler = createOAuthProtectedResourceMetadataHandler(mcp, options);
735
+ return ({ request }) => handler(request);
736
+ }
737
+ // Annotate the CommonJS export names for ESM import in node:
738
+ 0 && (module.exports = {
739
+ createTanStackInvokeToolHandler,
740
+ createTanStackListToolsHandler,
741
+ createTanStackMcpHandler,
742
+ createTanStackOAuthProtectedResourceMetadataHandler
743
+ });