@thorium-dev-group/x402-mcp-extension
Version:
X402-MCP Protocol Extension
110 lines • 4.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HandlerWrapperFactory = void 0;
const PaymentError_1 = require("../../../shared/errors/PaymentError");
const payment_utils_1 = require("./payment-utils");
class HandlerWrapperFactory {
static createToolWrapper(handlerInfo, context) {
const hasInputSchema = handlerInfo.options && 'inputSchema' in handlerInfo.options &&
handlerInfo.options.inputSchema && Object.keys(handlerInfo.options.inputSchema).length > 0;
if (hasInputSchema) {
return async (args, extra) => {
return await this.createWrappedHandler(handlerInfo, context, args, extra);
};
}
else {
return async (extra) => {
return await this.createWrappedHandler(handlerInfo, context, {}, extra);
};
}
}
static createPromptWrapper(handlerInfo, context) {
return async (args, extra) => {
return await this.createWrappedHandler(handlerInfo, context, args, extra);
};
}
static createResourceWrapper(handlerInfo, context) {
return async (uri, extra) => {
return await this.createWrappedHandler(handlerInfo, context, { uri }, extra);
};
}
static createResourceTemplateWrapper(handlerInfo, context) {
return async (uri, variables, extra) => {
return await this.createWrappedHandler(handlerInfo, context, { uri, variables }, extra);
};
}
static async createWrappedHandler(handlerInfo, context, args, extra) {
try {
if (handlerInfo.paymentOptions) {
context.logger.info('Validating payment for handler', {
operation: 'payment_validation_start',
handlerName: handlerInfo.name,
amount: handlerInfo.paymentOptions.amount
});
const resourcePath = `/tools/${handlerInfo.name}`;
const paymentRequirements = (0, payment_utils_1.assemblePaymentRequirements)(handlerInfo.paymentOptions, resourcePath, context.x402Config, context.x402Config.baseUrl);
await context.paymentOrchestrator.validatePayment(handlerInfo.name, paymentRequirements, extra);
context.logger.info('Payment validation successful', {
operation: 'payment_validation_success',
handlerName: handlerInfo.name
});
}
context.logger.info('Executing developer handler', {
operation: 'handler_execution_start',
handlerName: handlerInfo.name
});
let result;
if (handlerInfo.type === 'resource') {
result = await handlerInfo.handler(args.uri, extra);
}
else if (handlerInfo.type === 'resourceTemplate') {
result = await handlerInfo.handler(args.uri, args.variables, extra);
}
else {
result = await handlerInfo.handler(args, extra);
}
context.logger.info('Developer handler executed successfully', {
operation: 'handler_execution_success',
handlerName: handlerInfo.name
});
if (handlerInfo.paymentOptions) {
context.logger.info('Settling payment after successful handler execution', {
operation: 'payment_settlement_start',
handlerName: handlerInfo.name
});
await context.paymentOrchestrator.settlePayment(extra);
context.logger.info('Payment settled successfully', {
operation: 'payment_settlement_success',
handlerName: handlerInfo.name
});
}
return result;
}
catch (error) {
if (error instanceof PaymentError_1.PaymentError) {
context.logger.error('Payment processing failed', {
operation: 'payment_failed',
handlerName: handlerInfo.name,
error: error.message
});
throw error;
}
else {
context.logger.error('Handler execution failed', {
operation: 'handler_execution_failed',
handlerName: handlerInfo.name,
error: error instanceof Error ? error.message : String(error)
});
throw new Error(`Handler execution failed: ${error instanceof Error ? error.message : String(error)}`);
}
}
finally {
if (extra) {
delete extra.paymentProof;
delete extra.paymentRequirements;
}
}
}
}
exports.HandlerWrapperFactory = HandlerWrapperFactory;
//# sourceMappingURL=handler-wrapper.js.map