@fluxa-pay/fluxa-connect-mcp 0.1.4 → 0.1.5
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/dist/payment/payment-client.d.ts +14 -0
- package/dist/payment/payment-client.d.ts.map +1 -0
- package/dist/payment/payment-client.js +91 -0
- package/dist/payment/payment-client.js.map +1 -0
- package/dist/payment/payment-service.d.ts +41 -0
- package/dist/payment/payment-service.d.ts.map +1 -0
- package/dist/payment/payment-service.js +125 -0
- package/dist/payment/payment-service.js.map +1 -0
- package/dist/payment/types.d.ts +47 -0
- package/dist/payment/types.d.ts.map +1 -0
- package/dist/payment/types.js +5 -0
- package/dist/payment/types.js.map +1 -0
- package/dist/server/agent-registry.d.ts +18 -0
- package/dist/server/agent-registry.d.ts.map +1 -0
- package/dist/server/agent-registry.js +94 -0
- package/dist/server/agent-registry.js.map +1 -0
- package/dist/server/config.d.ts +27 -0
- package/dist/server/config.d.ts.map +1 -0
- package/dist/server/config.js +74 -0
- package/dist/server/config.js.map +1 -0
- package/dist/server/credential-cache.d.ts +46 -0
- package/dist/server/credential-cache.d.ts.map +1 -0
- package/dist/server/credential-cache.js +163 -0
- package/dist/server/credential-cache.js.map +1 -0
- package/dist/server/stdio-server.d.ts +7 -0
- package/dist/server/stdio-server.d.ts.map +1 -0
- package/dist/server/stdio-server.js +140 -0
- package/dist/server/stdio-server.js.map +1 -0
- package/dist/utils/api.d.ts +43 -0
- package/dist/utils/api.d.ts.map +1 -0
- package/dist/utils/api.js +164 -0
- package/dist/utils/api.js.map +1 -0
- package/dist/utils/error-messages.d.ts +21 -0
- package/dist/utils/error-messages.d.ts.map +1 -0
- package/dist/utils/error-messages.js +46 -0
- package/dist/utils/error-messages.js.map +1 -0
- package/dist/utils/jwt-utils.d.ts +25 -0
- package/dist/utils/jwt-utils.d.ts.map +1 -0
- package/dist/utils/jwt-utils.js +68 -0
- package/dist/utils/jwt-utils.js.map +1 -0
- package/dist/utils/logger.d.ts +23 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +75 -0
- package/dist/utils/logger.js.map +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Client wrapper with X402 payment support
|
|
3
|
+
* Intercepts tool calls and handles payment flows transparently
|
|
4
|
+
*/
|
|
5
|
+
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
6
|
+
import { PaymentServiceConfig, ConfirmationCallback } from './types.js';
|
|
7
|
+
export interface PaymentClientConfig extends PaymentServiceConfig {
|
|
8
|
+
confirmationCallback?: ConfirmationCallback;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Wrap MCP client with payment capabilities
|
|
12
|
+
*/
|
|
13
|
+
export declare function createPaymentClient(client: Client, config: PaymentClientConfig): Client;
|
|
14
|
+
//# sourceMappingURL=payment-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-client.d.ts","sourceRoot":"","sources":["../../src/payment/payment-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AAIxE,OAAO,EAEL,oBAAoB,EACpB,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,mBAAoB,SAAQ,oBAAoB;IAC/D,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,mBAAmB,GAC1B,MAAM,CAqER"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Client wrapper with X402 payment support
|
|
3
|
+
* Intercepts tool calls and handles payment flows transparently
|
|
4
|
+
*/
|
|
5
|
+
import { logger } from '../utils/logger.js';
|
|
6
|
+
import { formatApprovalRequiredError } from '../utils/error-messages.js';
|
|
7
|
+
import { PaymentService, PaymentError } from './payment-service.js';
|
|
8
|
+
/**
|
|
9
|
+
* Wrap MCP client with payment capabilities
|
|
10
|
+
*/
|
|
11
|
+
export function createPaymentClient(client, config) {
|
|
12
|
+
const paymentService = new PaymentService(config);
|
|
13
|
+
const { confirmationCallback } = config;
|
|
14
|
+
// Store original method
|
|
15
|
+
const originalCallTool = client.callTool.bind(client);
|
|
16
|
+
// Override with payment-aware version
|
|
17
|
+
client.callTool = async (params, resultSchema, options) => {
|
|
18
|
+
const toolName = params.name;
|
|
19
|
+
logger.debug('payment-client', 'Tool call initiated', { toolName });
|
|
20
|
+
// First attempt - call without payment
|
|
21
|
+
let result = await originalCallTool(params, resultSchema, options);
|
|
22
|
+
// Check if payment is required
|
|
23
|
+
const paymentError = result._meta?.['x402/error'];
|
|
24
|
+
if (!result.isError || !paymentError?.accepts) {
|
|
25
|
+
// No payment required or different error
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
logger.info('payment-client', 'Payment required for tool', {
|
|
29
|
+
toolName,
|
|
30
|
+
optionsCount: paymentError.accepts.length
|
|
31
|
+
});
|
|
32
|
+
try {
|
|
33
|
+
// Handle payment flow
|
|
34
|
+
const paymentToken = await handlePaymentFlow(paymentService, paymentError.accepts, confirmationCallback);
|
|
35
|
+
// Retry with payment token
|
|
36
|
+
logger.debug('payment-client', 'Retrying tool call with payment', { toolName });
|
|
37
|
+
result = await originalCallTool({
|
|
38
|
+
...params,
|
|
39
|
+
_meta: {
|
|
40
|
+
...(params._meta || {}),
|
|
41
|
+
'x402/payment': paymentToken
|
|
42
|
+
}
|
|
43
|
+
}, resultSchema, options);
|
|
44
|
+
logger.info('payment-client', 'Tool call completed with payment', { toolName });
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
logger.error('payment-client', 'Payment flow failed', {
|
|
49
|
+
toolName,
|
|
50
|
+
error: error.message
|
|
51
|
+
});
|
|
52
|
+
// Return error in MCP format
|
|
53
|
+
return {
|
|
54
|
+
isError: true,
|
|
55
|
+
content: [{
|
|
56
|
+
type: 'text',
|
|
57
|
+
text: error.message
|
|
58
|
+
}]
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
return client;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Handle the complete payment flow
|
|
66
|
+
*/
|
|
67
|
+
async function handlePaymentFlow(paymentService, paymentOptions, confirmationCallback) {
|
|
68
|
+
// Ask user for confirmation if callback provided
|
|
69
|
+
if (confirmationCallback) {
|
|
70
|
+
const approved = await confirmationCallback(paymentOptions);
|
|
71
|
+
if (!approved) {
|
|
72
|
+
throw new Error('Payment declined by user');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// Select appropriate payment option
|
|
76
|
+
const selectedOption = paymentService.selectPaymentOption(paymentOptions);
|
|
77
|
+
if (!selectedOption) {
|
|
78
|
+
throw new Error(`Payment network ${paymentService['config'].network} not supported by this resource.\n` +
|
|
79
|
+
`Available networks: ${paymentOptions.map(o => o.network).join(', ')}`);
|
|
80
|
+
}
|
|
81
|
+
// Create payment
|
|
82
|
+
const paymentResponse = await paymentService.createPayment(selectedOption);
|
|
83
|
+
// Check if approval needed
|
|
84
|
+
if (paymentResponse.status === 'need_approval') {
|
|
85
|
+
const message = formatApprovalRequiredError(paymentResponse.approvalUrl || '');
|
|
86
|
+
throw new PaymentError(message, 'need_approval', { approvalUrl: paymentResponse.approvalUrl });
|
|
87
|
+
}
|
|
88
|
+
// Encode and return payment token
|
|
89
|
+
return paymentService.encodePaymentToken(paymentResponse);
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=payment-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-client.js","sourceRoot":"","sources":["../../src/payment/payment-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EA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MAAM,IAAI,KAAK,CACb,mBAAmB,cAAc,CAAC,QAAQ,CAAC,CAAC,OAAO,oCAAoC;YACvF,uBAAuB,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CACvE,CAAC;IACJ,CAAC;IAED,iBAAiB;IACjB,MAAM,eAAe,GAAG,MAAM,cAAc,CAAC,aAAa,CAAC,cAAc,CAAC,CAAC;IAE3E,2BAA2B;IAC3B,IAAI,eAAe,CAAC,MAAM,KAAK,eAAe,EAAE,CAAC;QAC/C,MAAM,OAAO,GAAG,2BAA2B,CAAC,eAAe,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC;QAC/E,MAAM,IAAI,YAAY,CACpB,OAAO,EACP,eAAe,EACf,EAAE,WAAW,EAAE,eAAe,CAAC,WAAW,EAAE,CAC7C,CAAC;IACJ,CAAC;IAED,kCAAkC;IAClC,OAAO,cAAc,CAAC,kBAAkB,CAAC,eAAe,CAAC,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment service for creating and managing X402 payments via FluxA
|
|
3
|
+
*/
|
|
4
|
+
import { PaymentOption, PaymentResponse, PaymentServiceConfig } from './types.js';
|
|
5
|
+
export declare class PaymentError extends Error {
|
|
6
|
+
readonly code?: string | undefined;
|
|
7
|
+
readonly context?: any | undefined;
|
|
8
|
+
constructor(message: string, code?: string | undefined, context?: any | undefined);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Service for handling FluxA payment operations
|
|
12
|
+
*/
|
|
13
|
+
export declare class PaymentService {
|
|
14
|
+
private config;
|
|
15
|
+
constructor(config: PaymentServiceConfig);
|
|
16
|
+
/**
|
|
17
|
+
* Select payment option matching configured network
|
|
18
|
+
*/
|
|
19
|
+
selectPaymentOption(options: PaymentOption[]): PaymentOption | null;
|
|
20
|
+
/**
|
|
21
|
+
* Extract host from resource URL
|
|
22
|
+
*/
|
|
23
|
+
private extractHost;
|
|
24
|
+
/**
|
|
25
|
+
* Build payment request from payment option
|
|
26
|
+
*/
|
|
27
|
+
private buildPaymentRequest;
|
|
28
|
+
/**
|
|
29
|
+
* Create payment via FluxA Wallet Service
|
|
30
|
+
*/
|
|
31
|
+
createPayment(option: PaymentOption): Promise<PaymentResponse>;
|
|
32
|
+
/**
|
|
33
|
+
* Handle payment creation errors
|
|
34
|
+
*/
|
|
35
|
+
private handlePaymentError;
|
|
36
|
+
/**
|
|
37
|
+
* Encode payment response as base64 token
|
|
38
|
+
*/
|
|
39
|
+
encodePaymentToken(paymentResponse: PaymentResponse): string;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=payment-service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-service.d.ts","sourceRoot":"","sources":["../../src/payment/payment-service.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,OAAO,EACL,aAAa,EAEb,eAAe,EACf,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB,qBAAa,YAAa,SAAQ,KAAK;aAGnB,IAAI,CAAC,EAAE,MAAM;aACb,OAAO,CAAC,EAAE,GAAG;gBAF7B,OAAO,EAAE,MAAM,EACC,IAAI,CAAC,EAAE,MAAM,YAAA,EACb,OAAO,CAAC,EAAE,GAAG,YAAA;CAKhC;AAED;;GAEG;AACH,qBAAa,cAAc;IACb,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,oBAAoB;IAEhD;;OAEG;IACH,mBAAmB,CAAC,OAAO,EAAE,aAAa,EAAE,GAAG,aAAa,GAAG,IAAI;IAanE;;OAEG;IACH,OAAO,CAAC,WAAW;IAUnB;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAiB3B;;OAEG;IACG,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,eAAe,CAAC;IA4BpE;;OAEG;IACH,OAAO,CAAC,kBAAkB;IA0C1B;;OAEG;IACH,kBAAkB,CAAC,eAAe,EAAE,eAAe,GAAG,MAAM;CAI7D"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Payment service for creating and managing X402 payments via FluxA
|
|
3
|
+
*/
|
|
4
|
+
import { logger } from '../utils/logger.js';
|
|
5
|
+
import { createX402Payment, APIError } from '../utils/api.js';
|
|
6
|
+
import { formatAgentNotFoundError, extractAgentIdFromInstructions } from '../utils/error-messages.js';
|
|
7
|
+
export class PaymentError extends Error {
|
|
8
|
+
code;
|
|
9
|
+
context;
|
|
10
|
+
constructor(message, code, context) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.context = context;
|
|
14
|
+
this.name = 'PaymentError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Service for handling FluxA payment operations
|
|
19
|
+
*/
|
|
20
|
+
export class PaymentService {
|
|
21
|
+
config;
|
|
22
|
+
constructor(config) {
|
|
23
|
+
this.config = config;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Select payment option matching configured network
|
|
27
|
+
*/
|
|
28
|
+
selectPaymentOption(options) {
|
|
29
|
+
const selected = options.find(opt => opt.network === this.config.network);
|
|
30
|
+
if (!selected) {
|
|
31
|
+
logger.warn('payment-service', 'No payment option found for network', {
|
|
32
|
+
requestedNetwork: this.config.network,
|
|
33
|
+
availableNetworks: options.map(o => o.network)
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return selected || null;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Extract host from resource URL
|
|
40
|
+
*/
|
|
41
|
+
extractHost(resource) {
|
|
42
|
+
try {
|
|
43
|
+
const url = new URL(resource);
|
|
44
|
+
return url.hostname;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
// If not a valid URL, try to extract from MCP scheme
|
|
48
|
+
return resource.replace('mcp://', '').split('/')[0];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Build payment request from payment option
|
|
53
|
+
*/
|
|
54
|
+
buildPaymentRequest(option) {
|
|
55
|
+
return {
|
|
56
|
+
scheme: option.scheme,
|
|
57
|
+
network: option.network,
|
|
58
|
+
amount: option.maxAmountRequired,
|
|
59
|
+
currency: 'USDC',
|
|
60
|
+
assetAddress: option.asset,
|
|
61
|
+
payTo: option.payTo,
|
|
62
|
+
host: this.extractHost(option.resource),
|
|
63
|
+
resource: option.resource,
|
|
64
|
+
description: option.description,
|
|
65
|
+
tokenName: option.extra?.name || 'USDC',
|
|
66
|
+
tokenVersion: option.extra?.version || '2',
|
|
67
|
+
validityWindowSeconds: option.maxTimeoutSeconds || 60
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Create payment via FluxA Wallet Service
|
|
72
|
+
*/
|
|
73
|
+
async createPayment(option) {
|
|
74
|
+
const request = this.buildPaymentRequest(option);
|
|
75
|
+
logger.info('payment-service', 'Creating payment', {
|
|
76
|
+
network: request.network,
|
|
77
|
+
amount: request.amount,
|
|
78
|
+
resource: request.resource,
|
|
79
|
+
payTo: request.payTo
|
|
80
|
+
});
|
|
81
|
+
try {
|
|
82
|
+
const paymentResponse = await createX402Payment(this.config.walletServiceUrl, this.config.agentJwt, request);
|
|
83
|
+
logger.info('payment-service', 'Payment created', {
|
|
84
|
+
status: paymentResponse.status,
|
|
85
|
+
hasApprovalUrl: !!paymentResponse.approvalUrl
|
|
86
|
+
});
|
|
87
|
+
return paymentResponse;
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
return this.handlePaymentError(error);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Handle payment creation errors
|
|
95
|
+
*/
|
|
96
|
+
handlePaymentError(error) {
|
|
97
|
+
// If it's not an APIError, wrap it
|
|
98
|
+
if (!(error instanceof APIError)) {
|
|
99
|
+
throw new PaymentError(`Unexpected payment error: ${error.message}`, 'unknown_error');
|
|
100
|
+
}
|
|
101
|
+
const errorData = error.responseData;
|
|
102
|
+
// Agent not found - need authorization
|
|
103
|
+
if (errorData?.payment_model_context && errorData.code === 'agent_not_found') {
|
|
104
|
+
const instructions = errorData.payment_model_context.instructions || '';
|
|
105
|
+
const agentId = extractAgentIdFromInstructions(instructions);
|
|
106
|
+
const message = formatAgentNotFoundError(agentId || '', this.config.agentName, instructions);
|
|
107
|
+
throw new PaymentError(message, 'agent_not_found', { agentId, instructions });
|
|
108
|
+
}
|
|
109
|
+
// Other payment_model_context errors
|
|
110
|
+
if (errorData.payment_model_context) {
|
|
111
|
+
const instructions = errorData.payment_model_context.instructions || '';
|
|
112
|
+
throw new PaymentError(`Payment failed: ${errorData.message || error.message}\n\n${instructions}`, errorData.code, errorData.payment_model_context);
|
|
113
|
+
}
|
|
114
|
+
// Generic error
|
|
115
|
+
throw new PaymentError(`Payment creation failed: ${errorData.message || error.message}`, errorData.code);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Encode payment response as base64 token
|
|
119
|
+
*/
|
|
120
|
+
encodePaymentToken(paymentResponse) {
|
|
121
|
+
const payment = paymentResponse.xPayment || paymentResponse;
|
|
122
|
+
return Buffer.from(JSON.stringify(payment)).toString('base64');
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=payment-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-service.js","sourceRoot":"","sources":["../../src/payment/payment-service.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EACL,wBAAwB,EACxB,8BAA8B,EAC/B,MAAM,4BAA4B,CAAC;AAQpC,MAAM,OAAO,YAAa,SAAQ,KAAK;IAGnB;IACA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AAI,CACf,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,kBAAkB,CAAC,eAAgC;QACjD,MAAM,OAAO,GAAG,eAAe,CAAC,QAAQ,IAAI,eAAe,CAAC;QAC5D,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACjE,CAAC;CACF"}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type definitions for payment system
|
|
3
|
+
*/
|
|
4
|
+
export interface PaymentOption {
|
|
5
|
+
scheme: string;
|
|
6
|
+
network: string;
|
|
7
|
+
maxAmountRequired: string;
|
|
8
|
+
payTo: string;
|
|
9
|
+
asset: string;
|
|
10
|
+
maxTimeoutSeconds: number;
|
|
11
|
+
resource: string;
|
|
12
|
+
mimeType: string;
|
|
13
|
+
description: string;
|
|
14
|
+
extra?: any;
|
|
15
|
+
}
|
|
16
|
+
export interface X402Error {
|
|
17
|
+
accepts: PaymentOption[];
|
|
18
|
+
error?: string;
|
|
19
|
+
x402Version?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface PaymentResponse {
|
|
22
|
+
status: string;
|
|
23
|
+
xPayment?: any;
|
|
24
|
+
approvalUrl?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface PaymentRequest {
|
|
27
|
+
scheme: string;
|
|
28
|
+
network: string;
|
|
29
|
+
amount: string;
|
|
30
|
+
currency: string;
|
|
31
|
+
assetAddress: string;
|
|
32
|
+
payTo: string;
|
|
33
|
+
host: string;
|
|
34
|
+
resource: string;
|
|
35
|
+
description: string;
|
|
36
|
+
tokenName: string;
|
|
37
|
+
tokenVersion: string;
|
|
38
|
+
validityWindowSeconds: number;
|
|
39
|
+
}
|
|
40
|
+
export interface PaymentServiceConfig {
|
|
41
|
+
walletServiceUrl: string;
|
|
42
|
+
agentJwt: string;
|
|
43
|
+
agentName: string;
|
|
44
|
+
network: string;
|
|
45
|
+
}
|
|
46
|
+
export type ConfirmationCallback = (options: PaymentOption[]) => Promise<boolean>;
|
|
47
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/payment/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,iBAAiB,EAAE,MAAM,CAAC;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,GAAG,CAAC;CACb;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,MAAM,CAAC;IACrB,qBAAqB,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,oBAAoB;IACnC,gBAAgB,EAAE,MAAM,CAAC;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,EAAE,aAAa,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/payment/types.ts"],"names":[],"mappings":"AAAA;;GAEG"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent registration with FluxA Agent Registry
|
|
3
|
+
* Handles agent registration, JWT token management, and credential caching
|
|
4
|
+
*/
|
|
5
|
+
export interface AgentCredentials {
|
|
6
|
+
agentId: string;
|
|
7
|
+
jwt: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class AgentRegistrationError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Register an agent with FluxA and obtain JWT token
|
|
14
|
+
* Uses cached credentials if available and valid
|
|
15
|
+
* Automatically refreshes expired JWTs
|
|
16
|
+
*/
|
|
17
|
+
export declare function registerAgent(registryUrl: string, email: string, agentName: string, clientInfo: string): Promise<AgentCredentials>;
|
|
18
|
+
//# sourceMappingURL=agent-registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-registry.d.ts","sourceRoot":"","sources":["../../src/server/agent-registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAWH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,qBAAa,sBAAuB,SAAQ,KAAK;gBACnC,OAAO,EAAE,MAAM;CAI5B;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CACjC,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,gBAAgB,CAAC,CAuG3B"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent registration with FluxA Agent Registry
|
|
3
|
+
* Handles agent registration, JWT token management, and credential caching
|
|
4
|
+
*/
|
|
5
|
+
import { logger } from '../utils/logger.js';
|
|
6
|
+
import { isJWTExpired } from '../utils/jwt-utils.js';
|
|
7
|
+
import { registerAgent as apiRegisterAgent, refreshJWT as apiRefreshJWT, APIError } from '../utils/api.js';
|
|
8
|
+
import { getCredentials, storeCredentials, createCachedCredentials } from './credential-cache.js';
|
|
9
|
+
export class AgentRegistrationError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'AgentRegistrationError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Register an agent with FluxA and obtain JWT token
|
|
17
|
+
* Uses cached credentials if available and valid
|
|
18
|
+
* Automatically refreshes expired JWTs
|
|
19
|
+
*/
|
|
20
|
+
export async function registerAgent(registryUrl, email, agentName, clientInfo) {
|
|
21
|
+
// Step 1: Check cache for existing credentials
|
|
22
|
+
const cached = getCredentials(email, agentName);
|
|
23
|
+
if (cached) {
|
|
24
|
+
logger.info('agent-registry', 'Found cached credentials', {
|
|
25
|
+
email,
|
|
26
|
+
agentName,
|
|
27
|
+
agentId: cached.agent_id
|
|
28
|
+
});
|
|
29
|
+
// Step 2: Check if JWT is still valid
|
|
30
|
+
if (!isJWTExpired(cached.jwt)) {
|
|
31
|
+
logger.info('agent-registry', 'Using valid cached JWT');
|
|
32
|
+
return {
|
|
33
|
+
agentId: cached.agent_id,
|
|
34
|
+
jwt: cached.jwt
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
// Step 3: JWT expired, try to refresh it
|
|
38
|
+
logger.info('agent-registry', 'JWT expired, attempting refresh');
|
|
39
|
+
try {
|
|
40
|
+
const newJwt = await apiRefreshJWT(registryUrl, cached.agent_id, cached.token);
|
|
41
|
+
// Step 4: Update cache with refreshed JWT
|
|
42
|
+
const updated = createCachedCredentials(cached.agent_id, cached.token, newJwt, email, agentName, clientInfo);
|
|
43
|
+
storeCredentials(updated);
|
|
44
|
+
logger.info('agent-registry', 'JWT refreshed successfully');
|
|
45
|
+
return {
|
|
46
|
+
agentId: cached.agent_id,
|
|
47
|
+
jwt: newJwt
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
logger.warn('agent-registry', 'JWT refresh failed, will re-register', {
|
|
52
|
+
error: error.message
|
|
53
|
+
});
|
|
54
|
+
// Fall through to registration
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Step 5: No cache OR refresh failed - register new agent
|
|
58
|
+
logger.info('agent-registry', 'Registering new agent with FluxA', {
|
|
59
|
+
email,
|
|
60
|
+
agentName
|
|
61
|
+
});
|
|
62
|
+
try {
|
|
63
|
+
const data = await apiRegisterAgent(registryUrl, email, agentName, clientInfo);
|
|
64
|
+
// Step 6: Cache the new credentials
|
|
65
|
+
const credentials = createCachedCredentials(data.agent_id, data.token, data.jwt, email, agentName, clientInfo);
|
|
66
|
+
storeCredentials(credentials);
|
|
67
|
+
logger.info('agent-registry', 'Agent registered and cached successfully', {
|
|
68
|
+
agentId: data.agent_id
|
|
69
|
+
});
|
|
70
|
+
return {
|
|
71
|
+
agentId: data.agent_id,
|
|
72
|
+
jwt: data.jwt
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (error instanceof AgentRegistrationError) {
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
if (error instanceof APIError) {
|
|
80
|
+
throw new AgentRegistrationError(`Failed to register agent: ${error.message}\n\n` +
|
|
81
|
+
'Please check:\n' +
|
|
82
|
+
' - Your email and agent name are correct\n' +
|
|
83
|
+
' - You have internet connectivity\n' +
|
|
84
|
+
' - FluxA registration service is available');
|
|
85
|
+
}
|
|
86
|
+
// Unexpected errors
|
|
87
|
+
logger.error('agent-registry', 'Unexpected registration error', {
|
|
88
|
+
error: error.message,
|
|
89
|
+
registryUrl
|
|
90
|
+
});
|
|
91
|
+
throw new AgentRegistrationError(`Unexpected error during registration: ${error.message}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=agent-registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-registry.js","sourceRoot":"","sources":["../../src/server/agent-registry.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,aAAa,IAAI,gBAAgB,EAAE,UAAU,IAAI,aAAa,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAC3G,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,uBAAuB,EACxB,MAAM,uBAAuB,CAAC;AAO/B,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC/C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,WAAmB,EACnB,KAAa,EACb,SAAiB,EACjB,UAAkB;IAElB,+CAA+C;IAC/C,MAAM,MAAM,GAAG,cAAc,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAEhD,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,CAAC,IAAI,CAAC,gBAAgB,EAAE,0BAA0B,EAAE;YACxD,KAAK;YACL,SAAS;YACT,OAAO,EAAE,MAAM,CAAC,QAAQ;SACzB,CAAC,CAAC;QAEH,sCAAsC;QACtC,IAAI,CAAC,YAAY,CAAC,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CACzC,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,GAAG,EACR,KAAK,EACL,SAAS,EACT,UAAU,CACX,CAAC;QACF,gBAAgB,CAAC,WAAW,CAAC,CAAC;QAE9B,MAAM,CAAC,IAAI,CAAC,gBAAgB,EAAE,0CAA0C,EAAE;YACxE,OAAO,EAAE,IAAI,CAAC,QAAQ;SACvB,CAAC,CAAC;QAEH,OAAO;YACL,OAAO,EAAE,IAAI,CAAC,QAAQ;YACtB,GAAG,EAAE,IAAI,CAAC,GAAG;SACd,CAAC;IACJ,CAAC;IAAC,OAAO,KAAU,EAAE,CAAC;QACpB,IAAI,KAAK,YAAY,sBAAsB,EAAE,CAAC;YAC5C,MAAM,KAAK,CAAC;QACd,CAAC;QAED,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;YAC9B,MAAM,IAAI,sBAAsB,CAC9B,6BAA6B,KAAK,CAAC,OAAO,MAAM;gBAChD,iBAAiB;gBACjB,6CAA6C;gBAC7C,sCAAsC;gBACtC,6CAA6C,CAC9C,CAAC;QACJ,CAAC;QAED,oBAAoB;QACpB,MAAM,CAAC,KAAK,CAAC,gBAAgB,EAAE,+BAA+B,EAAE;YAC9D,KAAK,EAAE,KAAK,CAAC,OAAO;YACpB,WAAW;SACZ,CAAC,CAAC;QAEH,MAAM,IAAI,sBAAsB,CAC9B,yCAAyC,KAAK,CAAC,OAAO,EAAE,CACzD,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration management for FluxA Connect MCP
|
|
3
|
+
* Handles environment variables, CLI arguments, and validation
|
|
4
|
+
*/
|
|
5
|
+
export interface Config {
|
|
6
|
+
upstream: {
|
|
7
|
+
url: string;
|
|
8
|
+
};
|
|
9
|
+
agent: {
|
|
10
|
+
email: string;
|
|
11
|
+
name: string;
|
|
12
|
+
clientInfo: string;
|
|
13
|
+
};
|
|
14
|
+
fluxa: {
|
|
15
|
+
walletServiceUrl: string;
|
|
16
|
+
agentRegistryUrl: string;
|
|
17
|
+
network: string;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
export declare class ConfigError extends Error {
|
|
21
|
+
constructor(message: string);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Validate and load configuration from environment and CLI args
|
|
25
|
+
*/
|
|
26
|
+
export declare function loadConfig(args?: string[]): Config;
|
|
27
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/server/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,MAAM;IACrB,QAAQ,EAAE;QACR,GAAG,EAAE,MAAM,CAAC;KACb,CAAC;IACF,KAAK,EAAE;QACL,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,UAAU,EAAE,MAAM,CAAC;KACpB,CAAC;IACF,KAAK,EAAE;QACL,gBAAgB,EAAE,MAAM,CAAC;QACzB,gBAAgB,EAAE,MAAM,CAAC;QACzB,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH;AAED,qBAAa,WAAY,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM;CAI5B;AAiBD;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,GAAE,MAAM,EAA0B,GAAG,MAAM,CA4DzE"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration management for FluxA Connect MCP
|
|
3
|
+
* Handles environment variables, CLI arguments, and validation
|
|
4
|
+
*/
|
|
5
|
+
export class ConfigError extends Error {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'ConfigError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Parse command line arguments
|
|
13
|
+
*/
|
|
14
|
+
function parseArgs(args) {
|
|
15
|
+
const urlIndex = args.indexOf('--url');
|
|
16
|
+
if (urlIndex === -1 || !args[urlIndex + 1]) {
|
|
17
|
+
throw new ConfigError('Missing required argument: --url <upstream-mcp-server-url>');
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
url: args[urlIndex + 1]
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Validate and load configuration from environment and CLI args
|
|
25
|
+
*/
|
|
26
|
+
export function loadConfig(args = process.argv.slice(2)) {
|
|
27
|
+
// Parse CLI arguments
|
|
28
|
+
const cliArgs = parseArgs(args);
|
|
29
|
+
// Required environment variables
|
|
30
|
+
const agentEmail = process.env.AGENT_EMAIL;
|
|
31
|
+
const agentName = process.env.AGENT_NAME;
|
|
32
|
+
if (!agentEmail) {
|
|
33
|
+
throw new ConfigError('Missing required environment variable: AGENT_EMAIL\n' +
|
|
34
|
+
'Example: AGENT_EMAIL=your@email.com');
|
|
35
|
+
}
|
|
36
|
+
if (!agentName) {
|
|
37
|
+
throw new ConfigError('Missing required environment variable: AGENT_NAME\n' +
|
|
38
|
+
'Example: AGENT_NAME="Claude Code - My Agent"');
|
|
39
|
+
}
|
|
40
|
+
// Optional with defaults
|
|
41
|
+
const clientInfo = process.env.CLIENT_INFO || 'FluxA Connect MCP Client';
|
|
42
|
+
const evmNetwork = process.env.EVM_NETWORK || 'base';
|
|
43
|
+
const walletServiceUrl = process.env.FLUXA_WALLET_SERVICE_URL || 'https://walletapi.fluxapay.xyz';
|
|
44
|
+
const agentRegistryUrl = process.env.FLUXA_AGENT_REGISTRY_URL || 'https://agentid.fluxapay.xyz';
|
|
45
|
+
// Validate network
|
|
46
|
+
const supportedNetworks = [
|
|
47
|
+
'base', 'base-sepolia',
|
|
48
|
+
'avalanche', 'avalanche-fuji',
|
|
49
|
+
'iotex',
|
|
50
|
+
'sei', 'sei-testnet',
|
|
51
|
+
'polygon-amoy',
|
|
52
|
+
'solana', 'solana-devnet'
|
|
53
|
+
];
|
|
54
|
+
if (!supportedNetworks.includes(evmNetwork)) {
|
|
55
|
+
throw new ConfigError(`Unsupported network: ${evmNetwork}\n` +
|
|
56
|
+
`Supported networks: ${supportedNetworks.join(', ')}`);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
upstream: {
|
|
60
|
+
url: cliArgs.url
|
|
61
|
+
},
|
|
62
|
+
agent: {
|
|
63
|
+
email: agentEmail,
|
|
64
|
+
name: agentName,
|
|
65
|
+
clientInfo
|
|
66
|
+
},
|
|
67
|
+
fluxa: {
|
|
68
|
+
walletServiceUrl,
|
|
69
|
+
agentRegistryUrl,
|
|
70
|
+
network: evmNetwork
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/server/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkBH,MAAM,OAAO,WAAY,SAAQ,KAAK;IACpC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC5B,CAAC;CACF;AAED;;GAEG;AACH,SAAS,SAAS,CAAC,IAAc;IAC/B,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEvC,IAAI,QAAQ,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,WAAW,CAAC,4DAA4D,CAAC,CAAC;IACtF,CAAC;IAED,OAAO;QACL,GAAG,EAAE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;KACxB,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,UAAU,CAAC,OAAiB,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/D,sBAAsB;IAC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IAAI,EAAE,SAAS;YACf,UAAU;SACX;QACD,KAAK,EAAE;YACL,gBAAgB;YAChB,gBAAgB;YAChB,OAAO,EAAE,UAAU;SACpB;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential cache management
|
|
3
|
+
* Stores agent credentials in ~/.fluxa-ai-wallet-mcp/.agent-config.json
|
|
4
|
+
*/
|
|
5
|
+
export interface CachedCredentials {
|
|
6
|
+
agent_id: string;
|
|
7
|
+
token: string;
|
|
8
|
+
jwt: string;
|
|
9
|
+
email: string;
|
|
10
|
+
agent_name: string;
|
|
11
|
+
client_info: string;
|
|
12
|
+
registered_at: string;
|
|
13
|
+
jwt_expires_at: string;
|
|
14
|
+
}
|
|
15
|
+
interface CredentialCache {
|
|
16
|
+
version: string;
|
|
17
|
+
agents: {
|
|
18
|
+
[email: string]: {
|
|
19
|
+
[agent_name: string]: CachedCredentials;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Load cache from disk
|
|
25
|
+
* Returns empty cache structure if file doesn't exist or is invalid
|
|
26
|
+
*/
|
|
27
|
+
export declare function loadCache(): CredentialCache;
|
|
28
|
+
/**
|
|
29
|
+
* Save cache to disk with atomic write
|
|
30
|
+
*/
|
|
31
|
+
export declare function saveCache(cache: CredentialCache): void;
|
|
32
|
+
/**
|
|
33
|
+
* Lookup credentials by email and agent name
|
|
34
|
+
*/
|
|
35
|
+
export declare function getCredentials(email: string, agentName: string): CachedCredentials | null;
|
|
36
|
+
/**
|
|
37
|
+
* Store credentials in cache
|
|
38
|
+
* Creates email/agent_name structure if it doesn't exist
|
|
39
|
+
*/
|
|
40
|
+
export declare function storeCredentials(creds: CachedCredentials): void;
|
|
41
|
+
/**
|
|
42
|
+
* Helper to create CachedCredentials from registration response
|
|
43
|
+
*/
|
|
44
|
+
export declare function createCachedCredentials(agentId: string, token: string, jwt: string, email: string, agentName: string, clientInfo: string): CachedCredentials;
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=credential-cache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-cache.d.ts","sourceRoot":"","sources":["../../src/server/credential-cache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,UAAU,eAAe;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE;QACN,CAAC,KAAK,EAAE,MAAM,GAAG;YACf,CAAC,UAAU,EAAE,MAAM,GAAG,iBAAiB,CAAC;SACzC,CAAC;KACH,CAAC;CACH;AAkBD;;;GAGG;AACH,wBAAgB,SAAS,IAAI,eAAe,CA0C3C;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,eAAe,GAAG,IAAI,CAoCtD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,iBAAiB,GAAG,IAAI,CAyBzF;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,iBAAiB,GAAG,IAAI,CAmB/D;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,MAAM,EACb,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,GACjB,iBAAiB,CAenB"}
|