@openconductor/mcp-sdk 1.0.2 → 1.2.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.
- package/README.md +30 -3
- package/dist/errors/index.d.mts +29 -1
- package/dist/errors/index.d.ts +29 -1
- package/dist/errors/index.js +38 -1
- package/dist/errors/index.js.map +1 -1
- package/dist/errors/index.mjs +36 -2
- package/dist/errors/index.mjs.map +1 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +204 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +196 -2
- package/dist/index.mjs.map +1 -1
- package/dist/payment/index.d.mts +137 -0
- package/dist/payment/index.d.ts +137 -0
- package/dist/payment/index.js +253 -0
- package/dist/payment/index.js.map +1 -0
- package/dist/payment/index.mjs +246 -0
- package/dist/payment/index.mjs.map +1 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs.map +1 -1
- package/dist/validate/index.js.map +1 -1
- package/dist/validate/index.mjs.map +1 -1
- package/package.json +10 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
**The standard SDK for building production-ready MCP servers.**
|
|
4
4
|
|
|
5
|
-
Stop copy-pasting boilerplate. Get error handling, validation, logging, and
|
|
5
|
+
Stop copy-pasting boilerplate. Get error handling, validation, logging, telemetry, and **one-line monetization** out of the box.
|
|
6
6
|
|
|
7
7
|
[](https://www.npmjs.com/package/@openconductor/mcp-sdk)
|
|
8
8
|
[](https://www.npmjs.com/package/@openconductor/mcp-sdk)
|
|
@@ -19,9 +19,10 @@ Every MCP server needs the same things:
|
|
|
19
19
|
| **Input Validation** | Manual checks, type-unsafe | Zod schemas, fully typed |
|
|
20
20
|
| **Logging** | console.log chaos | Structured JSON, log-aggregator ready |
|
|
21
21
|
| **Telemetry** | Flying blind | One-line setup, real dashboards |
|
|
22
|
+
| **Monetization** | Build your own billing | One-line `requirePayment()` |
|
|
22
23
|
| **Timeouts** | None (hung requests) | Automatic with configurable limits |
|
|
23
24
|
|
|
24
|
-
All in **~
|
|
25
|
+
All in **~20kb**, zero config required.
|
|
25
26
|
|
|
26
27
|
## Install
|
|
27
28
|
|
|
@@ -169,13 +170,37 @@ initTelemetry({
|
|
|
169
170
|
|
|
170
171
|
[→ Telemetry Guide](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/telemetry.md)
|
|
171
172
|
|
|
173
|
+
### 💰 One-Line Monetization
|
|
174
|
+
|
|
175
|
+
Charge for your MCP tools with credits, subscriptions, or per-call:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import { initPayment, requirePayment } from '@openconductor/mcp-sdk/payment'
|
|
179
|
+
|
|
180
|
+
initPayment({ apiKey: 'oc_xxx' })
|
|
181
|
+
|
|
182
|
+
// Credits-based
|
|
183
|
+
const paidTool = requirePayment({ credits: 10 })(myHandler)
|
|
184
|
+
|
|
185
|
+
// Subscription tier
|
|
186
|
+
const premiumTool = requirePayment({ tier: 'pro' })(myHandler)
|
|
187
|
+
|
|
188
|
+
// Works with wrapTool
|
|
189
|
+
const safePaidTool = wrapTool(
|
|
190
|
+
requirePayment({ credits: 5 })(myHandler),
|
|
191
|
+
{ name: 'premium-analysis' }
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
[→ Monetization Guide](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/payment.md)
|
|
196
|
+
|
|
172
197
|
## Tree-Shakeable Imports
|
|
173
198
|
|
|
174
199
|
Import only what you need:
|
|
175
200
|
|
|
176
201
|
```typescript
|
|
177
202
|
// Full SDK
|
|
178
|
-
import { z, validate, wrapTool, createLogger } from '@openconductor/mcp-sdk'
|
|
203
|
+
import { z, validate, wrapTool, createLogger, requirePayment } from '@openconductor/mcp-sdk'
|
|
179
204
|
|
|
180
205
|
// Or specific modules (smaller bundles)
|
|
181
206
|
import { ValidationError } from '@openconductor/mcp-sdk/errors'
|
|
@@ -183,6 +208,7 @@ import { z, validate } from '@openconductor/mcp-sdk/validate'
|
|
|
183
208
|
import { createLogger } from '@openconductor/mcp-sdk/logger'
|
|
184
209
|
import { wrapTool } from '@openconductor/mcp-sdk/server'
|
|
185
210
|
import { initTelemetry } from '@openconductor/mcp-sdk/telemetry'
|
|
211
|
+
import { requirePayment } from '@openconductor/mcp-sdk/payment'
|
|
186
212
|
```
|
|
187
213
|
|
|
188
214
|
## Documentation
|
|
@@ -191,6 +217,7 @@ import { initTelemetry } from '@openconductor/mcp-sdk/telemetry'
|
|
|
191
217
|
- **[Error Handling](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/errors.md)** — All error types and usage
|
|
192
218
|
- **[Validation](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/validation.md)** — Schema patterns and helpers
|
|
193
219
|
- **[Telemetry](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/telemetry.md)** — Observability setup
|
|
220
|
+
- **[Monetization](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/payment.md)** — One-line payment gates
|
|
194
221
|
- **[API Reference](https://github.com/epicmotionSD/mcp-sdk/blob/main/docs/api-reference.md)** — Complete API docs
|
|
195
222
|
|
|
196
223
|
## Examples
|
package/dist/errors/index.d.mts
CHANGED
|
@@ -18,6 +18,9 @@ declare const ErrorCodes: {
|
|
|
18
18
|
readonly VALIDATION_ERROR: -32008;
|
|
19
19
|
readonly DEPENDENCY_ERROR: -32009;
|
|
20
20
|
readonly CONFIGURATION_ERROR: -32010;
|
|
21
|
+
readonly PAYMENT_REQUIRED: -32011;
|
|
22
|
+
readonly INSUFFICIENT_CREDITS: -32012;
|
|
23
|
+
readonly SUBSCRIPTION_REQUIRED: -32013;
|
|
21
24
|
};
|
|
22
25
|
type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
23
26
|
|
|
@@ -110,5 +113,30 @@ declare class DependencyError extends MCPError {
|
|
|
110
113
|
declare class ConfigurationError extends MCPError {
|
|
111
114
|
constructor(setting: string, reason: string);
|
|
112
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Thrown when payment is required to access a tool
|
|
118
|
+
*/
|
|
119
|
+
declare class PaymentRequiredError extends MCPError {
|
|
120
|
+
constructor(toolName: string, options?: {
|
|
121
|
+
upgradeUrl?: string;
|
|
122
|
+
priceId?: string;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Thrown when user doesn't have enough credits
|
|
127
|
+
*/
|
|
128
|
+
declare class InsufficientCreditsError extends MCPError {
|
|
129
|
+
constructor(required: number, available: number, options?: {
|
|
130
|
+
purchaseUrl?: string;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Thrown when a subscription tier is required
|
|
135
|
+
*/
|
|
136
|
+
declare class SubscriptionRequiredError extends MCPError {
|
|
137
|
+
constructor(requiredTier: string, currentTier?: string, options?: {
|
|
138
|
+
upgradeUrl?: string;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
113
141
|
|
|
114
|
-
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, type ErrorCode, ErrorCodes, MCPError, RateLimitError, ResourceNotFoundError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError };
|
|
142
|
+
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, type ErrorCode, ErrorCodes, InsufficientCreditsError, MCPError, PaymentRequiredError, RateLimitError, ResourceNotFoundError, SubscriptionRequiredError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError };
|
package/dist/errors/index.d.ts
CHANGED
|
@@ -18,6 +18,9 @@ declare const ErrorCodes: {
|
|
|
18
18
|
readonly VALIDATION_ERROR: -32008;
|
|
19
19
|
readonly DEPENDENCY_ERROR: -32009;
|
|
20
20
|
readonly CONFIGURATION_ERROR: -32010;
|
|
21
|
+
readonly PAYMENT_REQUIRED: -32011;
|
|
22
|
+
readonly INSUFFICIENT_CREDITS: -32012;
|
|
23
|
+
readonly SUBSCRIPTION_REQUIRED: -32013;
|
|
21
24
|
};
|
|
22
25
|
type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
|
|
23
26
|
|
|
@@ -110,5 +113,30 @@ declare class DependencyError extends MCPError {
|
|
|
110
113
|
declare class ConfigurationError extends MCPError {
|
|
111
114
|
constructor(setting: string, reason: string);
|
|
112
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Thrown when payment is required to access a tool
|
|
118
|
+
*/
|
|
119
|
+
declare class PaymentRequiredError extends MCPError {
|
|
120
|
+
constructor(toolName: string, options?: {
|
|
121
|
+
upgradeUrl?: string;
|
|
122
|
+
priceId?: string;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Thrown when user doesn't have enough credits
|
|
127
|
+
*/
|
|
128
|
+
declare class InsufficientCreditsError extends MCPError {
|
|
129
|
+
constructor(required: number, available: number, options?: {
|
|
130
|
+
purchaseUrl?: string;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Thrown when a subscription tier is required
|
|
135
|
+
*/
|
|
136
|
+
declare class SubscriptionRequiredError extends MCPError {
|
|
137
|
+
constructor(requiredTier: string, currentTier?: string, options?: {
|
|
138
|
+
upgradeUrl?: string;
|
|
139
|
+
});
|
|
140
|
+
}
|
|
113
141
|
|
|
114
|
-
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, type ErrorCode, ErrorCodes, MCPError, RateLimitError, ResourceNotFoundError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError };
|
|
142
|
+
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, type ErrorCode, ErrorCodes, InsufficientCreditsError, MCPError, PaymentRequiredError, RateLimitError, ResourceNotFoundError, SubscriptionRequiredError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError };
|
package/dist/errors/index.js
CHANGED
|
@@ -18,7 +18,10 @@ var ErrorCodes = {
|
|
|
18
18
|
TIMEOUT_ERROR: -32007,
|
|
19
19
|
VALIDATION_ERROR: -32008,
|
|
20
20
|
DEPENDENCY_ERROR: -32009,
|
|
21
|
-
CONFIGURATION_ERROR: -32010
|
|
21
|
+
CONFIGURATION_ERROR: -32010,
|
|
22
|
+
PAYMENT_REQUIRED: -32011,
|
|
23
|
+
INSUFFICIENT_CREDITS: -32012,
|
|
24
|
+
SUBSCRIPTION_REQUIRED: -32013
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
// src/errors/index.ts
|
|
@@ -142,15 +145,49 @@ var ConfigurationError = class extends MCPError {
|
|
|
142
145
|
this.name = "ConfigurationError";
|
|
143
146
|
}
|
|
144
147
|
};
|
|
148
|
+
var PaymentRequiredError = class extends MCPError {
|
|
149
|
+
constructor(toolName, options) {
|
|
150
|
+
super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {
|
|
151
|
+
tool: toolName,
|
|
152
|
+
...options?.upgradeUrl && { upgradeUrl: options.upgradeUrl },
|
|
153
|
+
...options?.priceId && { priceId: options.priceId }
|
|
154
|
+
});
|
|
155
|
+
this.name = "PaymentRequiredError";
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
var InsufficientCreditsError = class extends MCPError {
|
|
159
|
+
constructor(required, available, options) {
|
|
160
|
+
super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {
|
|
161
|
+
required,
|
|
162
|
+
available,
|
|
163
|
+
...options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }
|
|
164
|
+
});
|
|
165
|
+
this.name = "InsufficientCreditsError";
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
var SubscriptionRequiredError = class extends MCPError {
|
|
169
|
+
constructor(requiredTier, currentTier, options) {
|
|
170
|
+
const msg = currentTier ? `Subscription '${requiredTier}' required (current: '${currentTier}')` : `Subscription '${requiredTier}' required`;
|
|
171
|
+
super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {
|
|
172
|
+
requiredTier,
|
|
173
|
+
...currentTier && { currentTier },
|
|
174
|
+
...options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }
|
|
175
|
+
});
|
|
176
|
+
this.name = "SubscriptionRequiredError";
|
|
177
|
+
}
|
|
178
|
+
};
|
|
145
179
|
|
|
146
180
|
exports.AuthenticationError = AuthenticationError;
|
|
147
181
|
exports.AuthorizationError = AuthorizationError;
|
|
148
182
|
exports.ConfigurationError = ConfigurationError;
|
|
149
183
|
exports.DependencyError = DependencyError;
|
|
150
184
|
exports.ErrorCodes = ErrorCodes;
|
|
185
|
+
exports.InsufficientCreditsError = InsufficientCreditsError;
|
|
151
186
|
exports.MCPError = MCPError;
|
|
187
|
+
exports.PaymentRequiredError = PaymentRequiredError;
|
|
152
188
|
exports.RateLimitError = RateLimitError;
|
|
153
189
|
exports.ResourceNotFoundError = ResourceNotFoundError;
|
|
190
|
+
exports.SubscriptionRequiredError = SubscriptionRequiredError;
|
|
154
191
|
exports.TimeoutError = TimeoutError;
|
|
155
192
|
exports.ToolExecutionError = ToolExecutionError;
|
|
156
193
|
exports.ToolNotFoundError = ToolNotFoundError;
|
package/dist/errors/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA;AAAA,EAExB,WAAA,EAAa,MAAA;AAAA,EACb,eAAA,EAAiB,MAAA;AAAA,EACjB,gBAAA,EAAkB,MAAA;AAAA,EAClB,cAAA,EAAgB,MAAA;AAAA,EAChB,cAAA,EAAgB,MAAA;AAAA;AAAA,EAGhB,cAAA,EAAgB,MAAA;AAAA,EAChB,oBAAA,EAAsB,MAAA;AAAA,EACtB,kBAAA,EAAoB,MAAA;AAAA,EACpB,oBAAA,EAAsB,MAAA;AAAA,EACtB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,aAAA,EAAe,MAAA;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A;AAAA,KACD,CAAA;AACD,IAAA,IAAA,CAAK,IAAA,GAAO,iBAAA;AAAA,EACd;AACF;AAKO,IAAM,kBAAA,GAAN,cAAiC,QAAA,CAAS;AAAA,EAC/C,WAAA,CAAY,SAAiB,MAAA,EAAgB;AAC3C,IAAA,KAAA,CAAM,WAAW,mBAAA,EAAqB,CAAA,uBAAA,EAA0B,OAAO,CAAA,GAAA,EAAM,MAAM,CAAA,CAAA,EAAI;AAAA,MACrF,OAAA;AAAA,MACA;AAAA,KACD,CAAA;AACD,IAAA,IAAA,CAAK,IAAA,GAAO,oBAAA;AAAA,EACd;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} 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"]}
|
|
1
|
+
{"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts"],"names":[],"mappings":";;;AAIO,IAAM,UAAA,GAAa;AAAA;AAAA,EAExB,WAAA,EAAa,MAAA;AAAA,EACb,eAAA,EAAiB,MAAA;AAAA,EACjB,gBAAA,EAAkB,MAAA;AAAA,EAClB,cAAA,EAAgB,MAAA;AAAA,EAChB,cAAA,EAAgB,MAAA;AAAA;AAAA,EAGhB,cAAA,EAAgB,MAAA;AAAA,EAChB,oBAAA,EAAsB,MAAA;AAAA,EACtB,kBAAA,EAAoB,MAAA;A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B,QAAQ,CAAA,OAAA,EAAU,SAAS,CAAA,CAAA,EAAI;AAAA,MAClG,QAAA;AAAA,MACA,SAAA;AAAA,MACA,GAAI,OAAA,EAAS,WAAA,IAAe,EAAE,WAAA,EAAa,QAAQ,WAAA;AAAY,KAChE,CAAA;AACD,IAAA,IAAA,CAAK,IAAA,GAAO,0BAAA;AAAA,EACd;AACF;AAKO,IAAM,yBAAA,GAAN,cAAwC,QAAA,CAAS;AAAA,EACtD,WAAA,CAAY,YAAA,EAAsB,WAAA,EAAsB,OAAA,EAAmC;AACzF,IAAA,MAAM,GAAA,GAAM,cACR,CAAA,cAAA,EAAiB,YAAY,yBAAyB,WAAW,CAAA,EAAA,CAAA,GACjE,iBAAiB,YAAY,CAAA,UAAA,CAAA;AACjC,IAAA,KAAA,CAAM,UAAA,CAAW,uBAAuB,GAAA,EAAK;AAAA,MAC3C,YAAA;AAAA,MACA,GAAI,WAAA,IAAe,EAAE,WAAA,EAAY;AAAA,MACjC,GAAI,OAAA,EAAS,UAAA,IAAc,EAAE,UAAA,EAAY,QAAQ,UAAA;AAAW,KAC7D,CAAA;AACD,IAAA,IAAA,CAAK,IAAA,GAAO,2BAAA;AAAA,EACd;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"]}
|
package/dist/errors/index.mjs
CHANGED
|
@@ -16,7 +16,10 @@ var ErrorCodes = {
|
|
|
16
16
|
TIMEOUT_ERROR: -32007,
|
|
17
17
|
VALIDATION_ERROR: -32008,
|
|
18
18
|
DEPENDENCY_ERROR: -32009,
|
|
19
|
-
CONFIGURATION_ERROR: -32010
|
|
19
|
+
CONFIGURATION_ERROR: -32010,
|
|
20
|
+
PAYMENT_REQUIRED: -32011,
|
|
21
|
+
INSUFFICIENT_CREDITS: -32012,
|
|
22
|
+
SUBSCRIPTION_REQUIRED: -32013
|
|
20
23
|
};
|
|
21
24
|
|
|
22
25
|
// src/errors/index.ts
|
|
@@ -140,7 +143,38 @@ var ConfigurationError = class extends MCPError {
|
|
|
140
143
|
this.name = "ConfigurationError";
|
|
141
144
|
}
|
|
142
145
|
};
|
|
146
|
+
var PaymentRequiredError = class extends MCPError {
|
|
147
|
+
constructor(toolName, options) {
|
|
148
|
+
super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {
|
|
149
|
+
tool: toolName,
|
|
150
|
+
...options?.upgradeUrl && { upgradeUrl: options.upgradeUrl },
|
|
151
|
+
...options?.priceId && { priceId: options.priceId }
|
|
152
|
+
});
|
|
153
|
+
this.name = "PaymentRequiredError";
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
var InsufficientCreditsError = class extends MCPError {
|
|
157
|
+
constructor(required, available, options) {
|
|
158
|
+
super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {
|
|
159
|
+
required,
|
|
160
|
+
available,
|
|
161
|
+
...options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }
|
|
162
|
+
});
|
|
163
|
+
this.name = "InsufficientCreditsError";
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
var SubscriptionRequiredError = class extends MCPError {
|
|
167
|
+
constructor(requiredTier, currentTier, options) {
|
|
168
|
+
const msg = currentTier ? `Subscription '${requiredTier}' required (current: '${currentTier}')` : `Subscription '${requiredTier}' required`;
|
|
169
|
+
super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {
|
|
170
|
+
requiredTier,
|
|
171
|
+
...currentTier && { currentTier },
|
|
172
|
+
...options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }
|
|
173
|
+
});
|
|
174
|
+
this.name = "SubscriptionRequiredError";
|
|
175
|
+
}
|
|
176
|
+
};
|
|
143
177
|
|
|
144
|
-
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, ErrorCodes, MCPError, RateLimitError, ResourceNotFoundError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError };
|
|
178
|
+
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, ErrorCodes, InsufficientCreditsError, MCPError, PaymentRequiredError, RateLimitError, ResourceNotFoundError, SubscriptionRequiredError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError };
|
|
145
179
|
//# sourceMappingURL=index.mjs.map
|
|
146
180
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts"],"names":[],"mappings":";AAIO,IAAM,UAAA,GAAa;AAAA;AAAA,EAExB,WAAA,EAAa,MAAA;AAAA,EACb,eAAA,EAAiB,MAAA;AAAA,EACjB,gBAAA,EAAkB,MAAA;AAAA,EAClB,cAAA,EAAgB,MAAA;AAAA,EAChB,cAAA,EAAgB,MAAA;AAAA;AAAA,EAGhB,cAAA,EAAgB,MAAA;AAAA,EAChB,oBAAA,EAAsB,MAAA;AAAA,EACtB,kBAAA,EAAoB,MAAA;AAAA,EACpB,oBAAA,EAAsB,MAAA;AAAA,EACtB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,aAAA,EAAe,MAAA;AAAA,EACf,gBAAA,EAAkB,MAAA;AAAA,EAClB,gBAAA,EAAkB,MAAA;AAAA,EAClB,mBAAA,EAAqB;AACvB;;;ACfO,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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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} 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"]}
|
|
1
|
+
{"version":3,"sources":["../../src/errors/codes.ts","../../src/errors/index.ts"],"names":[],"mappings":";AAIO,IAAM,UAAA,GAAa;AAAA;AAAA,EAExB,WAAA,EAAa,MAAA;AAAA,EACb,eAAA,EAAiB,MAAA;AAAA,EACjB,gBAAA,EAAkB,MAAA;AAAA,EAClB,cAAA,EAAgB,MAAA;AAAA,EAChB,cAAA,EAAgB,MAAA;AAAA;AAAA,EAGhB,cAAA,EAAgB,MAAA;AAAA,EAChB,oBAAA,EAAsB,MAAA;AAAA,EACtB,kBAAA,EAAoB,MAAA;AAAA,EACpB,oBAAA,EAAsB,MAAA;AAAA,EACtB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,aAAA,EAAe,MAAA;AAAA,EACf,gBAAA,EAAkB,MAAA;AAAA,EAClB,gBAAA,EAAkB,MAAA;AAAA,EAClB,mBAAA,EAAqB,MAAA;AAAA,EACrB,gBAAA,EAAkB,MAAA;AAAA,EAClB,oBAAA,EAAsB,MAAA;AAAA,EACtB,qBAAA,EAAuB;AACzB;;;AClBO,IAAM,QAAA,GAAN,cAAuB,KAAA,CAAM;AAAA,EAClB,IAAA;AAAA,EACA,IAAA;AAAA,EAEh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WAAW,CAAA,EAAA,CAAA,GACjE,iBAAiB,YAAY,CAAA,UAAA,CAAA;AACjC,IAAA,KAAA,CAAM,UAAA,CAAW,uBAAuB,GAAA,EAAK;AAAA,MAC3C,YAAA;AAAA,MACA,GAAI,WAAA,IAAe,EAAE,WAAA,EAAY;AAAA,MACjC,GAAI,OAAA,EAAS,UAAA,IAAc,EAAE,UAAA,EAAY,QAAQ,UAAA;AAAW,KAC7D,CAAA;AACD,IAAA,IAAA,CAAK,IAAA,GAAO,2BAAA;AAAA,EACd;AACF","file":"index.mjs","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"]}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, ErrorCode, ErrorCodes, MCPError, RateLimitError, ResourceNotFoundError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError } from './errors/index.mjs';
|
|
1
|
+
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, ErrorCode, ErrorCodes, InsufficientCreditsError, MCPError, PaymentRequiredError, RateLimitError, ResourceNotFoundError, SubscriptionRequiredError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError } from './errors/index.mjs';
|
|
2
2
|
export { Infer, ValidateOptions, schemas, validate, validateInput } from './validate/index.mjs';
|
|
3
3
|
export { LogEntry, LogLevel, Logger, LoggerOptions, createLogger } from './logger/index.mjs';
|
|
4
4
|
export { HealthCheckInfo, HealthCheckResponse, ToolContext, WrapToolOptions, createHealthCheck, wrapTool } from './server/index.mjs';
|
|
5
5
|
export { Telemetry, TelemetryBatch, TelemetryConfig, ToolMetric, getTelemetry, initTelemetry } from './telemetry/index.mjs';
|
|
6
|
+
export { BillingStatus, CreditRequirement, PaymentConfig, PaymentRequirement, RequirePaymentOptions, StripeRequirement, SubscriptionRequirement, UserContext, canUserAccess, createPaidTool, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment } from './payment/index.mjs';
|
|
6
7
|
export { ZodError, ZodSchema, z } from 'zod';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, ErrorCode, ErrorCodes, MCPError, RateLimitError, ResourceNotFoundError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError } from './errors/index.js';
|
|
1
|
+
export { AuthenticationError, AuthorizationError, ConfigurationError, DependencyError, ErrorCode, ErrorCodes, InsufficientCreditsError, MCPError, PaymentRequiredError, RateLimitError, ResourceNotFoundError, SubscriptionRequiredError, TimeoutError, ToolExecutionError, ToolNotFoundError, ValidationError } from './errors/index.js';
|
|
2
2
|
export { Infer, ValidateOptions, schemas, validate, validateInput } from './validate/index.js';
|
|
3
3
|
export { LogEntry, LogLevel, Logger, LoggerOptions, createLogger } from './logger/index.js';
|
|
4
4
|
export { HealthCheckInfo, HealthCheckResponse, ToolContext, WrapToolOptions, createHealthCheck, wrapTool } from './server/index.js';
|
|
5
5
|
export { Telemetry, TelemetryBatch, TelemetryConfig, ToolMetric, getTelemetry, initTelemetry } from './telemetry/index.js';
|
|
6
|
+
export { BillingStatus, CreditRequirement, PaymentConfig, PaymentRequirement, RequirePaymentOptions, StripeRequirement, SubscriptionRequirement, UserContext, canUserAccess, createPaidTool, getPaymentConfig, getUserBillingStatus, initPayment, requirePayment } from './payment/index.js';
|
|
6
7
|
export { ZodError, ZodSchema, z } from 'zod';
|
package/dist/index.js
CHANGED
|
@@ -20,7 +20,10 @@ var ErrorCodes = {
|
|
|
20
20
|
TIMEOUT_ERROR: -32007,
|
|
21
21
|
VALIDATION_ERROR: -32008,
|
|
22
22
|
DEPENDENCY_ERROR: -32009,
|
|
23
|
-
CONFIGURATION_ERROR: -32010
|
|
23
|
+
CONFIGURATION_ERROR: -32010,
|
|
24
|
+
PAYMENT_REQUIRED: -32011,
|
|
25
|
+
INSUFFICIENT_CREDITS: -32012,
|
|
26
|
+
SUBSCRIPTION_REQUIRED: -32013
|
|
24
27
|
};
|
|
25
28
|
|
|
26
29
|
// src/errors/index.ts
|
|
@@ -144,6 +147,37 @@ var ConfigurationError = class extends MCPError {
|
|
|
144
147
|
this.name = "ConfigurationError";
|
|
145
148
|
}
|
|
146
149
|
};
|
|
150
|
+
var PaymentRequiredError = class extends MCPError {
|
|
151
|
+
constructor(toolName, options) {
|
|
152
|
+
super(ErrorCodes.PAYMENT_REQUIRED, `Payment required to use '${toolName}'`, {
|
|
153
|
+
tool: toolName,
|
|
154
|
+
...options?.upgradeUrl && { upgradeUrl: options.upgradeUrl },
|
|
155
|
+
...options?.priceId && { priceId: options.priceId }
|
|
156
|
+
});
|
|
157
|
+
this.name = "PaymentRequiredError";
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var InsufficientCreditsError = class extends MCPError {
|
|
161
|
+
constructor(required, available, options) {
|
|
162
|
+
super(ErrorCodes.INSUFFICIENT_CREDITS, `Insufficient credits: need ${required}, have ${available}`, {
|
|
163
|
+
required,
|
|
164
|
+
available,
|
|
165
|
+
...options?.purchaseUrl && { purchaseUrl: options.purchaseUrl }
|
|
166
|
+
});
|
|
167
|
+
this.name = "InsufficientCreditsError";
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var SubscriptionRequiredError = class extends MCPError {
|
|
171
|
+
constructor(requiredTier, currentTier, options) {
|
|
172
|
+
const msg = currentTier ? `Subscription '${requiredTier}' required (current: '${currentTier}')` : `Subscription '${requiredTier}' required`;
|
|
173
|
+
super(ErrorCodes.SUBSCRIPTION_REQUIRED, msg, {
|
|
174
|
+
requiredTier,
|
|
175
|
+
...currentTier && { currentTier },
|
|
176
|
+
...options?.upgradeUrl && { upgradeUrl: options.upgradeUrl }
|
|
177
|
+
});
|
|
178
|
+
this.name = "SubscriptionRequiredError";
|
|
179
|
+
}
|
|
180
|
+
};
|
|
147
181
|
function validate(schema, input, options = {}) {
|
|
148
182
|
const { stripUnknown = true } = options;
|
|
149
183
|
const result = stripUnknown ? schema.safeParse(input) : schema instanceof zod.z.ZodObject ? schema.strict().safeParse(input) : schema.safeParse(input);
|
|
@@ -491,6 +525,166 @@ function sanitizeInput(input) {
|
|
|
491
525
|
return sanitized;
|
|
492
526
|
}
|
|
493
527
|
|
|
528
|
+
// src/payment/index.ts
|
|
529
|
+
var paymentConfig = null;
|
|
530
|
+
function initPayment(config) {
|
|
531
|
+
paymentConfig = {
|
|
532
|
+
apiUrl: "https://api.openconductor.ai",
|
|
533
|
+
testMode: false,
|
|
534
|
+
...config
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function getPaymentConfig() {
|
|
538
|
+
return paymentConfig;
|
|
539
|
+
}
|
|
540
|
+
async function checkBilling(params) {
|
|
541
|
+
const config = paymentConfig;
|
|
542
|
+
if (!config) {
|
|
543
|
+
throw new ConfigurationError("payment", "Payment not initialized. Call initPayment() first.");
|
|
544
|
+
}
|
|
545
|
+
if (config.testMode) {
|
|
546
|
+
return { allowed: true, credits: 9999, tier: "test" };
|
|
547
|
+
}
|
|
548
|
+
try {
|
|
549
|
+
const response = await fetch(`${config.apiUrl}/functions/v1/billing-check`, {
|
|
550
|
+
method: "POST",
|
|
551
|
+
headers: {
|
|
552
|
+
"Content-Type": "application/json",
|
|
553
|
+
"Authorization": `Bearer ${config.apiKey}`
|
|
554
|
+
},
|
|
555
|
+
body: JSON.stringify({
|
|
556
|
+
userId: params.userId,
|
|
557
|
+
requirement: params.requirement,
|
|
558
|
+
tool: params.toolName
|
|
559
|
+
})
|
|
560
|
+
});
|
|
561
|
+
if (!response.ok) {
|
|
562
|
+
const errorBody = await response.text();
|
|
563
|
+
console.error("[payment] Billing check failed:", response.status, errorBody);
|
|
564
|
+
return {
|
|
565
|
+
allowed: false,
|
|
566
|
+
reason: "Billing service unavailable",
|
|
567
|
+
actionUrl: config.upgradeUrl
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
return await response.json();
|
|
571
|
+
} catch (error) {
|
|
572
|
+
console.error("[payment] Billing check error:", error);
|
|
573
|
+
return {
|
|
574
|
+
allowed: false,
|
|
575
|
+
reason: "Unable to verify billing status",
|
|
576
|
+
actionUrl: config.upgradeUrl
|
|
577
|
+
};
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
async function deductCredits(params) {
|
|
581
|
+
const config = paymentConfig;
|
|
582
|
+
if (!config) return false;
|
|
583
|
+
if (config.testMode) return true;
|
|
584
|
+
try {
|
|
585
|
+
const response = await fetch(`${config.apiUrl}/functions/v1/billing-deduct`, {
|
|
586
|
+
method: "POST",
|
|
587
|
+
headers: {
|
|
588
|
+
"Content-Type": "application/json",
|
|
589
|
+
"Authorization": `Bearer ${config.apiKey}`
|
|
590
|
+
},
|
|
591
|
+
body: JSON.stringify({
|
|
592
|
+
userId: params.userId,
|
|
593
|
+
credits: params.credits,
|
|
594
|
+
tool: params.toolName,
|
|
595
|
+
callId: params.callId
|
|
596
|
+
})
|
|
597
|
+
});
|
|
598
|
+
return response.ok;
|
|
599
|
+
} catch (error) {
|
|
600
|
+
console.error("[payment] Credit deduction error:", error);
|
|
601
|
+
return false;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
function isCreditRequirement(req) {
|
|
605
|
+
return "credits" in req && typeof req.credits === "number";
|
|
606
|
+
}
|
|
607
|
+
function isSubscriptionRequirement(req) {
|
|
608
|
+
return "tier" in req && typeof req.tier === "string";
|
|
609
|
+
}
|
|
610
|
+
function isStripeRequirement(req) {
|
|
611
|
+
return "priceId" in req && typeof req.priceId === "string";
|
|
612
|
+
}
|
|
613
|
+
function requirePayment(requirement, options = {}) {
|
|
614
|
+
return (handler) => {
|
|
615
|
+
const { toolName = "unknown", getUserId } = options;
|
|
616
|
+
return async (input) => {
|
|
617
|
+
const userId = getUserId?.(input) ?? input.userId;
|
|
618
|
+
if (!userId) {
|
|
619
|
+
throw new PaymentRequiredError(toolName, {
|
|
620
|
+
upgradeUrl: paymentConfig?.upgradeUrl
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
const status = await checkBilling({
|
|
624
|
+
userId,
|
|
625
|
+
requirement,
|
|
626
|
+
toolName
|
|
627
|
+
});
|
|
628
|
+
if (!status.allowed) {
|
|
629
|
+
if (isCreditRequirement(requirement)) {
|
|
630
|
+
throw new InsufficientCreditsError(
|
|
631
|
+
requirement.credits,
|
|
632
|
+
status.credits ?? 0,
|
|
633
|
+
{ purchaseUrl: status.actionUrl }
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
if (isSubscriptionRequirement(requirement)) {
|
|
637
|
+
throw new SubscriptionRequiredError(
|
|
638
|
+
requirement.tier,
|
|
639
|
+
status.tier,
|
|
640
|
+
{ upgradeUrl: status.actionUrl }
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
throw new PaymentRequiredError(toolName, {
|
|
644
|
+
upgradeUrl: status.actionUrl,
|
|
645
|
+
...isStripeRequirement(requirement) && { priceId: requirement.priceId }
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
const result = await handler(input);
|
|
649
|
+
if (isCreditRequirement(requirement)) {
|
|
650
|
+
await deductCredits({
|
|
651
|
+
userId,
|
|
652
|
+
credits: requirement.credits,
|
|
653
|
+
toolName
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
return result;
|
|
657
|
+
};
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
function createPaidTool(config) {
|
|
661
|
+
return requirePayment(config.payment, {
|
|
662
|
+
toolName: config.name,
|
|
663
|
+
getUserId: config.getUserId
|
|
664
|
+
})(config.handler);
|
|
665
|
+
}
|
|
666
|
+
async function canUserAccess(userId, requirement, toolName = "check") {
|
|
667
|
+
return checkBilling({ userId, requirement, toolName });
|
|
668
|
+
}
|
|
669
|
+
async function getUserBillingStatus(userId) {
|
|
670
|
+
const config = paymentConfig;
|
|
671
|
+
if (!config) return null;
|
|
672
|
+
if (config.testMode) {
|
|
673
|
+
return { credits: 9999, tier: "test", active: true };
|
|
674
|
+
}
|
|
675
|
+
try {
|
|
676
|
+
const response = await fetch(`${config.apiUrl}/functions/v1/billing-status/${userId}`, {
|
|
677
|
+
headers: {
|
|
678
|
+
"Authorization": `Bearer ${config.apiKey}`
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
if (!response.ok) return null;
|
|
682
|
+
return await response.json();
|
|
683
|
+
} catch {
|
|
684
|
+
return null;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
494
688
|
Object.defineProperty(exports, "ZodError", {
|
|
495
689
|
enumerable: true,
|
|
496
690
|
get: function () { return zod.ZodError; }
|
|
@@ -504,18 +698,27 @@ exports.AuthorizationError = AuthorizationError;
|
|
|
504
698
|
exports.ConfigurationError = ConfigurationError;
|
|
505
699
|
exports.DependencyError = DependencyError;
|
|
506
700
|
exports.ErrorCodes = ErrorCodes;
|
|
701
|
+
exports.InsufficientCreditsError = InsufficientCreditsError;
|
|
507
702
|
exports.MCPError = MCPError;
|
|
703
|
+
exports.PaymentRequiredError = PaymentRequiredError;
|
|
508
704
|
exports.RateLimitError = RateLimitError;
|
|
509
705
|
exports.ResourceNotFoundError = ResourceNotFoundError;
|
|
706
|
+
exports.SubscriptionRequiredError = SubscriptionRequiredError;
|
|
510
707
|
exports.Telemetry = Telemetry;
|
|
511
708
|
exports.TimeoutError = TimeoutError;
|
|
512
709
|
exports.ToolExecutionError = ToolExecutionError;
|
|
513
710
|
exports.ToolNotFoundError = ToolNotFoundError;
|
|
514
711
|
exports.ValidationError = ValidationError;
|
|
712
|
+
exports.canUserAccess = canUserAccess;
|
|
515
713
|
exports.createHealthCheck = createHealthCheck;
|
|
516
714
|
exports.createLogger = createLogger;
|
|
715
|
+
exports.createPaidTool = createPaidTool;
|
|
716
|
+
exports.getPaymentConfig = getPaymentConfig;
|
|
517
717
|
exports.getTelemetry = getTelemetry;
|
|
718
|
+
exports.getUserBillingStatus = getUserBillingStatus;
|
|
719
|
+
exports.initPayment = initPayment;
|
|
518
720
|
exports.initTelemetry = initTelemetry;
|
|
721
|
+
exports.requirePayment = requirePayment;
|
|
519
722
|
exports.schemas = schemas;
|
|
520
723
|
exports.validate = validate;
|
|
521
724
|
exports.validateInput = validateInput;
|