@lovable.dev/mcp-js 2.1.0 → 2.2.0-rc.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 (52) hide show
  1. package/README.md +97 -8
  2. package/dist/{authorize-CzyL7nmF.d.cts → authorize-BmYO5_-I.d.cts} +1 -1
  3. package/dist/{authorize-Drz_eJ7O.d.ts → authorize-d9-8n9u3.d.ts} +1 -1
  4. package/dist/{base-COwtdWE8.d.ts → base-BAxrhVjq.d.ts} +2 -2
  5. package/dist/{base-DD4MCXZ6.d.cts → base-Bmw6Wrb-.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-BaRKDl4r.js → cors-BfacizN-.js} +2 -2
  9. package/dist/{cors-D-X0aP4O.cjs → cors-Dm9Yn8E5.cjs} +2 -2
  10. package/dist/errors-CpTdzogX.cjs +308 -0
  11. package/dist/errors-DGcJ8jPK.js +261 -0
  12. package/dist/index.cjs +5 -148
  13. package/dist/index.d.cts +9 -4
  14. package/dist/index.d.ts +9 -4
  15. package/dist/index.js +3 -147
  16. package/dist/{io-pl5npWDw.d.ts → io-7U860POB.d.ts} +1 -1
  17. package/dist/{io-Ds5MRSiK.d.cts → io-CL1GeE3D.d.cts} +1 -1
  18. package/dist/{io-DG83CLKx.js → io-DD8wjGjd.js} +3 -2
  19. package/dist/{io-DZD6xmJQ.cjs → io-DaqblVaG.cjs} +3 -2
  20. package/dist/{logger-CN1q-KNn.cjs → logger-C-Hdu57K.cjs} +5 -1
  21. package/dist/{logger-Ctv5xUSD.js → logger-CpLgYbCG.js} +5 -1
  22. package/dist/{mcp-Bx3LUn5O.cjs → mcp-BGsPYWs1.cjs} +93 -16
  23. package/dist/{mcp-DcmfehoI.js → mcp-DdJJjYYZ.js} +93 -16
  24. package/dist/{package-BNFIhH0F.js → package-cPx-zYIb.js} +1 -1
  25. package/dist/{package-CFVl3oIe.cjs → package-hWM0Ru0w.cjs} +1 -1
  26. package/dist/protocols/mcp/index.cjs +1 -1
  27. package/dist/protocols/mcp/index.d.cts +3 -3
  28. package/dist/protocols/mcp/index.d.ts +3 -3
  29. package/dist/protocols/mcp/index.js +1 -1
  30. package/dist/protocols/oauth-metadata.cjs +2 -2
  31. package/dist/protocols/oauth-metadata.d.cts +3 -3
  32. package/dist/protocols/oauth-metadata.d.ts +3 -3
  33. package/dist/protocols/oauth-metadata.js +2 -2
  34. package/dist/stacks/supabase/index.cjs +3 -3
  35. package/dist/stacks/supabase/index.d.cts +1 -1
  36. package/dist/stacks/supabase/index.d.ts +1 -1
  37. package/dist/stacks/supabase/index.js +3 -3
  38. package/dist/stacks/supabase/vite.cjs +1 -1
  39. package/dist/stacks/supabase/vite.d.cts +1 -1
  40. package/dist/stacks/supabase/vite.d.ts +1 -1
  41. package/dist/stacks/supabase/vite.js +1 -1
  42. package/dist/stacks/tanstack/index.cjs +2 -2
  43. package/dist/stacks/tanstack/index.d.cts +2 -2
  44. package/dist/stacks/tanstack/index.d.ts +2 -2
  45. package/dist/stacks/tanstack/index.js +2 -2
  46. package/dist/stacks/tanstack/vite.d.cts +1 -1
  47. package/dist/stacks/tanstack/vite.d.ts +1 -1
  48. package/dist/{types-B3_lX-L1.d.ts → types-BCKyT2wb.d.cts} +68 -20
  49. package/dist/{types-B3_lX-L1.d.cts → types-BCKyT2wb.d.ts} +68 -20
  50. package/package.json +2 -2
  51. package/dist/errors-Bwd1L0Rf.js +0 -100
  52. package/dist/errors-Cr95rSkB.cjs +0 -123
@@ -1,5 +1,5 @@
1
- import { c as createRecorderForRuntime } from "../../cors-BaRKDl4r.js";
2
- import { t as createMcpProtocolHandler } from "../../mcp-DcmfehoI.js";
1
+ import { c as createRecorderForRuntime } from "../../cors-BfacizN-.js";
2
+ import { t as createMcpProtocolHandler } from "../../mcp-DdJJjYYZ.js";
3
3
  import { createOAuthProtectedResourceMetadataHandler } from "../../protocols/oauth-metadata.js";
