@fluxa-pay/fluxa-connect-mcp 0.1.4 → 0.1.6
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 +150 -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,163 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential cache management
|
|
3
|
+
* Stores agent credentials in ~/.fluxa-ai-wallet-mcp/.agent-config.json
|
|
4
|
+
*/
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { homedir } from 'node:os';
|
|
7
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, chmodSync, unlinkSync, renameSync } from 'node:fs';
|
|
8
|
+
import { logger } from '../utils/logger.js';
|
|
9
|
+
import { parseJWT } from '../utils/jwt-utils.js';
|
|
10
|
+
const CACHE_DIR = join(homedir(), '.fluxa-ai-wallet-mcp');
|
|
11
|
+
const CACHE_FILE = join(CACHE_DIR, '.agent-config.json');
|
|
12
|
+
const CACHE_VERSION = '1.0';
|
|
13
|
+
/**
|
|
14
|
+
* Ensure cache directory exists with proper permissions
|
|
15
|
+
*/
|
|
16
|
+
function ensureCacheDir() {
|
|
17
|
+
if (!existsSync(CACHE_DIR)) {
|
|
18
|
+
logger.info('credential-cache', 'Creating cache directory', {
|
|
19
|
+
path: CACHE_DIR
|
|
20
|
+
});
|
|
21
|
+
mkdirSync(CACHE_DIR, { recursive: true, mode: 0o700 });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Load cache from disk
|
|
26
|
+
* Returns empty cache structure if file doesn't exist or is invalid
|
|
27
|
+
*/
|
|
28
|
+
export function loadCache() {
|
|
29
|
+
ensureCacheDir();
|
|
30
|
+
if (!existsSync(CACHE_FILE)) {
|
|
31
|
+
logger.debug('credential-cache', 'Cache file does not exist, creating new cache');
|
|
32
|
+
return {
|
|
33
|
+
version: CACHE_VERSION,
|
|
34
|
+
agents: {}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const data = readFileSync(CACHE_FILE, 'utf-8');
|
|
39
|
+
const cache = JSON.parse(data);
|
|
40
|
+
// Validate cache structure
|
|
41
|
+
if (!cache.version || !cache.agents) {
|
|
42
|
+
logger.warn('credential-cache', 'Invalid cache structure, creating new cache');
|
|
43
|
+
return {
|
|
44
|
+
version: CACHE_VERSION,
|
|
45
|
+
agents: {}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
logger.debug('credential-cache', 'Cache loaded', {
|
|
49
|
+
emailCount: Object.keys(cache.agents).length,
|
|
50
|
+
totalAgents: Object.values(cache.agents).reduce((sum, agents) => sum + Object.keys(agents).length, 0)
|
|
51
|
+
});
|
|
52
|
+
return cache;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
logger.warn('credential-cache', 'Failed to load cache, creating new cache', {
|
|
56
|
+
error: error.message
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
version: CACHE_VERSION,
|
|
60
|
+
agents: {}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Save cache to disk with atomic write
|
|
66
|
+
*/
|
|
67
|
+
export function saveCache(cache) {
|
|
68
|
+
ensureCacheDir();
|
|
69
|
+
try {
|
|
70
|
+
const data = JSON.stringify(cache, null, 2);
|
|
71
|
+
// Atomic write: write to temp file, then rename
|
|
72
|
+
const tempFile = `${CACHE_FILE}.tmp`;
|
|
73
|
+
writeFileSync(tempFile, data, { mode: 0o600 });
|
|
74
|
+
// Rename is atomic on most systems
|
|
75
|
+
if (existsSync(CACHE_FILE)) {
|
|
76
|
+
// On some systems, need to remove destination first
|
|
77
|
+
// This is a small race condition window, but acceptable for our use case
|
|
78
|
+
const backupFile = `${CACHE_FILE}.bak`;
|
|
79
|
+
if (existsSync(backupFile)) {
|
|
80
|
+
// Remove old backup
|
|
81
|
+
unlinkSync(backupFile);
|
|
82
|
+
}
|
|
83
|
+
renameSync(CACHE_FILE, backupFile);
|
|
84
|
+
}
|
|
85
|
+
renameSync(tempFile, CACHE_FILE);
|
|
86
|
+
// Ensure permissions
|
|
87
|
+
chmodSync(CACHE_FILE, 0o600);
|
|
88
|
+
logger.debug('credential-cache', 'Cache saved', {
|
|
89
|
+
path: CACHE_FILE
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
logger.error('credential-cache', 'Failed to save cache', {
|
|
94
|
+
error: error.message,
|
|
95
|
+
path: CACHE_FILE
|
|
96
|
+
});
|
|
97
|
+
throw new Error(`Failed to save credential cache: ${error.message}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Lookup credentials by email and agent name
|
|
102
|
+
*/
|
|
103
|
+
export function getCredentials(email, agentName) {
|
|
104
|
+
const cache = loadCache();
|
|
105
|
+
const emailAgents = cache.agents[email];
|
|
106
|
+
if (!emailAgents) {
|
|
107
|
+
logger.debug('credential-cache', 'No credentials for email', { email });
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const credentials = emailAgents[agentName];
|
|
111
|
+
if (!credentials) {
|
|
112
|
+
logger.debug('credential-cache', 'No credentials for agent name', {
|
|
113
|
+
email,
|
|
114
|
+
agentName
|
|
115
|
+
});
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
logger.debug('credential-cache', 'Credentials found in cache', {
|
|
119
|
+
email,
|
|
120
|
+
agentName,
|
|
121
|
+
agentId: credentials.agent_id
|
|
122
|
+
});
|
|
123
|
+
return credentials;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Store credentials in cache
|
|
127
|
+
* Creates email/agent_name structure if it doesn't exist
|
|
128
|
+
*/
|
|
129
|
+
export function storeCredentials(creds) {
|
|
130
|
+
const cache = loadCache();
|
|
131
|
+
// Ensure email entry exists
|
|
132
|
+
if (!cache.agents[creds.email]) {
|
|
133
|
+
cache.agents[creds.email] = {};
|
|
134
|
+
}
|
|
135
|
+
// Store credentials under email -> agent_name
|
|
136
|
+
cache.agents[creds.email][creds.agent_name] = creds;
|
|
137
|
+
saveCache(cache);
|
|
138
|
+
logger.info('credential-cache', 'Credentials stored in cache', {
|
|
139
|
+
email: creds.email,
|
|
140
|
+
agentName: creds.agent_name,
|
|
141
|
+
agentId: creds.agent_id,
|
|
142
|
+
expiresAt: creds.jwt_expires_at
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Helper to create CachedCredentials from registration response
|
|
147
|
+
*/
|
|
148
|
+
export function createCachedCredentials(agentId, token, jwt, email, agentName, clientInfo) {
|
|
149
|
+
// Parse JWT to get expiry
|
|
150
|
+
const payload = parseJWT(jwt);
|
|
151
|
+
const expiresAt = new Date(payload.exp * 1000).toISOString();
|
|
152
|
+
return {
|
|
153
|
+
agent_id: agentId,
|
|
154
|
+
token,
|
|
155
|
+
jwt,
|
|
156
|
+
email,
|
|
157
|
+
agent_name: agentName,
|
|
158
|
+
client_info: clientInfo,
|
|
159
|
+
registered_at: new Date().toISOString(),
|
|
160
|
+
jwt_expires_at: expiresAt
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
//# sourceMappingURL=credential-cache.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credential-cache.js","sourceRoot":"","sources":["../../src/server/credential-cache.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAChH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAsBjD,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,sBAAsB,C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
+
/**
|
|
3
|
+
* MCP Stdio Server - Main entry point
|
|
4
|
+
* Minimal orchestrator that coordinates all components
|
|
5
|
+
*/
|
|
6
|
+
import { webcrypto } from 'node:crypto';
|
|
7
|
+
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
8
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
9
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
10
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
11
|
+
import { config as loadEnv } from 'dotenv';
|
|
12
|
+
import { CallToolRequestSchema, ListToolsRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
|
|
13
|
+
import { loadConfig, ConfigError } from './config.js';
|
|
14
|
+
import { registerAgent, AgentRegistrationError } from './agent-registry.js';
|
|
15
|
+
import { createPaymentClient } from '../payment/payment-client.js';
|
|
16
|
+
import { logger } from '../utils/logger.js';
|
|
17
|
+
// Polyfill crypto for Node.js environment
|
|
18
|
+
if (!globalThis.crypto) {
|
|
19
|
+
globalThis.crypto = webcrypto;
|
|
20
|
+
}
|
|
21
|
+
loadEnv();
|
|
22
|
+
async function main() {
|
|
23
|
+
try {
|
|
24
|
+
// Load and validate configuration
|
|
25
|
+
const config = loadConfig();
|
|
26
|
+
logger.info('stdio-server', 'Configuration loaded', {
|
|
27
|
+
upstreamUrl: config.upstream.url,
|
|
28
|
+
network: config.fluxa.network,
|
|
29
|
+
agentName: config.agent.name
|
|
30
|
+
});
|
|
31
|
+
// Register agent with FluxA
|
|
32
|
+
const credentials = await registerAgent(config.fluxa.agentRegistryUrl, config.agent.email, config.agent.name, config.agent.clientInfo);
|
|
33
|
+
// Connect to upstream MCP server
|
|
34
|
+
logger.info('stdio-server', 'Connecting to upstream MCP server', {
|
|
35
|
+
url: config.upstream.url
|
|
36
|
+
});
|
|
37
|
+
const upstreamUrl = new URL(config.upstream.url);
|
|
38
|
+
// Build custom headers for the transport (email for free credits)
|
|
39
|
+
const transport = new StreamableHTTPClientTransport(upstreamUrl, {
|
|
40
|
+
requestInit: {
|
|
41
|
+
headers: {
|
|
42
|
+
'X-Consumer-Email': config.agent.email
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
logger.debug('stdio-server', 'Adding X-Consumer-Email header for free credits', {
|
|
47
|
+
email: config.agent.email
|
|
48
|
+
});
|
|
49
|
+
const client = new Client({ name: 'fluxa-connect-mcp-client', version: '1.0.0' }, { capabilities: {} });
|
|
50
|
+
await client.connect(transport);
|
|
51
|
+
logger.info('stdio-server', 'Connected to upstream server');
|
|
52
|
+
// Wrap client with payment capabilities
|
|
53
|
+
const paymentClient = createPaymentClient(client, {
|
|
54
|
+
walletServiceUrl: config.fluxa.walletServiceUrl,
|
|
55
|
+
agentJwt: credentials.jwt,
|
|
56
|
+
agentName: config.agent.name,
|
|
57
|
+
network: config.fluxa.network,
|
|
58
|
+
confirmationCallback: async (paymentOptions) => {
|
|
59
|
+
logger.info('stdio-server', 'Payment requested', {
|
|
60
|
+
networks: paymentOptions.map(p => ({
|
|
61
|
+
network: p.network,
|
|
62
|
+
amount: p.maxAmountRequired,
|
|
63
|
+
description: p.description
|
|
64
|
+
}))
|
|
65
|
+
});
|
|
66
|
+
return true; // Auto-approve (user approves via FluxA UI)
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
// List available tools from upstream
|
|
70
|
+
logger.debug('stdio-server', 'Listing tools from upstream server');
|
|
71
|
+
let upstreamTools;
|
|
72
|
+
try {
|
|
73
|
+
upstreamTools = await paymentClient.listTools();
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
logger.error('stdio-server', 'Failed to list tools from upstream server', {
|
|
77
|
+
error: error.message,
|
|
78
|
+
upstreamUrl: config.upstream.url
|
|
79
|
+
});
|
|
80
|
+
throw new Error(`Failed to connect to upstream MCP server: ${error.message}\n\n` +
|
|
81
|
+
`Please check:\n` +
|
|
82
|
+
` - The upstream server URL is correct: ${config.upstream.url}\n` +
|
|
83
|
+
` - The upstream server is running and healthy\n` +
|
|
84
|
+
` - You have network connectivity to the server`);
|
|
85
|
+
}
|
|
86
|
+
logger.info('stdio-server', 'Upstream tools loaded', {
|
|
87
|
+
toolCount: upstreamTools.tools.length,
|
|
88
|
+
tools: upstreamTools.tools.map(t => t.name)
|
|
89
|
+
});
|
|
90
|
+
// Verify tool schemas
|
|
91
|
+
upstreamTools.tools.forEach(tool => {
|
|
92
|
+
if (!tool.inputSchema) {
|
|
93
|
+
logger.warn('stdio-server', 'Tool missing inputSchema', {
|
|
94
|
+
toolName: tool.name
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
// Create MCP stdio server
|
|
99
|
+
const server = new Server({
|
|
100
|
+
name: 'fluxa-connect-mcp',
|
|
101
|
+
version: '1.0.0',
|
|
102
|
+
}, {
|
|
103
|
+
capabilities: {
|
|
104
|
+
tools: {},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
// Register tool list handler
|
|
108
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
109
|
+
logger.debug('stdio-server', 'ListTools request');
|
|
110
|
+
return upstreamTools;
|
|
111
|
+
});
|
|
112
|
+
// Register tool call handler
|
|
113
|
+
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
114
|
+
const { name, arguments: args } = request.params;
|
|
115
|
+
logger.info('stdio-server', 'Tool call received', {
|
|
116
|
+
toolName: name,
|
|
117
|
+
hasArguments: !!args
|
|
118
|
+
});
|
|
119
|
+
try {
|
|
120
|
+
const result = await paymentClient.callTool({ name, arguments: args });
|
|
121
|
+
logger.info('stdio-server', 'Tool call completed', { toolName: name });
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
logger.error('stdio-server', 'Tool call failed', {
|
|
126
|
+
toolName: name,
|
|
127
|
+
error: error.message
|
|
128
|
+
});
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
// Connect server to stdio
|
|
133
|
+
const stdioTransport = new StdioServerTransport();
|
|
134
|
+
await server.connect(stdioTransport);
|
|
135
|
+
logger.info('stdio-server', 'FluxA Connect MCP Server running on stdio');
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (error instanceof ConfigError || error instanceof AgentRegistrationError) {
|
|
139
|
+
logger.error('stdio-server', error.message);
|
|
140
|
+
process.exit(1);
|
|
141
|
+
}
|
|
142
|
+
logger.error('stdio-server', 'Fatal error', {
|
|
143
|
+
error: error.message,
|
|
144
|
+
stack: error.stack
|
|
145
|
+
});
|
|
146
|
+
process.exit(1);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
main();
|
|
150
|
+
//# sourceMappingURL=stdio-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stdio-server.js","sourceRoot":"","sources":["../../src/server/stdio-server.ts"],"names":[],"mappings":";AACA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AACnG,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,QAAQ,CAAC;AAC3C,OAAO,EACL,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,oCAAoC,CAAC;AAE5C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MA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AAK,EAAE,KAAK,CAAC,KAAK;SACnB,CAAC,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC;AAED,IAAI,EAAE,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FluxA API client
|
|
3
|
+
* All API calls to FluxA services centralized here
|
|
4
|
+
*/
|
|
5
|
+
export declare class APIError extends Error {
|
|
6
|
+
readonly statusCode?: number | undefined;
|
|
7
|
+
readonly responseData?: any | undefined;
|
|
8
|
+
constructor(message: string, statusCode?: number | undefined, responseData?: any | undefined);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Register agent with FluxA Agent Registry
|
|
12
|
+
*/
|
|
13
|
+
export declare function registerAgent(registryUrl: string, email: string, agentName: string, clientInfo: string): Promise<{
|
|
14
|
+
agent_id: string;
|
|
15
|
+
token: string;
|
|
16
|
+
jwt: string;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* Refresh JWT token
|
|
20
|
+
*/
|
|
21
|
+
export declare function refreshJWT(registryUrl: string, agentId: string, token: string): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Create X402 payment via FluxA Wallet Service
|
|
24
|
+
*/
|
|
25
|
+
export declare function createX402Payment(walletServiceUrl: string, agentJwt: string, paymentRequest: {
|
|
26
|
+
scheme: string;
|
|
27
|
+
network: string;
|
|
28
|
+
amount: string;
|
|
29
|
+
currency: string;
|
|
30
|
+
assetAddress: string;
|
|
31
|
+
payTo: string;
|
|
32
|
+
host: string;
|
|
33
|
+
resource: string;
|
|
34
|
+
description: string;
|
|
35
|
+
tokenName: string;
|
|
36
|
+
tokenVersion: string;
|
|
37
|
+
validityWindowSeconds: number;
|
|
38
|
+
}): Promise<{
|
|
39
|
+
status: string;
|
|
40
|
+
xPayment?: any;
|
|
41
|
+
approvalUrl?: string;
|
|
42
|
+
}>;
|
|
43
|
+
//# sourceMappingURL=api.d.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QAAQ,CAAC,EAAE,GAAG,CAAC;IACf,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC,CA8DD"}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* FluxA API client
|
|
3
|
+
* All API calls to FluxA services centralized here
|
|
4
|
+
*/
|
|
5
|
+
import axios from 'axios';
|
|
6
|
+
import { logger } from './logger.js';
|
|
7
|
+
export class APIError extends Error {
|
|
8
|
+
statusCode;
|
|
9
|
+
responseData;
|
|
10
|
+
constructor(message, statusCode, responseData) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.statusCode = statusCode;
|
|
13
|
+
this.responseData = responseData;
|
|
14
|
+
this.name = 'APIError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Register agent with FluxA Agent Registry
|
|
19
|
+
*/
|
|
20
|
+
export async function registerAgent(registryUrl, email, agentName, clientInfo) {
|
|
21
|
+
const url = `${registryUrl}/register`;
|
|
22
|
+
logger.debug('api', 'POST /register', {
|
|
23
|
+
registryUrl,
|
|
24
|
+
email,
|
|
25
|
+
agentName
|
|
26
|
+
});
|
|
27
|
+
try {
|
|
28
|
+
const response = await axios.post(url, {
|
|
29
|
+
email,
|
|
30
|
+
agent_name: agentName,
|
|
31
|
+
client_info: clientInfo
|
|
32
|
+
}, {
|
|
33
|
+
headers: {
|
|
34
|
+
'Content-Type': 'application/json'
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const data = response.data;
|
|
38
|
+
if (!data.jwt || !data.agent_id || !data.token) {
|
|
39
|
+
throw new APIError('Invalid response from registry: missing jwt, agent_id, or token', response.status, data);
|
|
40
|
+
}
|
|
41
|
+
logger.debug('api', 'POST /register - success', {
|
|
42
|
+
agentId: data.agent_id
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
agent_id: data.agent_id,
|
|
46
|
+
token: data.token,
|
|
47
|
+
jwt: data.jwt
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
if (error instanceof APIError) {
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
if (error.response) {
|
|
55
|
+
const statusCode = error.response.status;
|
|
56
|
+
const responseData = error.response.data;
|
|
57
|
+
logger.error('api', 'POST /register failed', {
|
|
58
|
+
statusCode,
|
|
59
|
+
message: responseData?.message || error.message
|
|
60
|
+
});
|
|
61
|
+
throw new APIError(responseData?.message || `Registration failed with status ${statusCode}`, statusCode, responseData);
|
|
62
|
+
}
|
|
63
|
+
// Network or other errors
|
|
64
|
+
logger.error('api', 'POST /register network error', {
|
|
65
|
+
error: error.message
|
|
66
|
+
});
|
|
67
|
+
throw new APIError(`Network error: ${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Refresh JWT token
|
|
72
|
+
*/
|
|
73
|
+
export async function refreshJWT(registryUrl, agentId, token) {
|
|
74
|
+
const url = `${registryUrl}/refresh`;
|
|
75
|
+
logger.debug('api', 'POST /refresh', { agentId });
|
|
76
|
+
try {
|
|
77
|
+
const response = await axios.post(url, {
|
|
78
|
+
agent_id: agentId,
|
|
79
|
+
token
|
|
80
|
+
}, {
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': 'application/json'
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
const data = response.data;
|
|
86
|
+
if (!data.jwt) {
|
|
87
|
+
throw new APIError('Invalid refresh response: missing jwt', response.status, data);
|
|
88
|
+
}
|
|
89
|
+
logger.debug('api', 'POST /refresh - success');
|
|
90
|
+
return data.jwt;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
if (error instanceof APIError) {
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
if (error.response) {
|
|
97
|
+
const statusCode = error.response.status;
|
|
98
|
+
const responseData = error.response.data;
|
|
99
|
+
logger.error('api', 'POST /refresh failed', {
|
|
100
|
+
statusCode,
|
|
101
|
+
message: responseData?.message || error.message
|
|
102
|
+
});
|
|
103
|
+
throw new APIError(responseData?.message || `JWT refresh failed with status ${statusCode}`, statusCode, responseData);
|
|
104
|
+
}
|
|
105
|
+
logger.error('api', 'POST /refresh network error', {
|
|
106
|
+
error: error.message
|
|
107
|
+
});
|
|
108
|
+
throw new APIError(`Network error: ${error.message}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Create X402 payment via FluxA Wallet Service
|
|
113
|
+
*/
|
|
114
|
+
export async function createX402Payment(walletServiceUrl, agentJwt, paymentRequest) {
|
|
115
|
+
// Sanitize URL - ensure no double slashes
|
|
116
|
+
const baseUrl = walletServiceUrl.endsWith('/')
|
|
117
|
+
? walletServiceUrl.slice(0, -1)
|
|
118
|
+
: walletServiceUrl;
|
|
119
|
+
const url = `${baseUrl}/api/payment/x402V1Payment`;
|
|
120
|
+
logger.debug('api', 'POST /api/payment/x402V1Payment', {
|
|
121
|
+
network: paymentRequest.network,
|
|
122
|
+
amount: paymentRequest.amount,
|
|
123
|
+
resource: paymentRequest.resource
|
|
124
|
+
});
|
|
125
|
+
try {
|
|
126
|
+
const response = await axios.post(url, paymentRequest, {
|
|
127
|
+
headers: {
|
|
128
|
+
'Authorization': `Bearer ${agentJwt}`,
|
|
129
|
+
'Content-Type': 'application/json'
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
const data = response.data;
|
|
133
|
+
logger.debug('api', 'POST /api/payment/x402V1Payment - success', {
|
|
134
|
+
status: data.status,
|
|
135
|
+
hasApprovalUrl: !!data.approvalUrl
|
|
136
|
+
});
|
|
137
|
+
return {
|
|
138
|
+
status: data.status,
|
|
139
|
+
xPayment: data.xPayment,
|
|
140
|
+
approvalUrl: data.approvalUrl
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
if (error instanceof APIError) {
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
if (error.response) {
|
|
148
|
+
const statusCode = error.response.status;
|
|
149
|
+
const responseData = error.response.data;
|
|
150
|
+
logger.error('api', 'POST /api/payment/x402V1Payment failed', {
|
|
151
|
+
statusCode,
|
|
152
|
+
code: responseData?.code,
|
|
153
|
+
message: responseData?.message || error.message
|
|
154
|
+
});
|
|
155
|
+
// Return error data for specialized handling by payment-service
|
|
156
|
+
throw new APIError(responseData?.message || `Payment creation failed with status ${statusCode}`, statusCode, responseData);
|
|
157
|
+
}
|
|
158
|
+
logger.error('api', 'POST /api/payment/x402V1Payment network error', {
|
|
159
|
+
error: error.message
|
|
160
|
+
});
|
|
161
|
+
throw new APIError(`Network error: ${error.message}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/utils/api.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,OAAO,QAAS,SAAQ,KAAK;IAGf;IACA;IAHlB,YACE,OAAe,EACC,UAAmB,EACnB,YAAkB;QAElC,KAAK,CAAC,OAAO,CAAC,CAAC;QAHC,eAAU,GAAV,UAAU,CAAS;QACnB,iBAAY,GAAZ,YAAY,CAAM;QAGlC,IAAI,CAAC,IAAI,GAAG,UAAU,CAAC;IACzB,CAAC;CACF;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,WAAmB,EACnB,KAAa,EACb,SAAiB,EACjB,UAAkB;IAMlB,MAAM,GAAG,GAAG,GAAG,WAAW,WAAW,CAAC;IAEtC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,gBAAgB,EAAE;QACpC,WAAW;QACX,KAAK;QACL,SAAS;KACV,CAAC,CAAC;IAEH,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC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|
5
|
+
/**
|
|
6
|
+
* Build agent authorization URL
|
|
7
|
+
*/
|
|
8
|
+
export declare function buildAuthorizationUrl(agentId: string, agentName: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Format agent authorization error message
|
|
11
|
+
*/
|
|
12
|
+
export declare function formatAgentNotFoundError(agentId: string, agentName: string, instructions?: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Format payment approval required error message
|
|
15
|
+
*/
|
|
16
|
+
export declare function formatApprovalRequiredError(approvalUrl: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Extract agent ID from FluxA error instructions
|
|
19
|
+
*/
|
|
20
|
+
export declare function extractAgentIdFromInstructions(instructions: string): string | null;
|
|
21
|
+
//# sourceMappingURL=error-messages.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-messages.d.ts","sourceRoot":"","sources":["../../src/utils/error-messages.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAGhF;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,MAAM,EACjB,YAAY,CAAC,EAAE,MAAM,GACpB,MAAM,CAeR;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAMvE;AAED;;GAEG;AACH,wBAAgB,8BAA8B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAGlF"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User-friendly error message formatting
|
|
3
|
+
* Centralizes error message construction for consistency
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Build agent authorization URL
|
|
7
|
+
*/
|
|
8
|
+
export function buildAuthorizationUrl(agentId, agentName) {
|
|
9
|
+
const encodedName = encodeURIComponent(agentName);
|
|
10
|
+
return `https://agentwallet.fluxapay.xyz/add-agent?agentId=${agentId}&name=${encodedName}`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Format agent authorization error message
|
|
14
|
+
*/
|
|
15
|
+
export function formatAgentNotFoundError(agentId, agentName, instructions) {
|
|
16
|
+
let message = 'Payment failed: Agent not authorized\n\n';
|
|
17
|
+
if (agentId) {
|
|
18
|
+
const authUrl = buildAuthorizationUrl(agentId, agentName);
|
|
19
|
+
message += 'Your agent needs to be authorized in FluxA wallet.\n\n';
|
|
20
|
+
message += `Please open this link to authorize:\n${authUrl}\n\n`;
|
|
21
|
+
message += 'After authorization, please retry this request.';
|
|
22
|
+
}
|
|
23
|
+
else if (instructions) {
|
|
24
|
+
message += instructions;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
message += 'Please contact support for assistance with agent authorization.';
|
|
28
|
+
}
|
|
29
|
+
return message;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Format payment approval required error message
|
|
33
|
+
*/
|
|
34
|
+
export function formatApprovalRequiredError(approvalUrl) {
|
|
35
|
+
return (`Payment requires approval.\n\n` +
|
|
36
|
+
`Please visit: ${approvalUrl}\n\n` +
|
|
37
|
+
`Then retry this operation.`);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Extract agent ID from FluxA error instructions
|
|
41
|
+
*/
|
|
42
|
+
export function extractAgentIdFromInstructions(instructions) {
|
|
43
|
+
const agentIdMatch = instructions.match(/ID:\s*([a-f0-9-]+)/i);
|
|
44
|
+
return agentIdMatch ? agentIdMatch[1] : null;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=error-messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-messages.js","sourceRoot":"","sources":["../../src/utils/error-messages.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAAe,EAAE,SAAiB;IACtE,MAAM,WAAW,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAClD,OAAO,sDAAsD,OAAO,SAAS,WAAW,EAAE,CAAC;AAC7F,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACtC,OAAe,EACf,SAAiB,EACjB,YAAqB;IAErB,IAAI,OAAO,GAAG,0CAA0C,CAAC;IAEzD,IAAI,OAAO,EAAE,CAAC;QACZ,MAAM,OAAO,GAAG,qBAAqB,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;QAC1D,OAAO,IAAI,wDAAwD,CAAC;QACpE,OAAO,IAAI,wCAAwC,OAAO,MAAM,CAAC;QACjE,OAAO,IAAI,iDAAiD,CAAC;IAC/D,CAAC;SAAM,IAAI,YAAY,EAAE,CAAC;QACxB,OAAO,IAAI,YAAY,CAAC;IAC1B,CAAC;SAAM,CAAC;QACN,OAAO,IAAI,iEAAiE,CAAC;IAC/E,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,2BAA2B,CAAC,WAAmB;IAC7D,OAAO,CACL,gCAAgC;QAChC,iBAAiB,WAAW,MAAM;QAClC,4BAA4B,CAC7B,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAAC,YAAoB;IACjE,MAAM,YAAY,GAAG,YAAY,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC/D,OAAO,YAAY,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/C,CAAC"}
|