@openconductor/mcp-sdk 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ import { SubscriptionRequiredError, PaymentRequiredError } from '../errors/index.mjs';
2
+
1
3
  interface PaymentConfig {
2
4
  /** OpenConductor API key for billing */
3
5
  apiKey: string;
@@ -133,5 +135,140 @@ declare function getUserBillingStatus(userId: string): Promise<{
133
135
  tier: string;
134
136
  active: boolean;
135
137
  } | null>;
138
+ type CreditPack = 'starter' | 'pro' | 'business';
139
+ interface CreditPackInfo {
140
+ name: string;
141
+ credits: number;
142
+ price: number;
143
+ perCredit: number;
144
+ savings: number;
145
+ bestFor: string;
146
+ popular?: boolean;
147
+ }
148
+ interface CheckoutSession {
149
+ sessionId: string;
150
+ url: string;
151
+ pack: CreditPack;
152
+ credits: number;
153
+ price: number;
154
+ perCredit: number;
155
+ }
156
+ /**
157
+ * Get available credit packs and pricing
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const packs = await getCreditPacks()
162
+ * console.log(packs.pro) // { name: 'Pro Pack', credits: 500, price: 39.99, ... }
163
+ * ```
164
+ */
165
+ declare function getCreditPacks(): Promise<Record<CreditPack, CreditPackInfo> | null>;
166
+ /**
167
+ * Create a Stripe checkout session for purchasing credits
168
+ * Returns a URL to redirect the user to for payment
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * const checkout = await createCreditsCheckout('pro', {
173
+ * successUrl: 'https://myapp.com/success',
174
+ * cancelUrl: 'https://myapp.com/pricing',
175
+ * })
176
+ *
177
+ * // Redirect user to checkout
178
+ * window.location.href = checkout.url
179
+ * ```
180
+ */
181
+ declare function createCreditsCheckout(pack: CreditPack, options?: {
182
+ successUrl?: string;
183
+ cancelUrl?: string;
184
+ }): Promise<CheckoutSession | null>;
185
+ /**
186
+ * Generate a direct checkout URL for embedding in upgrade prompts
187
+ *
188
+ * @example In an error handler:
189
+ * ```typescript
190
+ * catch (error) {
191
+ * if (error instanceof InsufficientCreditsError) {
192
+ * const checkoutUrl = await getCheckoutUrl('pro')
193
+ * return { error: `Insufficient credits. Buy more: ${checkoutUrl}` }
194
+ * }
195
+ * }
196
+ * ```
197
+ */
198
+ declare function getCheckoutUrl(pack: CreditPack): Promise<string | null>;
199
+ interface UpgradePromptOptions {
200
+ /** Required credits for the blocked action */
201
+ required?: number;
202
+ /** Currently available credits */
203
+ available?: number;
204
+ /** Tool that was blocked */
205
+ toolName?: string;
206
+ /** Include direct checkout links */
207
+ includeLinks?: boolean;
208
+ }
209
+ /**
210
+ * Generate a user-friendly upgrade prompt message
211
+ * Use this in your error handlers to show helpful messages
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * catch (error) {
216
+ * if (error instanceof InsufficientCreditsError) {
217
+ * const prompt = await generateUpgradePrompt({
218
+ * required: error.data.required,
219
+ * available: error.data.available,
220
+ * toolName: 'analyze-data'
221
+ * })
222
+ * console.log(prompt.message)
223
+ * // "You need 10 more credits to use analyze-data.
224
+ * // Buy the Pro Pack (500 credits) for $39.99 → https://..."
225
+ * }
226
+ * }
227
+ * ```
228
+ */
229
+ declare function generateUpgradePrompt(options?: UpgradePromptOptions): Promise<{
230
+ message: string;
231
+ shortMessage: string;
232
+ recommendedPack: CreditPack;
233
+ packs: Record<CreditPack, {
234
+ credits: number;
235
+ price: number;
236
+ url: string | null;
237
+ }>;
238
+ }>;
239
+ /**
240
+ * Get a simple checkout URL for the dashboard
241
+ * Useful when you don't want to create a full Stripe session
242
+ */
243
+ declare function getDashboardCheckoutUrl(options?: {
244
+ required?: number;
245
+ available?: number;
246
+ pack?: CreditPack;
247
+ }): string;
248
+ /**
249
+ * Create an error handler that automatically generates upgrade prompts
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * const handleError = createUpgradeErrorHandler({
254
+ * onInsufficientCredits: (prompt) => {
255
+ * return { error: prompt.shortMessage, upgradeUrl: prompt.packs.pro.url }
256
+ * }
257
+ * })
258
+ *
259
+ * // In your tool:
260
+ * try {
261
+ * return await paidHandler(input)
262
+ * } catch (error) {
263
+ * return handleError(error)
264
+ * }
265
+ * ```
266
+ */
267
+ declare function createUpgradeErrorHandler(handlers: {
268
+ onInsufficientCredits?: (prompt: Awaited<ReturnType<typeof generateUpgradePrompt>>) => unknown;
269
+ onSubscriptionRequired?: (error: SubscriptionRequiredError) => unknown;
270
+ onPaymentRequired?: (error: PaymentRequiredError) => unknown;
271
+ onOtherError?: (error: Error) => unknown;
272
+ }): (error: Error) => Promise<unknown>;
136
273
 
137
- export { type BillingStatus, type CreditRequirement, type PaymentConfig, type PaymentRequirement, type RequirePaymentOptions, type StripeRequirement, type SubscriptionRequirement, type UserContext, canUserAccess, createPaidTool, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment };
274
+ export { type BillingStatus, type CheckoutSession, type CreditPack, type CreditPackInfo, type CreditRequirement, type PaymentConfig, type PaymentRequirement, type RequirePaymentOptions, type StripeRequirement, type SubscriptionRequirement, type UpgradePromptOptions, type UserContext, canUserAccess, createCreditsCheckout, createPaidTool, createUpgradeErrorHandler, generateUpgradePrompt, getCheckoutUrl, getCreditPacks, getDashboardCheckoutUrl, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment };
@@ -1,3 +1,5 @@
1
+ import { SubscriptionRequiredError, PaymentRequiredError } from '../errors/index.js';
2
+
1
3
  interface PaymentConfig {
2
4
  /** OpenConductor API key for billing */
3
5
  apiKey: string;
@@ -133,5 +135,140 @@ declare function getUserBillingStatus(userId: string): Promise<{
133
135
  tier: string;
134
136
  active: boolean;
135
137
  } | null>;
138
+ type CreditPack = 'starter' | 'pro' | 'business';
139
+ interface CreditPackInfo {
140
+ name: string;
141
+ credits: number;
142
+ price: number;
143
+ perCredit: number;
144
+ savings: number;
145
+ bestFor: string;
146
+ popular?: boolean;
147
+ }
148
+ interface CheckoutSession {
149
+ sessionId: string;
150
+ url: string;
151
+ pack: CreditPack;
152
+ credits: number;
153
+ price: number;
154
+ perCredit: number;
155
+ }
156
+ /**
157
+ * Get available credit packs and pricing
158
+ *
159
+ * @example
160
+ * ```typescript
161
+ * const packs = await getCreditPacks()
162
+ * console.log(packs.pro) // { name: 'Pro Pack', credits: 500, price: 39.99, ... }
163
+ * ```
164
+ */
165
+ declare function getCreditPacks(): Promise<Record<CreditPack, CreditPackInfo> | null>;
166
+ /**
167
+ * Create a Stripe checkout session for purchasing credits
168
+ * Returns a URL to redirect the user to for payment
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * const checkout = await createCreditsCheckout('pro', {
173
+ * successUrl: 'https://myapp.com/success',
174
+ * cancelUrl: 'https://myapp.com/pricing',
175
+ * })
176
+ *
177
+ * // Redirect user to checkout
178
+ * window.location.href = checkout.url
179
+ * ```
180
+ */
181
+ declare function createCreditsCheckout(pack: CreditPack, options?: {
182
+ successUrl?: string;
183
+ cancelUrl?: string;
184
+ }): Promise<CheckoutSession | null>;
185
+ /**
186
+ * Generate a direct checkout URL for embedding in upgrade prompts
187
+ *
188
+ * @example In an error handler:
189
+ * ```typescript
190
+ * catch (error) {
191
+ * if (error instanceof InsufficientCreditsError) {
192
+ * const checkoutUrl = await getCheckoutUrl('pro')
193
+ * return { error: `Insufficient credits. Buy more: ${checkoutUrl}` }
194
+ * }
195
+ * }
196
+ * ```
197
+ */
198
+ declare function getCheckoutUrl(pack: CreditPack): Promise<string | null>;
199
+ interface UpgradePromptOptions {
200
+ /** Required credits for the blocked action */
201
+ required?: number;
202
+ /** Currently available credits */
203
+ available?: number;
204
+ /** Tool that was blocked */
205
+ toolName?: string;
206
+ /** Include direct checkout links */
207
+ includeLinks?: boolean;
208
+ }
209
+ /**
210
+ * Generate a user-friendly upgrade prompt message
211
+ * Use this in your error handlers to show helpful messages
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * catch (error) {
216
+ * if (error instanceof InsufficientCreditsError) {
217
+ * const prompt = await generateUpgradePrompt({
218
+ * required: error.data.required,
219
+ * available: error.data.available,
220
+ * toolName: 'analyze-data'
221
+ * })
222
+ * console.log(prompt.message)
223
+ * // "You need 10 more credits to use analyze-data.
224
+ * // Buy the Pro Pack (500 credits) for $39.99 → https://..."
225
+ * }
226
+ * }
227
+ * ```
228
+ */
229
+ declare function generateUpgradePrompt(options?: UpgradePromptOptions): Promise<{
230
+ message: string;
231
+ shortMessage: string;
232
+ recommendedPack: CreditPack;
233
+ packs: Record<CreditPack, {
234
+ credits: number;
235
+ price: number;
236
+ url: string | null;
237
+ }>;
238
+ }>;
239
+ /**
240
+ * Get a simple checkout URL for the dashboard
241
+ * Useful when you don't want to create a full Stripe session
242
+ */
243
+ declare function getDashboardCheckoutUrl(options?: {
244
+ required?: number;
245
+ available?: number;
246
+ pack?: CreditPack;
247
+ }): string;
248
+ /**
249
+ * Create an error handler that automatically generates upgrade prompts
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * const handleError = createUpgradeErrorHandler({
254
+ * onInsufficientCredits: (prompt) => {
255
+ * return { error: prompt.shortMessage, upgradeUrl: prompt.packs.pro.url }
256
+ * }
257
+ * })
258
+ *
259
+ * // In your tool:
260
+ * try {
261
+ * return await paidHandler(input)
262
+ * } catch (error) {
263
+ * return handleError(error)
264
+ * }
265
+ * ```
266
+ */
267
+ declare function createUpgradeErrorHandler(handlers: {
268
+ onInsufficientCredits?: (prompt: Awaited<ReturnType<typeof generateUpgradePrompt>>) => unknown;
269
+ onSubscriptionRequired?: (error: SubscriptionRequiredError) => unknown;
270
+ onPaymentRequired?: (error: PaymentRequiredError) => unknown;
271
+ onOtherError?: (error: Error) => unknown;
272
+ }): (error: Error) => Promise<unknown>;
136
273
 
137
- export { type BillingStatus, type CreditRequirement, type PaymentConfig, type PaymentRequirement, type RequirePaymentOptions, type StripeRequirement, type SubscriptionRequirement, type UserContext, canUserAccess, createPaidTool, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment };
274
+ export { type BillingStatus, type CheckoutSession, type CreditPack, type CreditPackInfo, type CreditRequirement, type PaymentConfig, type PaymentRequirement, type RequirePaymentOptions, type StripeRequirement, type SubscriptionRequirement, type UpgradePromptOptions, type UserContext, canUserAccess, createCreditsCheckout, createPaidTool, createUpgradeErrorHandler, generateUpgradePrompt, getCheckoutUrl, getCreditPacks, getDashboardCheckoutUrl, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment };
@@ -242,9 +242,139 @@ async function getUserBillingStatus(userId) {
242
242
  return null;
243
243
  }
244
244
  }
245
+ async function getCreditPacks() {
246
+ const config = paymentConfig;
247
+ if (!config) return null;
248
+ try {
249
+ const response = await fetch(`${config.apiUrl}/functions/v1/credit-packs`);
250
+ if (!response.ok) return null;
251
+ const data = await response.json();
252
+ return data.packs;
253
+ } catch {
254
+ return null;
255
+ }
256
+ }
257
+ async function createCreditsCheckout(pack, options = {}) {
258
+ const config = paymentConfig;
259
+ if (!config) {
260
+ console.error("[payment] Payment not initialized. Call initPayment() first.");
261
+ return null;
262
+ }
263
+ try {
264
+ const response = await fetch(`${config.apiUrl}/functions/v1/stripe-checkout-credits`, {
265
+ method: "POST",
266
+ headers: {
267
+ "Content-Type": "application/json",
268
+ "Authorization": `Bearer ${config.apiKey}`
269
+ },
270
+ body: JSON.stringify({
271
+ pack,
272
+ successUrl: options.successUrl,
273
+ cancelUrl: options.cancelUrl
274
+ })
275
+ });
276
+ if (!response.ok) {
277
+ const error = await response.text();
278
+ console.error("[payment] Checkout creation failed:", error);
279
+ return null;
280
+ }
281
+ return await response.json();
282
+ } catch (error) {
283
+ console.error("[payment] Checkout error:", error);
284
+ return null;
285
+ }
286
+ }
287
+ async function getCheckoutUrl(pack) {
288
+ const session = await createCreditsCheckout(pack);
289
+ return session?.url ?? null;
290
+ }
291
+ async function generateUpgradePrompt(options = {}) {
292
+ const { required = 0, available = 0, toolName, includeLinks = true } = options;
293
+ const needed = Math.max(0, required - available);
294
+ let recommendedPack = "starter";
295
+ if (needed > 100) recommendedPack = "pro";
296
+ if (needed > 500) recommendedPack = "business";
297
+ const packUrls = {
298
+ starter: null,
299
+ pro: null,
300
+ business: null
301
+ };
302
+ if (includeLinks) {
303
+ const [starterUrl, proUrl, businessUrl] = await Promise.all([
304
+ getCheckoutUrl("starter"),
305
+ getCheckoutUrl("pro"),
306
+ getCheckoutUrl("business")
307
+ ]);
308
+ packUrls.starter = starterUrl;
309
+ packUrls.pro = proUrl;
310
+ packUrls.business = businessUrl;
311
+ }
312
+ const toolPart = toolName ? ` to use ${toolName}` : "";
313
+ const shortMessage = `Insufficient credits: need ${required}, have ${available}`;
314
+ let message = `You need ${needed} more credits${toolPart}.
315
+
316
+ `;
317
+ message += `\u{1F4B3} Quick top-up options:
318
+ `;
319
+ message += ` \u2022 Starter: 100 credits for $9.99${packUrls.starter ? ` \u2192 ${packUrls.starter}` : ""}
320
+ `;
321
+ message += ` \u2022 Pro: 500 credits for $39.99 (20% off)${packUrls.pro ? ` \u2192 ${packUrls.pro}` : ""}
322
+ `;
323
+ message += ` \u2022 Business: 2,000 credits for $119.99 (40% off)${packUrls.business ? ` \u2192 ${packUrls.business}` : ""}
324
+ `;
325
+ if (recommendedPack !== "starter") {
326
+ message += `
327
+ \u2728 Recommended: ${recommendedPack.charAt(0).toUpperCase() + recommendedPack.slice(1)} Pack`;
328
+ }
329
+ return {
330
+ message,
331
+ shortMessage,
332
+ recommendedPack,
333
+ packs: {
334
+ starter: { credits: 100, price: 9.99, url: packUrls.starter },
335
+ pro: { credits: 500, price: 39.99, url: packUrls.pro },
336
+ business: { credits: 2e3, price: 119.99, url: packUrls.business }
337
+ }
338
+ };
339
+ }
340
+ function getDashboardCheckoutUrl(options = {}) {
341
+ const params = new URLSearchParams();
342
+ params.set("action", "buy-credits");
343
+ if (options.required) params.set("required", String(options.required));
344
+ if (options.available) params.set("available", String(options.available));
345
+ if (options.pack) params.set("pack", options.pack);
346
+ return `https://dashboard.openconductor.ai?${params.toString()}`;
347
+ }
348
+ function createUpgradeErrorHandler(handlers) {
349
+ return async (error) => {
350
+ if (error instanceof InsufficientCreditsError && handlers.onInsufficientCredits) {
351
+ const prompt = await generateUpgradePrompt({
352
+ required: error.data?.required,
353
+ available: error.data?.available
354
+ });
355
+ return handlers.onInsufficientCredits(prompt);
356
+ }
357
+ if (error instanceof SubscriptionRequiredError && handlers.onSubscriptionRequired) {
358
+ return handlers.onSubscriptionRequired(error);
359
+ }
360
+ if (error instanceof PaymentRequiredError && handlers.onPaymentRequired) {
361
+ return handlers.onPaymentRequired(error);
362
+ }
363
+ if (handlers.onOtherError) {
364
+ return handlers.onOtherError(error);
365
+ }
366
+ throw error;
367
+ };
368
+ }
245
369
 
246
370
  exports.canUserAccess = canUserAccess;
371
+ exports.createCreditsCheckout = createCreditsCheckout;
247
372
  exports.createPaidTool = createPaidTool;
373
+ exports.createUpgradeErrorHandler = createUpgradeErrorHandler;
374
+ exports.generateUpgradePrompt = generateUpgradePrompt;
375
+ exports.getCheckoutUrl = getCheckoutUrl;
376
+ exports.getCreditPacks = getCreditPacks;
377
+ exports.getDashboardCheckoutUrl = getDashboardCheckoutUrl;
248
378
  exports.getPaymentConfig = getPaymentConfig;
249
379
  exports.getUserBillingStatus = getUserBillingStatus;
250
380
  exports.initPayment = initPayment;
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts","../../src/payment/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA,EAkBxB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,oBAAA,EAAsB,MAAA;AAAA,EACtB,qBAAA,EAAuB;AACzB,CAAA;;;AClBO,IAAM,QAAA,GAAN,cAAuB,KAAA,CAAM;AAAA,EAClB,IAAA;AAAA,EACA,IAAA;AAAA,EAEhB,WAAA,CACE,IAAA,EACA,OAAA,EACA,IAAA,EACA;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IA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A;AAAA;AAC1C,KACD,CAAA;AAED,IAAA,IAAI,CAAC,QAAA,CAAS,EAAA,EAAI,OAAO,IAAA;AACzB,IAAA,OAAO,MAAM,SAAS,IAAA,EAAK;AAAA,EAC7B,CAAA,CAAA,MAAQ;AACN,IAAA,OAAO,IAAA;AAAA,EACT;AACF","file":"index.js","sourcesContent":["/**\n * JSON-RPC 2.0 Standard Error Codes\n * https://www.jsonrpc.org/specification#error_object\n */\nexport const ErrorCodes = {\n // JSON-RPC 2.0 Standard Errors\n PARSE_ERROR: -32700,\n INVALID_REQUEST: -32600,\n METHOD_NOT_FOUND: -32601,\n INVALID_PARAMS: -32602,\n INTERNAL_ERROR: -32603,\n\n // MCP-Specific Errors (-32000 to -32099 reserved for implementation)\n TOOL_NOT_FOUND: -32001,\n TOOL_EXECUTION_ERROR: -32002,\n RESOURCE_NOT_FOUND: -32003,\n AUTHENTICATION_ERROR: -32004,\n AUTHORIZATION_ERROR: -32005,\n RATE_LIMIT_ERROR: -32006,\n TIMEOUT_ERROR: -32007,\n VALIDATION_ERROR: -32008,\n DEPENDENCY_ERROR: -32009,\n CONFIGURATION_ERROR: -32010,\n PAYMENT_REQUIRED: -32011,\n INSUFFICIENT_CREDITS: -32012,\n SUBSCRIPTION_REQUIRED: -32013,\n} as const\n\nexport type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes]\n","import { ErrorCodes, type ErrorCode } from './codes'\n\nexport { ErrorCodes, type ErrorCode } from './codes'\n\n/**\n * Base error class for MCP servers\n * Formats errors according to JSON-RPC 2.0 specification\n */\nexport class MCPError extends Error {\n public readonly code: ErrorCode\n public readonly data?: Record<string, unknown>\n\n constructor(\n code: ErrorCode,\n message: string,\n data?: Record<string, unknown>\n ) {\n super(message)\n this.name = 'MCPError'\n this.code = code\n this.data = data\n\n // Maintains proper stack trace in V8 environments\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor)\n }\n }\n\n /**\n * Returns JSON-RPC 2.0 formatted error object\n */\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n ...(this.data && { data: this.data }),\n }\n }\n\n /**\n * Create error response for JSON-RPC\n */\n toResponse(id: string | number | null = null) {\n return {\n jsonrpc: '2.0' as const,\n id,\n error: this.toJSON(),\n }\n }\n}\n\n/**\n * Thrown when tool input validation fails\n */\nexport class ValidationError extends MCPError {\n constructor(field: string, reason: string, value?: unknown) {\n super(ErrorCodes.INVALID_PARAMS, `Validation failed for '${field}': ${reason}`, {\n field,\n reason,\n ...(value !== undefined && { value }),\n })\n this.name = 'ValidationError'\n }\n}\n\n/**\n * Thrown when a requested tool doesn't exist\n */\nexport class ToolNotFoundError extends MCPError {\n constructor(toolName: string) {\n super(ErrorCodes.TOOL_NOT_FOUND, `Tool '${toolName}' not found`, {\n tool: toolName,\n })\n this.name = 'ToolNotFoundError'\n }\n}\n\n/**\n * Thrown when tool execution fails\n */\nexport class ToolExecutionError extends MCPError {\n constructor(toolName: string, reason: string, cause?: Error) {\n super(ErrorCodes.TOOL_EXECUTION_ERROR, `Tool '${toolName}' failed: ${reason}`, {\n tool: toolName,\n reason,\n ...(cause && { cause: cause.message }),\n })\n this.name = 'ToolExecutionError'\n }\n}\n\n/**\n * Thrown when a requested resource doesn't exist\n */\nexport class ResourceNotFoundError extends MCPError {\n constructor(resourceUri: string) {\n super(ErrorCodes.RESOURCE_NOT_FOUND, `Resource '${resourceUri}' not found`, {\n uri: resourceUri,\n })\n this.name = 'ResourceNotFoundError'\n }\n}\n\n/**\n * Thrown when authentication fails\n */\nexport class AuthenticationError extends MCPError {\n constructor(reason: string = 'Authentication required') {\n super(ErrorCodes.AUTHENTICATION_ERROR, reason)\n this.name = 'AuthenticationError'\n }\n}\n\n/**\n * Thrown when authorization fails (authenticated but not permitted)\n */\nexport class AuthorizationError extends MCPError {\n constructor(action: string, resource?: string) {\n const msg = resource\n ? `Not authorized to ${action} on '${resource}'`\n : `Not authorized to ${action}`\n super(ErrorCodes.AUTHORIZATION_ERROR, msg, {\n action,\n ...(resource && { resource }),\n })\n this.name = 'AuthorizationError'\n }\n}\n\n/**\n * Thrown when rate limits are exceeded\n */\nexport class RateLimitError extends MCPError {\n constructor(retryAfterMs?: number) {\n super(ErrorCodes.RATE_LIMIT_ERROR, 'Rate limit exceeded', {\n ...(retryAfterMs && { retryAfterMs }),\n })\n this.name = 'RateLimitError'\n }\n}\n\n/**\n * Thrown when an operation times out\n */\nexport class TimeoutError extends MCPError {\n constructor(operation: string, timeoutMs: number) {\n super(ErrorCodes.TIMEOUT_ERROR, `Operation '${operation}' timed out after ${timeoutMs}ms`, {\n operation,\n timeoutMs,\n })\n this.name = 'TimeoutError'\n }\n}\n\n/**\n * Thrown when a required dependency is unavailable\n */\nexport class DependencyError extends MCPError {\n constructor(dependency: string, reason: string) {\n super(ErrorCodes.DEPENDENCY_ERROR, `Dependency '${dependency}' unavailable: ${reason}`, {\n dependency,\n reason,\n })\n this.name = 'DependencyError'\n }\n}\n\n/**\n * Thrown when server configuration is invalid\n */\nexport class ConfigurationError extends MCPError {\n constructor(setting: string, reason: string) {\n super(ErrorCodes.CONFIGURATION_ERROR, `Invalid configuration '${setting}': ${reason}`, {\n setting,\n reason,\n })\n this.name = 'ConfigurationError'\n }\n}\n\n/**\n * Thrown when payment is required to access a tool\n */\nexport class PaymentRequiredError extends MCPError {\n constructor(toolName: string, options?: { upgradeUrl?: string; priceId?: string }) {\n super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {\n tool: toolName,\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n ...(options?.priceId && { priceId: options.priceId }),\n })\n this.name = 'PaymentRequiredError'\n }\n}\n\n/**\n * Thrown when user doesn't have enough credits\n */\nexport class InsufficientCreditsError extends MCPError {\n constructor(required: number, available: number, options?: { purchaseUrl?: string }) {\n super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {\n required,\n available,\n ...(options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }),\n })\n this.name = 'InsufficientCreditsError'\n }\n}\n\n/**\n * Thrown when a subscription tier is required\n */\nexport class SubscriptionRequiredError extends MCPError {\n constructor(requiredTier: string, currentTier?: string, options?: { upgradeUrl?: string }) {\n const msg = currentTier \n ? `Subscription '${requiredTier}' required (current: '${currentTier}')`\n : `Subscription '${requiredTier}' required`\n super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {\n requiredTier,\n ...(currentTier && { currentTier }),\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n })\n this.name = 'SubscriptionRequiredError'\n }\n}\n","import {\n PaymentRequiredError,\n InsufficientCreditsError,\n SubscriptionRequiredError,\n ConfigurationError,\n} from '../errors'\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PaymentConfig {\n /** OpenConductor API key for billing */\n apiKey: string\n /** Base URL for billing API (default: https://api.openconductor.ai) */\n apiUrl?: string\n /** Default upgrade URL to show users */\n upgradeUrl?: string\n /** Enable test mode (skips actual billing) */\n testMode?: boolean\n}\n\nexport interface CreditRequirement {\n /** Number of credits to deduct per call */\n credits: number\n}\n\nexport interface SubscriptionRequirement {\n /** Required subscription tier (e.g., 'pro', 'enterprise') */\n tier: string\n /** Alternative tiers that also grant access */\n allowedTiers?: string[]\n}\n\nexport interface StripeRequirement {\n /** Stripe Price ID for per-call billing */\n priceId: string\n}\n\nexport type PaymentRequirement = \n | CreditRequirement \n | SubscriptionRequirement \n | StripeRequirement\n\nexport interface UserContext {\n /** User ID for billing lookup */\n userId: string\n /** Optional API key override */\n apiKey?: string\n}\n\nexport interface BillingStatus {\n /** Whether the user can proceed */\n allowed: boolean\n /** Current credit balance (if applicable) */\n credits?: number\n /** Current subscription tier (if applicable) */\n tier?: string\n /** Reason for denial (if not allowed) */\n reason?: string\n /** URL for upgrade/purchase */\n actionUrl?: string\n}\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\nlet paymentConfig: PaymentConfig | null = null\n\n/**\n * Initialize payment/billing configuration\n * \n * @example\n * ```typescript\n * initPayment({\n * apiKey: process.env.OPENCONDUCTOR_API_KEY!,\n * upgradeUrl: 'https://myapp.com/upgrade'\n * })\n * ```\n */\nexport function initPayment(config: PaymentConfig): void {\n paymentConfig = {\n apiUrl: 'https://api.openconductor.ai',\n testMode: false,\n ...config,\n }\n}\n\n/**\n * Get current payment configuration\n */\nexport function getPaymentConfig(): PaymentConfig | null {\n return paymentConfig\n}\n\n// ============================================================================\n// Billing Client\n// ============================================================================\n\ninterface CheckBillingParams {\n userId: string\n requirement: PaymentRequirement\n toolName: string\n}\n\ninterface DeductCreditsParams {\n userId: string\n credits: number\n toolName: string\n callId?: string\n}\n\nasync function checkBilling(params: CheckBillingParams): Promise<BillingStatus> {\n const config = paymentConfig\n if (!config) {\n throw new ConfigurationError('payment', 'Payment not initialized. Call initPayment() first.')\n }\n\n // Test mode - always allow\n if (config.testMode) {\n return { allowed: true, credits: 9999, tier: 'test' }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-check`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n requirement: params.requirement,\n tool: params.toolName,\n }),\n })\n\n if (!response.ok) {\n // Non-2xx response - treat as billing check failed\n const errorBody = await response.text()\n console.error('[payment] Billing check failed:', response.status, errorBody)\n return {\n allowed: false,\n reason: 'Billing service unavailable',\n actionUrl: config.upgradeUrl,\n }\n }\n\n return await response.json() as BillingStatus\n } catch (error) {\n // Network error - fail open or closed based on config\n console.error('[payment] Billing check error:', error)\n return {\n allowed: false,\n reason: 'Unable to verify billing status',\n actionUrl: config.upgradeUrl,\n }\n }\n}\n\nasync function deductCredits(params: DeductCreditsParams): Promise<boolean> {\n const config = paymentConfig\n if (!config) return false\n\n // Test mode - don't actually deduct\n if (config.testMode) return true\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-deduct`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n credits: params.credits,\n tool: params.toolName,\n callId: params.callId,\n }),\n })\n\n return response.ok\n } catch (error) {\n console.error('[payment] Credit deduction error:', error)\n return false\n }\n}\n\n// ============================================================================\n// Type Guards\n// ============================================================================\n\nfunction isCreditRequirement(req: PaymentRequirement): req is CreditRequirement {\n return 'credits' in req && typeof req.credits === 'number'\n}\n\nfunction isSubscriptionRequirement(req: PaymentRequirement): req is SubscriptionRequirement {\n return 'tier' in req && typeof req.tier === 'string'\n}\n\nfunction isStripeRequirement(req: PaymentRequirement): req is StripeRequirement {\n return 'priceId' in req && typeof req.priceId === 'string'\n}\n\n// ============================================================================\n// Main Middleware\n// ============================================================================\n\nexport interface RequirePaymentOptions {\n /** Tool name for billing records */\n toolName?: string\n /** Function to extract user ID from input */\n getUserId?: (input: unknown) => string | undefined\n /** Custom error handler */\n onPaymentError?: (error: Error) => void\n}\n\n/**\n * One-line payment middleware for MCP tools\n * \n * @example Credits-based billing\n * ```typescript\n * const paidTool = requirePayment({ credits: 10 })(myHandler)\n * ```\n * \n * @example Subscription tier requirement\n * ```typescript\n * const premiumTool = requirePayment({ tier: 'pro' })(myHandler)\n * ```\n * \n * @example With wrapTool\n * ```typescript\n * const safePaidTool = wrapTool(\n * requirePayment({ credits: 5 })(myHandler),\n * { name: 'premium-analysis' }\n * )\n * ```\n */\nexport function requirePayment<TInput extends { userId?: string }, TOutput>(\n requirement: PaymentRequirement,\n options: RequirePaymentOptions = {}\n) {\n return (\n handler: (input: TInput) => TOutput | Promise<TOutput>\n ): ((input: TInput) => Promise<TOutput>) => {\n \n const { toolName = 'unknown', getUserId } = options\n\n return async (input: TInput): Promise<TOutput> => {\n // Extract user ID\n const userId = getUserId?.(input) ?? input.userId\n \n if (!userId) {\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: paymentConfig?.upgradeUrl,\n })\n }\n\n // Check billing status\n const status = await checkBilling({\n userId,\n requirement,\n toolName,\n })\n\n if (!status.allowed) {\n // Throw appropriate error based on requirement type\n if (isCreditRequirement(requirement)) {\n throw new InsufficientCreditsError(\n requirement.credits,\n status.credits ?? 0,\n { purchaseUrl: status.actionUrl }\n )\n }\n\n if (isSubscriptionRequirement(requirement)) {\n throw new SubscriptionRequiredError(\n requirement.tier,\n status.tier,\n { upgradeUrl: status.actionUrl }\n )\n }\n\n // Generic payment required\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: status.actionUrl,\n ...(isStripeRequirement(requirement) && { priceId: requirement.priceId }),\n })\n }\n\n // Execute the handler\n const result = await handler(input)\n\n // Deduct credits after successful execution (if credit-based)\n if (isCreditRequirement(requirement)) {\n await deductCredits({\n userId,\n credits: requirement.credits,\n toolName,\n })\n }\n\n return result\n }\n }\n}\n\n// ============================================================================\n// Convenience Functions\n// ============================================================================\n\n/**\n * Create a paid tool with built-in payment verification\n * \n * @example\n * ```typescript\n * const analyzeData = createPaidTool({\n * name: 'analyze-data',\n * payment: { credits: 10 },\n * handler: async (input) => {\n * // Your tool logic\n * return result\n * }\n * })\n * ```\n */\nexport function createPaidTool<TInput extends { userId?: string }, TOutput>(config: {\n name: string\n payment: PaymentRequirement\n handler: (input: TInput) => TOutput | Promise<TOutput>\n getUserId?: (input: TInput) => string | undefined\n}): (input: TInput) => Promise<TOutput> {\n return requirePayment<TInput, TOutput>(config.payment, {\n toolName: config.name,\n getUserId: config.getUserId as (input: unknown) => string | undefined,\n })(config.handler)\n}\n\n/**\n * Check if a user can access a paid feature without executing it\n * Useful for UI gating\n * \n * @example\n * ```typescript\n * const canAccess = await canUserAccess('user_123', { credits: 10 }, 'premium-tool')\n * if (!canAccess.allowed) {\n * showUpgradePrompt(canAccess.actionUrl)\n * }\n * ```\n */\nexport async function canUserAccess(\n userId: string,\n requirement: PaymentRequirement,\n toolName: string = 'check'\n): Promise<BillingStatus> {\n return checkBilling({ userId, requirement, toolName })\n}\n\n/**\n * Get user's current billing status\n */\nexport async function getUserBillingStatus(userId: string): Promise<{\n credits: number\n tier: string\n active: boolean\n} | null> {\n const config = paymentConfig\n if (!config) return null\n\n if (config.testMode) {\n return { credits: 9999, tier: 'test', active: true }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-status/${userId}`, {\n headers: {\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n })\n\n if (!response.ok) return null\n return await response.json()\n } catch {\n return null\n }\n}\n"]}
1
+ {"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts","../../src/payment/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA,EAkBxB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,oBAAA,EAAsB,MAAA;AAAA,EACtB,qBAAA,EAAuB;AACzB,CAAA;;;AClBO,IAAM,QAAA,GAAN,cAAuB,KAAA,CAAM;AAAA,EAClB,IAAA;AAAA,EACA,IAAA;AAAA,EAEhB,WAAA,CACE,IAAA,EACA,OAAA,EACA,IAAA,EACA;AACA,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,GAAO,UAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AACZ,IAAA,IAAA,CAAK,IAAA,GAAO,IAAA;AAGZ,IAAA,IAAI,MAAM,iBAAA,EAAmB;AAC3B,MAAA,KAAA,CAAM,iBAAA,CAAkB,IAAA,EAAM,IAAA,CAAK,WAAW,CAAA;AAAA,IAChD;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAA,GAAS;AACP,IAAA,OAAO;AAAA,MACL,MAAM,IAAA,CAAK,IAAA;AAAA,MACX,SAAS,IAAA,CAAK,OAAA;AAAA,MACd,GAAI,IAAA,CAAK,IAAA,IAAQ,EAAE,IAAA,EAAM,KAAK,IAAA;AAAK,KACrC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,UAAA,CAAW,KAA6B,IA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n // JSON-RPC 2.0 Standard Errors\n PARSE_ERROR: -32700,\n INVALID_REQUEST: -32600,\n METHOD_NOT_FOUND: -32601,\n INVALID_PARAMS: -32602,\n INTERNAL_ERROR: -32603,\n\n // MCP-Specific Errors (-32000 to -32099 reserved for implementation)\n TOOL_NOT_FOUND: -32001,\n TOOL_EXECUTION_ERROR: -32002,\n RESOURCE_NOT_FOUND: -32003,\n AUTHENTICATION_ERROR: -32004,\n AUTHORIZATION_ERROR: -32005,\n RATE_LIMIT_ERROR: -32006,\n TIMEOUT_ERROR: -32007,\n VALIDATION_ERROR: -32008,\n DEPENDENCY_ERROR: -32009,\n CONFIGURATION_ERROR: -32010,\n PAYMENT_REQUIRED: -32011,\n INSUFFICIENT_CREDITS: -32012,\n SUBSCRIPTION_REQUIRED: -32013,\n} as const\n\nexport type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes]\n","import { ErrorCodes, type ErrorCode } from './codes'\n\nexport { ErrorCodes, type ErrorCode } from './codes'\n\n/**\n * Base error class for MCP servers\n * Formats errors according to JSON-RPC 2.0 specification\n */\nexport class MCPError extends Error {\n public readonly code: ErrorCode\n public readonly data?: Record<string, unknown>\n\n constructor(\n code: ErrorCode,\n message: string,\n data?: Record<string, unknown>\n ) {\n super(message)\n this.name = 'MCPError'\n this.code = code\n this.data = data\n\n // Maintains proper stack trace in V8 environments\n if (Error.captureStackTrace) {\n Error.captureStackTrace(this, this.constructor)\n }\n }\n\n /**\n * Returns JSON-RPC 2.0 formatted error object\n */\n toJSON() {\n return {\n code: this.code,\n message: this.message,\n ...(this.data && { data: this.data }),\n }\n }\n\n /**\n * Create error response for JSON-RPC\n */\n toResponse(id: string | number | null = null) {\n return {\n jsonrpc: '2.0' as const,\n id,\n error: this.toJSON(),\n }\n }\n}\n\n/**\n * Thrown when tool input validation fails\n */\nexport class ValidationError extends MCPError {\n constructor(field: string, reason: string, value?: unknown) {\n super(ErrorCodes.INVALID_PARAMS, `Validation failed for '${field}': ${reason}`, {\n field,\n reason,\n ...(value !== undefined && { value }),\n })\n this.name = 'ValidationError'\n }\n}\n\n/**\n * Thrown when a requested tool doesn't exist\n */\nexport class ToolNotFoundError extends MCPError {\n constructor(toolName: string) {\n super(ErrorCodes.TOOL_NOT_FOUND, `Tool '${toolName}' not found`, {\n tool: toolName,\n })\n this.name = 'ToolNotFoundError'\n }\n}\n\n/**\n * Thrown when tool execution fails\n */\nexport class ToolExecutionError extends MCPError {\n constructor(toolName: string, reason: string, cause?: Error) {\n super(ErrorCodes.TOOL_EXECUTION_ERROR, `Tool '${toolName}' failed: ${reason}`, {\n tool: toolName,\n reason,\n ...(cause && { cause: cause.message }),\n })\n this.name = 'ToolExecutionError'\n }\n}\n\n/**\n * Thrown when a requested resource doesn't exist\n */\nexport class ResourceNotFoundError extends MCPError {\n constructor(resourceUri: string) {\n super(ErrorCodes.RESOURCE_NOT_FOUND, `Resource '${resourceUri}' not found`, {\n uri: resourceUri,\n })\n this.name = 'ResourceNotFoundError'\n }\n}\n\n/**\n * Thrown when authentication fails\n */\nexport class AuthenticationError extends MCPError {\n constructor(reason: string = 'Authentication required') {\n super(ErrorCodes.AUTHENTICATION_ERROR, reason)\n this.name = 'AuthenticationError'\n }\n}\n\n/**\n * Thrown when authorization fails (authenticated but not permitted)\n */\nexport class AuthorizationError extends MCPError {\n constructor(action: string, resource?: string) {\n const msg = resource\n ? `Not authorized to ${action} on '${resource}'`\n : `Not authorized to ${action}`\n super(ErrorCodes.AUTHORIZATION_ERROR, msg, {\n action,\n ...(resource && { resource }),\n })\n this.name = 'AuthorizationError'\n }\n}\n\n/**\n * Thrown when rate limits are exceeded\n */\nexport class RateLimitError extends MCPError {\n constructor(retryAfterMs?: number) {\n super(ErrorCodes.RATE_LIMIT_ERROR, 'Rate limit exceeded', {\n ...(retryAfterMs && { retryAfterMs }),\n })\n this.name = 'RateLimitError'\n }\n}\n\n/**\n * Thrown when an operation times out\n */\nexport class TimeoutError extends MCPError {\n constructor(operation: string, timeoutMs: number) {\n super(ErrorCodes.TIMEOUT_ERROR, `Operation '${operation}' timed out after ${timeoutMs}ms`, {\n operation,\n timeoutMs,\n })\n this.name = 'TimeoutError'\n }\n}\n\n/**\n * Thrown when a required dependency is unavailable\n */\nexport class DependencyError extends MCPError {\n constructor(dependency: string, reason: string) {\n super(ErrorCodes.DEPENDENCY_ERROR, `Dependency '${dependency}' unavailable: ${reason}`, {\n dependency,\n reason,\n })\n this.name = 'DependencyError'\n }\n}\n\n/**\n * Thrown when server configuration is invalid\n */\nexport class ConfigurationError extends MCPError {\n constructor(setting: string, reason: string) {\n super(ErrorCodes.CONFIGURATION_ERROR, `Invalid configuration '${setting}': ${reason}`, {\n setting,\n reason,\n })\n this.name = 'ConfigurationError'\n }\n}\n\n/**\n * Thrown when payment is required to access a tool\n */\nexport class PaymentRequiredError extends MCPError {\n constructor(toolName: string, options?: { upgradeUrl?: string; priceId?: string }) {\n super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {\n tool: toolName,\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n ...(options?.priceId && { priceId: options.priceId }),\n })\n this.name = 'PaymentRequiredError'\n }\n}\n\n/**\n * Thrown when user doesn't have enough credits\n */\nexport class InsufficientCreditsError extends MCPError {\n constructor(required: number, available: number, options?: { purchaseUrl?: string }) {\n super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {\n required,\n available,\n ...(options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }),\n })\n this.name = 'InsufficientCreditsError'\n }\n}\n\n/**\n * Thrown when a subscription tier is required\n */\nexport class SubscriptionRequiredError extends MCPError {\n constructor(requiredTier: string, currentTier?: string, options?: { upgradeUrl?: string }) {\n const msg = currentTier \n ? `Subscription '${requiredTier}' required (current: '${currentTier}')`\n : `Subscription '${requiredTier}' required`\n super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {\n requiredTier,\n ...(currentTier && { currentTier }),\n ...(options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }),\n })\n this.name = 'SubscriptionRequiredError'\n }\n}\n","import {\n PaymentRequiredError,\n InsufficientCreditsError,\n SubscriptionRequiredError,\n ConfigurationError,\n} from '../errors'\n\n// ============================================================================\n// Types\n// ============================================================================\n\nexport interface PaymentConfig {\n /** OpenConductor API key for billing */\n apiKey: string\n /** Base URL for billing API (default: https://api.openconductor.ai) */\n apiUrl?: string\n /** Default upgrade URL to show users */\n upgradeUrl?: string\n /** Enable test mode (skips actual billing) */\n testMode?: boolean\n}\n\nexport interface CreditRequirement {\n /** Number of credits to deduct per call */\n credits: number\n}\n\nexport interface SubscriptionRequirement {\n /** Required subscription tier (e.g., 'pro', 'enterprise') */\n tier: string\n /** Alternative tiers that also grant access */\n allowedTiers?: string[]\n}\n\nexport interface StripeRequirement {\n /** Stripe Price ID for per-call billing */\n priceId: string\n}\n\nexport type PaymentRequirement = \n | CreditRequirement \n | SubscriptionRequirement \n | StripeRequirement\n\nexport interface UserContext {\n /** User ID for billing lookup */\n userId: string\n /** Optional API key override */\n apiKey?: string\n}\n\nexport interface BillingStatus {\n /** Whether the user can proceed */\n allowed: boolean\n /** Current credit balance (if applicable) */\n credits?: number\n /** Current subscription tier (if applicable) */\n tier?: string\n /** Reason for denial (if not allowed) */\n reason?: string\n /** URL for upgrade/purchase */\n actionUrl?: string\n}\n\n// ============================================================================\n// Configuration\n// ============================================================================\n\nlet paymentConfig: PaymentConfig | null = null\n\n/**\n * Initialize payment/billing configuration\n * \n * @example\n * ```typescript\n * initPayment({\n * apiKey: process.env.OPENCONDUCTOR_API_KEY!,\n * upgradeUrl: 'https://myapp.com/upgrade'\n * })\n * ```\n */\nexport function initPayment(config: PaymentConfig): void {\n paymentConfig = {\n apiUrl: 'https://api.openconductor.ai',\n testMode: false,\n ...config,\n }\n}\n\n/**\n * Get current payment configuration\n */\nexport function getPaymentConfig(): PaymentConfig | null {\n return paymentConfig\n}\n\n// ============================================================================\n// Billing Client\n// ============================================================================\n\ninterface CheckBillingParams {\n userId: string\n requirement: PaymentRequirement\n toolName: string\n}\n\ninterface DeductCreditsParams {\n userId: string\n credits: number\n toolName: string\n callId?: string\n}\n\nasync function checkBilling(params: CheckBillingParams): Promise<BillingStatus> {\n const config = paymentConfig\n if (!config) {\n throw new ConfigurationError('payment', 'Payment not initialized. Call initPayment() first.')\n }\n\n // Test mode - always allow\n if (config.testMode) {\n return { allowed: true, credits: 9999, tier: 'test' }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-check`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n requirement: params.requirement,\n tool: params.toolName,\n }),\n })\n\n if (!response.ok) {\n // Non-2xx response - treat as billing check failed\n const errorBody = await response.text()\n console.error('[payment] Billing check failed:', response.status, errorBody)\n return {\n allowed: false,\n reason: 'Billing service unavailable',\n actionUrl: config.upgradeUrl,\n }\n }\n\n return await response.json() as BillingStatus\n } catch (error) {\n // Network error - fail open or closed based on config\n console.error('[payment] Billing check error:', error)\n return {\n allowed: false,\n reason: 'Unable to verify billing status',\n actionUrl: config.upgradeUrl,\n }\n }\n}\n\nasync function deductCredits(params: DeductCreditsParams): Promise<boolean> {\n const config = paymentConfig\n if (!config) return false\n\n // Test mode - don't actually deduct\n if (config.testMode) return true\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-deduct`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n userId: params.userId,\n credits: params.credits,\n tool: params.toolName,\n callId: params.callId,\n }),\n })\n\n return response.ok\n } catch (error) {\n console.error('[payment] Credit deduction error:', error)\n return false\n }\n}\n\n// ============================================================================\n// Type Guards\n// ============================================================================\n\nfunction isCreditRequirement(req: PaymentRequirement): req is CreditRequirement {\n return 'credits' in req && typeof req.credits === 'number'\n}\n\nfunction isSubscriptionRequirement(req: PaymentRequirement): req is SubscriptionRequirement {\n return 'tier' in req && typeof req.tier === 'string'\n}\n\nfunction isStripeRequirement(req: PaymentRequirement): req is StripeRequirement {\n return 'priceId' in req && typeof req.priceId === 'string'\n}\n\n// ============================================================================\n// Main Middleware\n// ============================================================================\n\nexport interface RequirePaymentOptions {\n /** Tool name for billing records */\n toolName?: string\n /** Function to extract user ID from input */\n getUserId?: (input: unknown) => string | undefined\n /** Custom error handler */\n onPaymentError?: (error: Error) => void\n}\n\n/**\n * One-line payment middleware for MCP tools\n * \n * @example Credits-based billing\n * ```typescript\n * const paidTool = requirePayment({ credits: 10 })(myHandler)\n * ```\n * \n * @example Subscription tier requirement\n * ```typescript\n * const premiumTool = requirePayment({ tier: 'pro' })(myHandler)\n * ```\n * \n * @example With wrapTool\n * ```typescript\n * const safePaidTool = wrapTool(\n * requirePayment({ credits: 5 })(myHandler),\n * { name: 'premium-analysis' }\n * )\n * ```\n */\nexport function requirePayment<TInput extends { userId?: string }, TOutput>(\n requirement: PaymentRequirement,\n options: RequirePaymentOptions = {}\n) {\n return (\n handler: (input: TInput) => TOutput | Promise<TOutput>\n ): ((input: TInput) => Promise<TOutput>) => {\n \n const { toolName = 'unknown', getUserId } = options\n\n return async (input: TInput): Promise<TOutput> => {\n // Extract user ID\n const userId = getUserId?.(input) ?? input.userId\n \n if (!userId) {\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: paymentConfig?.upgradeUrl,\n })\n }\n\n // Check billing status\n const status = await checkBilling({\n userId,\n requirement,\n toolName,\n })\n\n if (!status.allowed) {\n // Throw appropriate error based on requirement type\n if (isCreditRequirement(requirement)) {\n throw new InsufficientCreditsError(\n requirement.credits,\n status.credits ?? 0,\n { purchaseUrl: status.actionUrl }\n )\n }\n\n if (isSubscriptionRequirement(requirement)) {\n throw new SubscriptionRequiredError(\n requirement.tier,\n status.tier,\n { upgradeUrl: status.actionUrl }\n )\n }\n\n // Generic payment required\n throw new PaymentRequiredError(toolName, {\n upgradeUrl: status.actionUrl,\n ...(isStripeRequirement(requirement) && { priceId: requirement.priceId }),\n })\n }\n\n // Execute the handler\n const result = await handler(input)\n\n // Deduct credits after successful execution (if credit-based)\n if (isCreditRequirement(requirement)) {\n await deductCredits({\n userId,\n credits: requirement.credits,\n toolName,\n })\n }\n\n return result\n }\n }\n}\n\n// ============================================================================\n// Convenience Functions\n// ============================================================================\n\n/**\n * Create a paid tool with built-in payment verification\n * \n * @example\n * ```typescript\n * const analyzeData = createPaidTool({\n * name: 'analyze-data',\n * payment: { credits: 10 },\n * handler: async (input) => {\n * // Your tool logic\n * return result\n * }\n * })\n * ```\n */\nexport function createPaidTool<TInput extends { userId?: string }, TOutput>(config: {\n name: string\n payment: PaymentRequirement\n handler: (input: TInput) => TOutput | Promise<TOutput>\n getUserId?: (input: TInput) => string | undefined\n}): (input: TInput) => Promise<TOutput> {\n return requirePayment<TInput, TOutput>(config.payment, {\n toolName: config.name,\n getUserId: config.getUserId as (input: unknown) => string | undefined,\n })(config.handler)\n}\n\n/**\n * Check if a user can access a paid feature without executing it\n * Useful for UI gating\n * \n * @example\n * ```typescript\n * const canAccess = await canUserAccess('user_123', { credits: 10 }, 'premium-tool')\n * if (!canAccess.allowed) {\n * showUpgradePrompt(canAccess.actionUrl)\n * }\n * ```\n */\nexport async function canUserAccess(\n userId: string,\n requirement: PaymentRequirement,\n toolName: string = 'check'\n): Promise<BillingStatus> {\n return checkBilling({ userId, requirement, toolName })\n}\n\n/**\n * Get user's current billing status\n */\nexport async function getUserBillingStatus(userId: string): Promise<{\n credits: number\n tier: string\n active: boolean\n} | null> {\n const config = paymentConfig\n if (!config) return null\n\n if (config.testMode) {\n return { credits: 9999, tier: 'test', active: true }\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/billing-status/${userId}`, {\n headers: {\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n })\n\n if (!response.ok) return null\n return await response.json()\n } catch {\n return null\n }\n}\n\n// ============================================================================\n// Checkout Helpers\n// ============================================================================\n\nexport type CreditPack = 'starter' | 'pro' | 'business'\n\nexport interface CreditPackInfo {\n name: string\n credits: number\n price: number\n perCredit: number\n savings: number\n bestFor: string\n popular?: boolean\n}\n\nexport interface CheckoutSession {\n sessionId: string\n url: string\n pack: CreditPack\n credits: number\n price: number\n perCredit: number\n}\n\n/**\n * Get available credit packs and pricing\n * \n * @example\n * ```typescript\n * const packs = await getCreditPacks()\n * console.log(packs.pro) // { name: 'Pro Pack', credits: 500, price: 39.99, ... }\n * ```\n */\nexport async function getCreditPacks(): Promise<Record<CreditPack, CreditPackInfo> | null> {\n const config = paymentConfig\n if (!config) return null\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/credit-packs`)\n if (!response.ok) return null\n const data = await response.json()\n return data.packs\n } catch {\n return null\n }\n}\n\n/**\n * Create a Stripe checkout session for purchasing credits\n * Returns a URL to redirect the user to for payment\n * \n * @example\n * ```typescript\n * const checkout = await createCreditsCheckout('pro', {\n * successUrl: 'https://myapp.com/success',\n * cancelUrl: 'https://myapp.com/pricing',\n * })\n * \n * // Redirect user to checkout\n * window.location.href = checkout.url\n * ```\n */\nexport async function createCreditsCheckout(\n pack: CreditPack,\n options: {\n successUrl?: string\n cancelUrl?: string\n } = {}\n): Promise<CheckoutSession | null> {\n const config = paymentConfig\n if (!config) {\n console.error('[payment] Payment not initialized. Call initPayment() first.')\n return null\n }\n\n try {\n const response = await fetch(`${config.apiUrl}/functions/v1/stripe-checkout-credits`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${config.apiKey}`,\n },\n body: JSON.stringify({\n pack,\n successUrl: options.successUrl,\n cancelUrl: options.cancelUrl,\n }),\n })\n\n if (!response.ok) {\n const error = await response.text()\n console.error('[payment] Checkout creation failed:', error)\n return null\n }\n\n return await response.json() as CheckoutSession\n } catch (error) {\n console.error('[payment] Checkout error:', error)\n return null\n }\n}\n\n/**\n * Generate a direct checkout URL for embedding in upgrade prompts\n * \n * @example In an error handler:\n * ```typescript\n * catch (error) {\n * if (error instanceof InsufficientCreditsError) {\n * const checkoutUrl = await getCheckoutUrl('pro')\n * return { error: `Insufficient credits. Buy more: ${checkoutUrl}` }\n * }\n * }\n * ```\n */\nexport async function getCheckoutUrl(pack: CreditPack): Promise<string | null> {\n const session = await createCreditsCheckout(pack)\n return session?.url ?? null\n}\n\n// ============================================================================\n// Upgrade Prompts\n// ============================================================================\n\nexport interface UpgradePromptOptions {\n /** Required credits for the blocked action */\n required?: number\n /** Currently available credits */\n available?: number\n /** Tool that was blocked */\n toolName?: string\n /** Include direct checkout links */\n includeLinks?: boolean\n}\n\n/**\n * Generate a user-friendly upgrade prompt message\n * Use this in your error handlers to show helpful messages\n * \n * @example\n * ```typescript\n * catch (error) {\n * if (error instanceof InsufficientCreditsError) {\n * const prompt = await generateUpgradePrompt({\n * required: error.data.required,\n * available: error.data.available,\n * toolName: 'analyze-data'\n * })\n * console.log(prompt.message)\n * // \"You need 10 more credits to use analyze-data. \n * // Buy the Pro Pack (500 credits) for $39.99 → https://...\"\n * }\n * }\n * ```\n */\nexport async function generateUpgradePrompt(\n options: UpgradePromptOptions = {}\n): Promise<{\n message: string\n shortMessage: string\n recommendedPack: CreditPack\n packs: Record<CreditPack, { credits: number; price: number; url: string | null }>\n}> {\n const { required = 0, available = 0, toolName, includeLinks = true } = options\n const needed = Math.max(0, required - available)\n \n // Determine recommended pack based on needed credits\n let recommendedPack: CreditPack = 'starter'\n if (needed > 100) recommendedPack = 'pro'\n if (needed > 500) recommendedPack = 'business'\n \n // Get pack URLs if requested\n const packUrls: Record<CreditPack, string | null> = {\n starter: null,\n pro: null,\n business: null,\n }\n \n if (includeLinks) {\n const [starterUrl, proUrl, businessUrl] = await Promise.all([\n getCheckoutUrl('starter'),\n getCheckoutUrl('pro'),\n getCheckoutUrl('business'),\n ])\n packUrls.starter = starterUrl\n packUrls.pro = proUrl\n packUrls.business = businessUrl\n }\n \n // Build message\n const toolPart = toolName ? ` to use ${toolName}` : ''\n const shortMessage = `Insufficient credits: need ${required}, have ${available}`\n \n let message = `You need ${needed} more credits${toolPart}.\\n\\n`\n message += `💳 Quick top-up options:\\n`\n message += ` • Starter: 100 credits for $9.99${packUrls.starter ? ` → ${packUrls.starter}` : ''}\\n`\n message += ` • Pro: 500 credits for $39.99 (20% off)${packUrls.pro ? ` → ${packUrls.pro}` : ''}\\n`\n message += ` • Business: 2,000 credits for $119.99 (40% off)${packUrls.business ? ` → ${packUrls.business}` : ''}\\n`\n \n if (recommendedPack !== 'starter') {\n message += `\\n✨ Recommended: ${recommendedPack.charAt(0).toUpperCase() + recommendedPack.slice(1)} Pack`\n }\n \n return {\n message,\n shortMessage,\n recommendedPack,\n packs: {\n starter: { credits: 100, price: 9.99, url: packUrls.starter },\n pro: { credits: 500, price: 39.99, url: packUrls.pro },\n business: { credits: 2000, price: 119.99, url: packUrls.business },\n },\n }\n}\n\n/**\n * Get a simple checkout URL for the dashboard\n * Useful when you don't want to create a full Stripe session\n */\nexport function getDashboardCheckoutUrl(options: {\n required?: number\n available?: number\n pack?: CreditPack\n} = {}): string {\n const params = new URLSearchParams()\n params.set('action', 'buy-credits')\n if (options.required) params.set('required', String(options.required))\n if (options.available) params.set('available', String(options.available))\n if (options.pack) params.set('pack', options.pack)\n return `https://dashboard.openconductor.ai?${params.toString()}`\n}\n\n/**\n * Create an error handler that automatically generates upgrade prompts\n * \n * @example\n * ```typescript\n * const handleError = createUpgradeErrorHandler({\n * onInsufficientCredits: (prompt) => {\n * return { error: prompt.shortMessage, upgradeUrl: prompt.packs.pro.url }\n * }\n * })\n * \n * // In your tool:\n * try {\n * return await paidHandler(input)\n * } catch (error) {\n * return handleError(error)\n * }\n * ```\n */\nexport function createUpgradeErrorHandler(handlers: {\n onInsufficientCredits?: (prompt: Awaited<ReturnType<typeof generateUpgradePrompt>>) => unknown\n onSubscriptionRequired?: (error: SubscriptionRequiredError) => unknown\n onPaymentRequired?: (error: PaymentRequiredError) => unknown\n onOtherError?: (error: Error) => unknown\n}) {\n return async (error: Error): Promise<unknown> => {\n if (error instanceof InsufficientCreditsError && handlers.onInsufficientCredits) {\n const prompt = await generateUpgradePrompt({\n required: error.data?.required as number,\n available: error.data?.available as number,\n })\n return handlers.onInsufficientCredits(prompt)\n }\n \n if (error instanceof SubscriptionRequiredError && handlers.onSubscriptionRequired) {\n return handlers.onSubscriptionRequired(error)\n }\n \n if (error instanceof PaymentRequiredError && handlers.onPaymentRequired) {\n return handlers.onPaymentRequired(error)\n }\n \n if (handlers.onOtherError) {\n return handlers.onOtherError(error)\n }\n \n throw error\n }\n}\n"]}