4
4
  import { t as applyForwardedOrigin } from "../../forwarded-BBXvFeh6.js";
5
5
  //#region src/stacks/tanstack/handlers.ts
@@ -1,4 +1,4 @@
1
- import { t as McpPluginApi } from "../../io-Ds5MRSiK.cjs";
1
+ import { t as McpPluginApi } from "../../io-CL1GeE3D.cjs";
2
2
  import { Plugin } from "vite";
3
3
  //#region src/stacks/tanstack/vite.d.ts
4
4
  interface McpPluginOptions {
@@ -1,4 +1,4 @@
1
- import { t as McpPluginApi } from "../../io-pl5npWDw.js";
1
+ import { t as McpPluginApi } from "../../io-7U860POB.js";
2
2
  import { Plugin } from "vite";
3
3
  //#region src/stacks/tanstack/vite.d.ts
4
4
  interface McpPluginOptions {
@@ -116,18 +116,11 @@ interface ToolRequestContext {
116
116
  client?: McpClientInfo;
117
117
  sendProgress?: (update: ProgressUpdate) => Promise<void>;
118
118
  }
119
- /** The per-request context passed to every tool handler as its second argument. The
120
- * auth context lives in a private field so the accessors are the only way out; `getToken()` is the sole bearer escape hatch. */
121
- declare class ToolContext {
119
+ /** Verified caller identity exposed to the app-context factory.
120
+ * `getToken()` is the sole bearer escape hatch. */
121
+ declare class AuthContext {
122
122
  #private;
123
- constructor(auth: McpAuthContext | undefined, request?: ToolRequestContext);
124
- /** Aborts when the client cancels the request or disconnects. */
125
- get signal(): AbortSignal;
126
- /** What the client declared about itself on this request. Unverified input. */
127
- get client(): McpClientInfo;
128
- /** Send a progress notification for this call. A no-op when the client
129
- * didn't request progress (no `progressToken` on the request). */
130
- progress(update: ProgressUpdate): Promise<void>;
123
+ constructor(auth: McpAuthContext | undefined);
131
124
  /** Whether the in-flight tool call carries a verified auth context. */
132
125
  isAuthenticated(): boolean;
133
126
  /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
@@ -148,6 +141,20 @@ declare class ToolContext {
148
141
  */
149
142
  getClaims(): JwtClaims | undefined;
150
143
  }
144
+ /** The per-request context passed to every tool handler as its second argument. */
145
+ declare class ToolContext<TAppContext = undefined> extends AuthContext {
146
+ #private;
147
+ constructor(auth: McpAuthContext | undefined, request?: ToolRequestContext, appContext?: TAppContext);
148
+ /** App-defined context created for this tool call. */
149
+ get appContext(): TAppContext;
150
+ /** Aborts when the client cancels the request or disconnects. */
151
+ get signal(): AbortSignal;
152
+ /** What the client declared about itself on this request. Unverified input. */
153
+ get client(): McpClientInfo;
154
+ /** Send a progress notification for this call. A no-op when the client
155
+ * didn't request progress (no `progressToken` on the request). */
156
+ progress(update: ProgressUpdate): Promise<void>;
157
+ }
151
158
  //#endregion
152
159
  //#region src/metrics/config.d.ts
153
160
  /**
@@ -307,7 +314,16 @@ interface ToolHandlerResult {
307
314
  structuredContent?: JsonValueInput;
308
315
  isError?: boolean;
309
316
  }
310
- interface ToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined> {
317
+ /** Inputs available while creating app context for one MCP request. */
318
+ interface CreateAppContextInput {
319
+ readonly auth: AuthContext;
320
+ readonly signal: AbortSignal;
321
+ }
322
+ /** The restricted surface used to decide whether a tool exists. */
323
+ interface ToolVisibilityContext<TAppContext> {
324
+ readonly appContext: TAppContext;
325
+ }
326
+ interface ToolDefinitionBase<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined> {
311
327
  readonly name: string;
312
328
  /** Human-readable display name shown to the user in MCP clients. Required. */
313
329
  readonly title: string;
@@ -316,22 +332,52 @@ interface ToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$
316
332
  readonly inputSchema?: TInput;
317
333
  readonly outputSchema?: TOutput;
318
334
  readonly annotations?: ToolAnnotations;
319
- readonly handler: TInput extends ZodRawShape$1 ? (args: ShapeOutput<TInput>, ctx: ToolContext) => ToolHandlerResult | Promise<ToolHandlerResult> : (args: Record<string, never> | undefined, ctx: ToolContext) => ToolHandlerResult | Promise<ToolHandlerResult>;
335
+ }
336
+ type ToolHandler<TInput extends ZodRawShape$1 | undefined, TAppContext> = TInput extends ZodRawShape$1 ? (args: ShapeOutput<TInput>, ctx: ToolContext<TAppContext>) => ToolHandlerResult | Promise<ToolHandlerResult> : (args: Record<string, never> | undefined, ctx: ToolContext<TAppContext>) => ToolHandlerResult | Promise<ToolHandlerResult>;
337
+ /** A tool that exists for every caller. Its handler receives the plain `ToolContext`. */
338
+ interface ToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined> extends ToolDefinitionBase<TInput, TOutput> {
339
+ readonly handler: ToolHandler<TInput, undefined>;
340
+ }
341
+ /** Input to `defineTool.withAppContext<TAppContext>()`. */
342
+ interface AppContextToolDefinitionInput<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined, TAppContext = unknown> extends ToolDefinitionBase<TInput, TOutput> {
343
+ /** Synchronously decide whether this tool exists for the current caller. */
344
+ readonly enabled?: (ctx: ToolVisibilityContext<TAppContext>) => boolean;
345
+ readonly handler: ToolHandler<TInput, TAppContext>;
346
+ }
347
+ interface AppContextToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined, TAppContext = unknown> extends AppContextToolDefinitionInput<TInput, TOutput, TAppContext> {
348
+ readonly requiresAppContext: true;
320
349
  }
321
350
  /** Type-erased element of `McpDefinition.tools`; per-tool generic typing
322
351
  * happens at the `defineTool` call site. */
323
- interface AnyToolDefinition {
352
+ interface AnyToolDefinition<TAppContext = never> {
324
353
  readonly name: string;
325
354
  readonly title: string;
326
355
  readonly description: string;
327
356
  readonly inputSchema?: ZodRawShape$1 | undefined;
328
357
  readonly outputSchema?: ZodOutputSchema | undefined;
329
358
  readonly annotations?: ToolAnnotations;
330
- readonly handler: (args: any, ctx: ToolContext) => ToolHandlerResult | Promise<ToolHandlerResult>;
359
+ readonly enabled?: (ctx: ToolVisibilityContext<TAppContext>) => boolean;
360
+ readonly handler: (args: any, ctx: ToolContext<TAppContext>) => ToolHandlerResult | Promise<ToolHandlerResult>;
331
361
  }
362
+ type StaticToolDefinition = Omit<AnyToolDefinition<undefined>, "enabled"> & {
363
+ readonly enabled?: never;
364
+ };
365
+ type AnyAppContextToolDefinition<TAppContext = unknown> = AnyToolDefinition<TAppContext> & {
366
+ readonly requiresAppContext: true;
367
+ };
368
+ type CreateAppContext<TAppContext> = (input: CreateAppContextInput) => TAppContext | Promise<TAppContext>;
369
+ /** Concrete app context requires a factory. `undefined`, `unknown`, and `never`
370
+ * remain optional because the SDK uses them for static or type-erased tools. */
371
+ type AppContextConfiguration<TAppContext> = [TAppContext] extends [undefined] ? {
372
+ readonly createAppContext?: CreateAppContext<TAppContext>;
373
+ } : unknown extends TAppContext ? {
374
+ readonly createAppContext?: CreateAppContext<TAppContext>;
375
+ } : {
376
+ readonly createAppContext: CreateAppContext<TAppContext>;
377
+ };
332
378
  /** Input shape for `defineMcp(...)`, whose branded `McpDefinition` return type is what
333
379
  * `createMcpProtocolHandler` consumes — the brand forces validation + freeze. */
334
- interface McpDefinitionInput {
380
+ type McpDefinitionInput<TAppContext = undefined> = {
335
381
  readonly name: string;
336
382
  /** Human-readable display name shown to MCP clients. Required. */
337
383
  readonly title: string;
@@ -346,17 +392,19 @@ interface McpDefinitionInput {
346
392
  * empty string to opt out.
347
393
  */
348
394
  readonly instructions: string;
349
- readonly tools: readonly AnyToolDefinition[];
395
+ readonly tools: readonly (AnyAppContextToolDefinition<TAppContext> | StaticToolDefinition)[];
350
396
  /**
351
397
  * Usage-metrics emission (runtime telemetry), on by default. `false` opts out;
352
398
  * `{ endpoint, headers }` sends to your own OTLP collector. Self-disables at
353
399
  * runtime without `LOVABLE_API_KEY` on the default Lovable endpoint.
354
400
  */
355
401
  readonly metrics?: MetricsConfig;
356
- }
402
+ } & AppContextConfiguration<TAppContext>;
357
403
  declare const McpDefinitionBrand: unique symbol;
358
- type McpDefinition = McpDefinitionInput & {
404
+ type McpDefinition<TAppContext = unknown> = Omit<McpDefinitionInput<TAppContext>, "createAppContext" | "tools"> & {
405
+ readonly createAppContext?: (input: CreateAppContextInput) => TAppContext | Promise<TAppContext>;
406
+ readonly tools: readonly AnyToolDefinition[];
359
407
  readonly [McpDefinitionBrand]: never;
360
408
  };
361
409
  //#endregion
362
- export { McpAuthConfig as A, MetricsConfig as C, ProgressUpdate as D, McpProtocolEra as E, ToolContext as O, ZodSchema as S, McpClientInfo as T, ToolContent as _, EmbeddedResource as a, ZodOutputSchema as b, JsonValue as c, McpDefinition as d, McpDefinitionInput as f, ToolAnnotations as g, TextContent as h, EmbeddedBlobResource as i, JwtClaims as k, JsonValueInput as l, ResourceLinkIcon as m, ContentAnnotations as n, EmbeddedTextResource as o, ResourceLink as p, ContentBlock as r, ImageContent as s, AudioContent as t, McpAllowedOrigins as u, ToolDefinition as v, MetricsOptions as w, ZodRawShape$1 as x, ToolHandlerResult as y };
410
+ export { McpClientInfo as A, ToolVisibilityContext as C, MetricsConfig as D, ZodSchema as E, McpAuthConfig as F, ProgressUpdate as M, ToolContext as N, MetricsOptions as O, JwtClaims as P, ToolHandlerResult as S, ZodRawShape$1 as T, ResourceLinkIcon as _, ContentBlock as a, ToolContent as b, EmbeddedResource as c, JsonValue as d, JsonValueInput as f, ResourceLink as g, McpDefinitionInput as h, ContentAnnotations as i, McpProtocolEra as j, AuthContext as k, EmbeddedTextResource as l, McpDefinition as m, AppContextToolDefinitionInput as n, CreateAppContextInput as o, McpAllowedOrigins as p, AudioContent as r, EmbeddedBlobResource as s, AppContextToolDefinition as t, ImageContent as u, TextContent as v, ZodOutputSchema as w, ToolDefinition as x, ToolAnnotations as y };
@@ -116,18 +116,11 @@ interface ToolRequestContext {
116
116
  client?: McpClientInfo;
117
117
  sendProgress?: (update: ProgressUpdate) => Promise<void>;
118
118
  }
119
- /** The per-request context passed to every tool handler as its second argument. The
120
- * auth context lives in a private field so the accessors are the only way out; `getToken()` is the sole bearer escape hatch. */
121
- declare class ToolContext {
119
+ /** Verified caller identity exposed to the app-context factory.
120
+ * `getToken()` is the sole bearer escape hatch. */
121
+ declare class AuthContext {
122
122
  #private;
123
- constructor(auth: McpAuthContext | undefined, request?: ToolRequestContext);
124
- /** Aborts when the client cancels the request or disconnects. */
125
- get signal(): AbortSignal;
126
- /** What the client declared about itself on this request. Unverified input. */
127
- get client(): McpClientInfo;
128
- /** Send a progress notification for this call. A no-op when the client
129
- * didn't request progress (no `progressToken` on the request). */
130
- progress(update: ProgressUpdate): Promise<void>;
123
+ constructor(auth: McpAuthContext | undefined);
131
124
  /** Whether the in-flight tool call carries a verified auth context. */
132
125
  isAuthenticated(): boolean;
133
126
  /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
@@ -148,6 +141,20 @@ declare class ToolContext {
148
141
  */
149
142
  getClaims(): JwtClaims | undefined;
150
143
  }
144
+ /** The per-request context passed to every tool handler as its second argument. */
145
+ declare class ToolContext<TAppContext = undefined> extends AuthContext {
146
+ #private;
147
+ constructor(auth: McpAuthContext | undefined, request?: ToolRequestContext, appContext?: TAppContext);
148
+ /** App-defined context created for this tool call. */
149
+ get appContext(): TAppContext;
150
+ /** Aborts when the client cancels the request or disconnects. */
151
+ get signal(): AbortSignal;
152
+ /** What the client declared about itself on this request. Unverified input. */
153
+ get client(): McpClientInfo;
154
+ /** Send a progress notification for this call. A no-op when the client
155
+ * didn't request progress (no `progressToken` on the request). */
156
+ progress(update: ProgressUpdate): Promise<void>;
157
+ }
151
158
  //#endregion
152
159
  //#region src/metrics/config.d.ts
153
160
  /**
@@ -307,7 +314,16 @@ interface ToolHandlerResult {
307
314
  structuredContent?: JsonValueInput;
308
315
  isError?: boolean;
309
316
  }
310
- interface ToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined> {
317
+ /** Inputs available while creating app context for one MCP request. */
318
+ interface CreateAppContextInput {
319
+ readonly auth: AuthContext;
320
+ readonly signal: AbortSignal;
321
+ }
322
+ /** The restricted surface used to decide whether a tool exists. */
323
+ interface ToolVisibilityContext<TAppContext> {
324
+ readonly appContext: TAppContext;
325
+ }
326
+ interface ToolDefinitionBase<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined> {
311
327
  readonly name: string;
312
328
  /** Human-readable display name shown to the user in MCP clients. Required. */
313
329
  readonly title: string;
@@ -316,22 +332,52 @@ interface ToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$
316
332
  readonly inputSchema?: TInput;
317
333
  readonly outputSchema?: TOutput;
318
334
  readonly annotations?: ToolAnnotations;
319
- readonly handler: TInput extends ZodRawShape$1 ? (args: ShapeOutput<TInput>, ctx: ToolContext) => ToolHandlerResult | Promise<ToolHandlerResult> : (args: Record<string, never> | undefined, ctx: ToolContext) => ToolHandlerResult | Promise<ToolHandlerResult>;
335
+ }
336
+ type ToolHandler<TInput extends ZodRawShape$1 | undefined, TAppContext> = TInput extends ZodRawShape$1 ? (args: ShapeOutput<TInput>, ctx: ToolContext<TAppContext>) => ToolHandlerResult | Promise<ToolHandlerResult> : (args: Record<string, never> | undefined, ctx: ToolContext<TAppContext>) => ToolHandlerResult | Promise<ToolHandlerResult>;
337
+ /** A tool that exists for every caller. Its handler receives the plain `ToolContext`. */
338
+ interface ToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined> extends ToolDefinitionBase<TInput, TOutput> {
339
+ readonly handler: ToolHandler<TInput, undefined>;
340
+ }
341
+ /** Input to `defineTool.withAppContext<TAppContext>()`. */
342
+ interface AppContextToolDefinitionInput<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined, TAppContext = unknown> extends ToolDefinitionBase<TInput, TOutput> {
343
+ /** Synchronously decide whether this tool exists for the current caller. */
344
+ readonly enabled?: (ctx: ToolVisibilityContext<TAppContext>) => boolean;
345
+ readonly handler: ToolHandler<TInput, TAppContext>;
346
+ }
347
+ interface AppContextToolDefinition<TInput extends ZodRawShape$1 | undefined = ZodRawShape$1 | undefined, TOutput extends ZodOutputSchema | undefined = ZodOutputSchema | undefined, TAppContext = unknown> extends AppContextToolDefinitionInput<TInput, TOutput, TAppContext> {
348
+ readonly requiresAppContext: true;
320
349
  }
321
350
  /** Type-erased element of `McpDefinition.tools`; per-tool generic typing
322
351
  * happens at the `defineTool` call site. */
323
- interface AnyToolDefinition {
352
+ interface AnyToolDefinition<TAppContext = never> {
324
353
  readonly name: string;
325
354
  readonly title: string;
326
355
  readonly description: string;
327
356
  readonly inputSchema?: ZodRawShape$1 | undefined;
328
357
  readonly outputSchema?: ZodOutputSchema | undefined;
329
358
  readonly annotations?: ToolAnnotations;
330
- readonly handler: (args: any, ctx: ToolContext) => ToolHandlerResult | Promise<ToolHandlerResult>;
359
+ readonly enabled?: (ctx: ToolVisibilityContext<TAppContext>) => boolean;
360
+ readonly handler: (args: any, ctx: ToolContext<TAppContext>) => ToolHandlerResult | Promise<ToolHandlerResult>;
331
361
  }
362
+ type StaticToolDefinition = Omit<AnyToolDefinition<undefined>, "enabled"> & {
363
+ readonly enabled?: never;
364
+ };
365
+ type AnyAppContextToolDefinition<TAppContext = unknown> = AnyToolDefinition<TAppContext> & {
366
+ readonly requiresAppContext: true;
367
+ };
368
+ type CreateAppContext<TAppContext> = (input: CreateAppContextInput) => TAppContext | Promise<TAppContext>;
369
+ /** Concrete app context requires a factory. `undefined`, `unknown`, and `never`
370
+ * remain optional because the SDK uses them for static or type-erased tools. */
371
+ type AppContextConfiguration<TAppContext> = [TAppContext] extends [undefined] ? {
372
+ readonly createAppContext?: CreateAppContext<TAppContext>;
373
+ } : unknown extends TAppContext ? {
374
+ readonly createAppContext?: CreateAppContext<TAppContext>;
375
+ } : {
376
+ readonly createAppContext: CreateAppContext<TAppContext>;
377
+ };
332
378
  /** Input shape for `defineMcp(...)`, whose branded `McpDefinition` return type is what
333
379
  * `createMcpProtocolHandler` consumes — the brand forces validation + freeze. */
334
- interface McpDefinitionInput {
380
+ type McpDefinitionInput<TAppContext = undefined> = {
335
381
  readonly name: string;
336
382
  /** Human-readable display name shown to MCP clients. Required. */
337
383
  readonly title: string;
@@ -346,17 +392,19 @@ interface McpDefinitionInput {
346
392
  * empty string to opt out.
347
393
  */
348
394
  readonly instructions: string;
349
- readonly tools: readonly AnyToolDefinition[];
395
+ readonly tools: readonly (AnyAppContextToolDefinition<TAppContext> | StaticToolDefinition)[];
350
396
  /**
351
397
  * Usage-metrics emission (runtime telemetry), on by default. `false` opts out;
352
398
  * `{ endpoint, headers }` sends to your own OTLP collector. Self-disables at
353
399
  * runtime without `LOVABLE_API_KEY` on the default Lovable endpoint.
354
400
  */
355
401
  readonly metrics?: MetricsConfig;
356
- }
402
+ } & AppContextConfiguration<TAppContext>;
357
403
  declare const McpDefinitionBrand: unique symbol;
358
- type McpDefinition = McpDefinitionInput & {
404
+ type McpDefinition<TAppContext = unknown> = Omit<McpDefinitionInput<TAppContext>, "createAppContext" | "tools"> & {
405
+ readonly createAppContext?: (input: CreateAppContextInput) => TAppContext | Promise<TAppContext>;
406
+ readonly tools: readonly AnyToolDefinition[];
359
407
  readonly [McpDefinitionBrand]: never;
360
408
  };
361
409
  //#endregion
362
- export { McpAuthConfig as A, MetricsConfig as C, ProgressUpdate as D, McpProtocolEra as E, ToolContext as O, ZodSchema as S, McpClientInfo as T, ToolContent as _, EmbeddedResource as a, ZodOutputSchema as b, JsonValue as c, McpDefinition as d, McpDefinitionInput as f, ToolAnnotations as g, TextContent as h, EmbeddedBlobResource as i, JwtClaims as k, JsonValueInput as l, ResourceLinkIcon as m, ContentAnnotations as n, EmbeddedTextResource as o, ResourceLink as p, ContentBlock as r, ImageContent as s, AudioContent as t, McpAllowedOrigins as u, ToolDefinition as v, MetricsOptions as w, ZodRawShape$1 as x, ToolHandlerResult as y };
410
+ export { McpClientInfo as A, ToolVisibilityContext as C, MetricsConfig as D, ZodSchema as E, McpAuthConfig as F, ProgressUpdate as M, ToolContext as N, MetricsOptions as O, JwtClaims as P, ToolHandlerResult as S, ZodRawShape$1 as T, ResourceLinkIcon as _, ContentBlock as a, ToolContent as b, EmbeddedResource as c, JsonValue as d, JsonValueInput as f, ResourceLink as g, McpDefinitionInput as h, ContentAnnotations as i, McpProtocolEra as j, AuthContext as k, EmbeddedTextResource as l, McpDefinition as m, AppContextToolDefinitionInput as n, CreateAppContextInput as o, McpAllowedOrigins as p, AudioContent as r, EmbeddedBlobResource as s, AppContextToolDefinition as t, ImageContent as u, TextContent as v, ZodOutputSchema as w, ToolDefinition as x, ToolAnnotations as y };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovable.dev/mcp-js",
3
- "version": "2.1.0",
3
+ "version": "2.2.0-rc.0",
4
4
  "engines": {
5
5
  "node": ">=20"
6
6
  },
@@ -108,8 +108,8 @@
108
108
  ],
109
109
  "license": "MIT",
110
110
  "dependencies": {
111
- "@modelcontextprotocol/server": "2.0.0",
112
111
  "@modelcontextprotocol/sdk": "1.28.0",
112
+ "@modelcontextprotocol/server": "2.0.0",
113
113
  "esbuild": "^0.27.0",
114
114
  "jose": "^6.2.2"
115
115
  },
@@ -1,100 +0,0 @@
1
- //#region src/auth/context.ts
2
- const NEVER_ABORTED = new AbortController().signal;
3
- /** The per-request context passed to every tool handler as its second argument. The
4
- * auth context lives in a private field so the accessors are the only way out; `getToken()` is the sole bearer escape hatch. */
5
- var ToolContext = class {
6
- #auth;
7
- #signal;
8
- #client;
9
- #sendProgress;
10
- constructor(auth, request = {}) {
11
- this.#auth = auth;
12
- this.#signal = request.signal ?? NEVER_ABORTED;
13
- this.#client = request.client ?? { protocol: "legacy" };
14
- this.#sendProgress = request.sendProgress;
15
- }
16
- /** Aborts when the client cancels the request or disconnects. */
17
- get signal() {
18
- return this.#signal;
19
- }
20
- /** What the client declared about itself on this request. Unverified input. */
21
- get client() {
22
- return this.#client;
23
- }
24
- /** Send a progress notification for this call. A no-op when the client
25
- * didn't request progress (no `progressToken` on the request). */
26
- async progress(update) {
27
- await this.#sendProgress?.(update);
28
- }
29
- /** Whether the in-flight tool call carries a verified auth context. */
30
- isAuthenticated() {
31
- return this.#auth !== void 0;
32
- }
33
- /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
34
- getToken() {
35
- return this.#auth?.bearer.token;
36
- }
37
- /** The verified user id (the token `sub`), or `undefined`. */
38
- getUserId() {
39
- return this.#auth?.principal.sub;
40
- }
41
- /** The verified user email, or `undefined` when absent. */
42
- getUserEmail() {
43
- return this.#auth?.principal.email;
44
- }
45
- /** The verified OAuth `client_id`, or `undefined`. */
46
- getClientId() {
47
- return this.#auth?.principal.clientId;
48
- }
49
- /** The verified OAuth scopes, or `undefined` when unauthenticated. */
50
- getScopes() {
51
- return this.#auth?.principal.scopes;
52
- }
53
- /** The verified token issuer, or `undefined`. */
54
- getIssuer() {
55
- return this.#auth?.principal.issuer;
56
- }
57
- /**
58
- * The full verified JWT claims, or `undefined`. Use this for app/business
59
- * authorization on issuer-specific claims that have no dedicated accessor.
60
- */
61
- getClaims() {
62
- return this.#auth?.principal.claims;
63
- }
64
- };
65
- //#endregion
66
- //#region src/core/errors.ts
67
- const TOOL_ERROR_BRAND = Symbol.for("@lovable.dev/mcp-js/tool-error");
68
- /** Error whose message is meant for the remote MCP caller; thrown from a handler
69
- * it becomes an `isError` result carrying exactly `message`. */
70
- var ToolError = class extends Error {
71
- constructor(message, options) {
72
- super(message, options);
73
- this.name = "ToolError";
74
- Object.defineProperty(this, TOOL_ERROR_BRAND, { value: true });
75
- }
76
- };
77
- function isToolError(err) {
78
- return err instanceof Error && Object.getOwnPropertyDescriptor(err, TOOL_ERROR_BRAND)?.value === true;
79
- }
80
- /** The caller-visible message of a thrown ToolError, or undefined for anything
81
- * else — including when inspecting the value itself throws (fail closed). */
82
- function resolveToolErrorMessage(err) {
83
- try {
84
- return isToolError(err) ? String(err.message) : void 0;
85
- } catch {
86
- return;
87
- }
88
- }
89
- const MAX_ERROR_SUMMARY = 500;
90
- /** Bounded name+message line for local logs; never for wire responses. */
91
- function errorSummary(err) {
92
- try {
93
- const text = err instanceof Error ? `${err.name}: ${err.message}` : String(err);
94
- return text.length > MAX_ERROR_SUMMARY ? `${text.slice(0, MAX_ERROR_SUMMARY)}…` : text;
95
- } catch {
96
- return "unprintable error";
97
- }
98
- }
99
- //#endregion
100
- export { ToolContext as i, errorSummary as n, resolveToolErrorMessage as r, ToolError as t };
@@ -1,123 +0,0 @@
1
- //#region src/auth/context.ts
2
- const NEVER_ABORTED = new AbortController().signal;
3
- /** The per-request context passed to every tool handler as its second argument. The
4
- * auth context lives in a private field so the accessors are the only way out; `getToken()` is the sole bearer escape hatch. */
5
- var ToolContext = class {
6
- #auth;
7
- #signal;
8
- #client;
9
- #sendProgress;
10
- constructor(auth, request = {}) {
11
- this.#auth = auth;
12
- this.#signal = request.signal ?? NEVER_ABORTED;
13
- this.#client = request.client ?? { protocol: "legacy" };
14
- this.#sendProgress = request.sendProgress;
15
- }
16
- /** Aborts when the client cancels the request or disconnects. */
17
- get signal() {
18
- return this.#signal;
19
- }
20
- /** What the client declared about itself on this request. Unverified input. */
21
- get client() {
22
- return this.#client;
23
- }
24
- /** Send a progress notification for this call. A no-op when the client
25
- * didn't request progress (no `progressToken` on the request). */
26
- async progress(update) {
27
- await this.#sendProgress?.(update);
28
- }
29
- /** Whether the in-flight tool call carries a verified auth context. */
30
- isAuthenticated() {
31
- return this.#auth !== void 0;
32
- }
33
- /** The verified bearer token, or `undefined` when unauthenticated. Pass it to downstream APIs; never return or log it. */
34
- getToken() {
35
- return this.#auth?.bearer.token;
36
- }
37
- /** The verified user id (the token `sub`), or `undefined`. */
38
- getUserId() {
39
- return this.#auth?.principal.sub;
40
- }
41
- /** The verified user email, or `undefined` when absent. */
42
- getUserEmail() {
43
- return this.#auth?.principal.email;
44
- }
45
- /** The verified OAuth `client_id`, or `undefined`. */
46
- getClientId() {
47
- return this.#auth?.principal.clientId;
48
- }
49
- /** The verified OAuth scopes, or `undefined` when unauthenticated. */
50
- getScopes() {
51
- return this.#auth?.principal.scopes;
52
- }
53
- /** The verified token issuer, or `undefined`. */
54
- getIssuer() {
55
- return this.#auth?.principal.issuer;
56
- }
57
- /**
58
- * The full verified JWT claims, or `undefined`. Use this for app/business
59
- * authorization on issuer-specific claims that have no dedicated accessor.
60
- */
61
- getClaims() {
62
- return this.#auth?.principal.claims;
63
- }
64
- };
65
- //#endregion
66
- //#region src/core/errors.ts
67
- const TOOL_ERROR_BRAND = Symbol.for("@lovable.dev/mcp-js/tool-error");
68
- /** Error whose message is meant for the remote MCP caller; thrown from a handler
69
- * it becomes an `isError` result carrying exactly `message`. */
70
- var ToolError = class extends Error {
71
- constructor(message, options) {
72
- super(message, options);
73
- this.name = "ToolError";
74
- Object.defineProperty(this, TOOL_ERROR_BRAND, { value: true });
75
- }
76
- };
77
- function isToolError(err) {
78
- return err instanceof Error && Object.getOwnPropertyDescriptor(err, TOOL_ERROR_BRAND)?.value === true;
79
- }
80
- /** The caller-visible message of a thrown ToolError, or undefined for anything
81
- * else — including when inspecting the value itself throws (fail closed). */
82
- function resolveToolErrorMessage(err) {
83
- try {
84
- return isToolError(err) ? String(err.message) : void 0;
85
- } catch {
86
- return;
87
- }
88
- }
89
- const MAX_ERROR_SUMMARY = 500;
90
- /** Bounded name+message line for local logs; never for wire responses. */
91
- function errorSummary(err) {
92
- try {
93
- const text = err instanceof Error ? `${err.name}: ${err.message}` : String(err);
94
- return text.length > MAX_ERROR_SUMMARY ? `${text.slice(0, MAX_ERROR_SUMMARY)}…` : text;
95
- } catch {
96
- return "unprintable error";
97
- }
98
- }
99
- //#endregion
100
- Object.defineProperty(exports, "ToolContext", {
101
- enumerable: true,
102
- get: function() {
103
- return ToolContext;
104
- }
105
- });
106
- Object.defineProperty(exports, "ToolError", {
107
- enumerable: true,
108
- get: function() {
109
- return ToolError;
110
- }
111
- });
112
- Object.defineProperty(exports, "errorSummary", {
113
- enumerable: true,
114
- get: function() {
115
- return errorSummary;
116
- }
117
- });
118
- Object.defineProperty(exports, "resolveToolErrorMessage", {
119
- enumerable: true,
120
- get: function() {
121
- return resolveToolErrorMessage;
122
- }
123
- });