@atxp/client 0.2.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/README.md +112 -0
- package/dist/atxpAccount.d.ts +13 -0
- package/dist/atxpAccount.d.ts.map +1 -0
- package/dist/atxpAccount.js +81 -0
- package/dist/atxpAccount.js.map +1 -0
- package/dist/atxpClient.d.ts +14 -0
- package/dist/atxpClient.d.ts.map +1 -0
- package/dist/atxpClient.js +67 -0
- package/dist/atxpClient.js.map +1 -0
- package/dist/atxpFetcher.d.ts +71 -0
- package/dist/atxpFetcher.d.ts.map +1 -0
- package/dist/atxpFetcher.js +347 -0
- package/dist/atxpFetcher.js.map +1 -0
- package/dist/clientTestHelpers.d.ts +6 -0
- package/dist/clientTestHelpers.d.ts.map +1 -0
- package/dist/clientTestHelpers.js +94 -0
- package/dist/clientTestHelpers.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/oAuth.d.ts +44 -0
- package/dist/oAuth.d.ts.map +1 -0
- package/dist/oAuth.js +274 -0
- package/dist/oAuth.js.map +1 -0
- package/dist/setup.expo.d.ts +2 -0
- package/dist/setup.expo.d.ts.map +1 -0
- package/dist/setup.expo.js +42 -0
- package/dist/setup.expo.js.map +1 -0
- package/dist/solanaAccount.d.ts +9 -0
- package/dist/solanaAccount.d.ts.map +1 -0
- package/dist/solanaAccount.js +19 -0
- package/dist/solanaAccount.js.map +1 -0
- package/dist/solanaPaymentMaker.d.ts +17 -0
- package/dist/solanaPaymentMaker.d.ts.map +1 -0
- package/dist/solanaPaymentMaker.js +62 -0
- package/dist/solanaPaymentMaker.js.map +1 -0
- package/dist/src/atxpClient.d.ts +14 -0
- package/dist/src/atxpClient.d.ts.map +1 -0
- package/dist/src/atxpClient.js +62 -0
- package/dist/src/atxpClient.js.map +1 -0
- package/dist/src/atxpFetcher.d.ts +71 -0
- package/dist/src/atxpFetcher.d.ts.map +1 -0
- package/dist/src/atxpFetcher.js +347 -0
- package/dist/src/atxpFetcher.js.map +1 -0
- package/dist/src/clientTestHelpers.d.ts +6 -0
- package/dist/src/clientTestHelpers.d.ts.map +1 -0
- package/dist/src/clientTestHelpers.js +94 -0
- package/dist/src/clientTestHelpers.js.map +1 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +7 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/oAuth.d.ts +44 -0
- package/dist/src/oAuth.d.ts.map +1 -0
- package/dist/src/oAuth.js +274 -0
- package/dist/src/oAuth.js.map +1 -0
- package/dist/src/setup.expo.d.ts +2 -0
- package/dist/src/setup.expo.d.ts.map +1 -0
- package/dist/src/setup.expo.js +42 -0
- package/dist/src/setup.expo.js.map +1 -0
- package/dist/src/solanaAccount.d.ts +9 -0
- package/dist/src/solanaAccount.d.ts.map +1 -0
- package/dist/src/solanaAccount.js +19 -0
- package/dist/src/solanaAccount.js.map +1 -0
- package/dist/src/solanaPaymentMaker.d.ts +17 -0
- package/dist/src/solanaPaymentMaker.d.ts.map +1 -0
- package/dist/src/solanaPaymentMaker.js +62 -0
- package/dist/src/solanaPaymentMaker.js.map +1 -0
- package/dist/src/types.d.ts +58 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +2 -0
- package/dist/src/types.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +58 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# @atxp/client
|
|
2
|
+
|
|
3
|
+
ATXP Client - MCP client with OAuth authentication and payment processing capabilities.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The ATXP Client package provides a comprehensive solution for connecting to MCP (Model Context Protocol) servers that require OAuth authentication and payment processing. It handles the complete flow from initial connection through authentication, payment verification, and tool execution.
|
|
8
|
+
|
|
9
|
+
**🚀 Getting Started**: Learn more about ATXP in [the docs](https://docs.atxp.ai/atxp), and follow the [Agent Quickstart](https://docs.atxp.ai/client) to build your first ATXP-powered agent.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🔐 **OAuth Authentication**: Full OAuth 2.0 flow implementation
|
|
14
|
+
- 💰 **Payment Processing**: Solana-based payment integration
|
|
15
|
+
- 🔌 **MCP Protocol**: Complete Model Context Protocol client implementation
|
|
16
|
+
- 📱 **Multi-Platform**: Supports Node.js and React Native/Expo environments
|
|
17
|
+
- 🛡️ **Type Safety**: Full TypeScript support with comprehensive type definitions
|
|
18
|
+
- 🔄 **Event System**: Real-time connection and authentication status updates
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @atxp/client
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Basic Usage
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { ATXPClient } from '@atxp/client';
|
|
30
|
+
|
|
31
|
+
const client = new ATXPClient({
|
|
32
|
+
serverUrl: 'http://localhost:3010',
|
|
33
|
+
clientId: 'your-oauth-client-id',
|
|
34
|
+
authServerUrl: 'https://auth.atxp.ai'
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Connect and authenticate
|
|
38
|
+
await client.connect();
|
|
39
|
+
|
|
40
|
+
// Call tools that require payment
|
|
41
|
+
const result = await client.callTool('hello_world', {
|
|
42
|
+
name: 'Alice',
|
|
43
|
+
message: 'Hello from ATXP!'
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Platform Support
|
|
48
|
+
|
|
49
|
+
- **Node.js**: Full support with built-in OAuth database
|
|
50
|
+
- **React Native/Expo**: Requires `expo-crypto` and `expo-sqlite` peer dependencies
|
|
51
|
+
|
|
52
|
+
## Authentication Flow
|
|
53
|
+
|
|
54
|
+
1. Client initiates connection to MCP server
|
|
55
|
+
2. Server responds with OAuth challenge
|
|
56
|
+
3. Client redirects user to authentication server
|
|
57
|
+
4. User completes OAuth flow and returns with authorization code
|
|
58
|
+
5. Client exchanges code for access token
|
|
59
|
+
6. Authenticated requests can now be made to the server
|
|
60
|
+
|
|
61
|
+
## Payment Processing
|
|
62
|
+
|
|
63
|
+
When a tool requires payment, the client automatically:
|
|
64
|
+
|
|
65
|
+
1. Detects payment requirement from server response
|
|
66
|
+
2. Creates Solana payment transaction
|
|
67
|
+
3. Prompts user to approve payment
|
|
68
|
+
4. Submits payment proof to server
|
|
69
|
+
5. Retries original tool call upon payment confirmation
|
|
70
|
+
|
|
71
|
+
## API Reference
|
|
72
|
+
|
|
73
|
+
### ATXPClient
|
|
74
|
+
|
|
75
|
+
Main client class for ATXP protocol communication.
|
|
76
|
+
|
|
77
|
+
#### Constructor Options
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
interface ATXPClientConfig {
|
|
81
|
+
serverUrl: string; // MCP server URL
|
|
82
|
+
clientId: string; // OAuth client ID
|
|
83
|
+
authServerUrl?: string; // Auth server URL (defaults to https://auth.atxp.ai)
|
|
84
|
+
oAuthDb?: OAuthDb; // Custom OAuth database implementation
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Methods
|
|
89
|
+
|
|
90
|
+
- `connect()`: Establish connection to MCP server
|
|
91
|
+
- `disconnect()`: Close connection and cleanup resources
|
|
92
|
+
- `callTool(name, args)`: Execute server tool with automatic auth/payment handling
|
|
93
|
+
- `listTools()`: Get available tools from server
|
|
94
|
+
- `getAuthStatus()`: Check current authentication status
|
|
95
|
+
|
|
96
|
+
#### Events
|
|
97
|
+
|
|
98
|
+
- `authenticated`: Fired when OAuth flow completes successfully
|
|
99
|
+
- `paymentRequired`: Fired when tool execution requires payment
|
|
100
|
+
- `paymentCompleted`: Fired when payment is confirmed
|
|
101
|
+
- `error`: Fired on connection or protocol errors
|
|
102
|
+
|
|
103
|
+
## Examples
|
|
104
|
+
|
|
105
|
+
See the `examples/` directory for complete working examples:
|
|
106
|
+
|
|
107
|
+
- `examples/basic/`: Simple Node.js client implementation
|
|
108
|
+
- `examples/server/`: Compatible MCP server with payment requirements
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Account, PaymentMaker } from './types.js';
|
|
2
|
+
import type { FetchLike } from '@atxp/common';
|
|
3
|
+
export declare class ATXPAccount implements Account {
|
|
4
|
+
accountId: string;
|
|
5
|
+
paymentMakers: {
|
|
6
|
+
[key: string]: PaymentMaker;
|
|
7
|
+
};
|
|
8
|
+
constructor(connectionString: string, opts?: {
|
|
9
|
+
fetchFn?: FetchLike;
|
|
10
|
+
network?: string;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=atxpAccount.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atxpAccount.d.ts","sourceRoot":"","sources":["../src/atxpAccount.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAgF7C,qBAAa,WAAY,YAAW,OAAO;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAA;KAAE,CAAC;gBAEnC,gBAAgB,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,SAAS,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE;CAWvF"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
function toBasicAuth(token) {
|
|
2
|
+
// Basic auth is base64("username:password"), password is blank
|
|
3
|
+
const b64 = Buffer.from(`${token}:`).toString('base64');
|
|
4
|
+
return `Basic ${b64}`;
|
|
5
|
+
}
|
|
6
|
+
function parseConnectionString(connectionString) {
|
|
7
|
+
const url = new URL(connectionString);
|
|
8
|
+
const origin = url.origin;
|
|
9
|
+
const token = url.searchParams.get('connection_token') || '';
|
|
10
|
+
if (!token) {
|
|
11
|
+
throw new Error('ATXPAccount: connection string missing connection token');
|
|
12
|
+
}
|
|
13
|
+
return { origin, token };
|
|
14
|
+
}
|
|
15
|
+
class ATXPHttpPaymentMaker {
|
|
16
|
+
constructor(origin, token, fetchFn = fetch) {
|
|
17
|
+
this.origin = origin;
|
|
18
|
+
this.token = token;
|
|
19
|
+
this.fetchFn = fetchFn;
|
|
20
|
+
}
|
|
21
|
+
async makePayment(amount, currency, receiver, memo) {
|
|
22
|
+
const body = {
|
|
23
|
+
amount: amount.toString(),
|
|
24
|
+
currency: (currency || '').toLowerCase() === 'usdc' ? 'usdc' : 'usdc',
|
|
25
|
+
receiver,
|
|
26
|
+
memo,
|
|
27
|
+
};
|
|
28
|
+
const response = await this.fetchFn(`${this.origin}/pay`, {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
headers: {
|
|
31
|
+
'Authorization': toBasicAuth(this.token),
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify(body),
|
|
35
|
+
});
|
|
36
|
+
if (!response.ok) {
|
|
37
|
+
const text = await response.text();
|
|
38
|
+
throw new Error(`ATXPAccount: /pay failed: ${response.status} ${response.statusText} ${text}`);
|
|
39
|
+
}
|
|
40
|
+
const json = await response.json();
|
|
41
|
+
if (!json?.txHash) {
|
|
42
|
+
throw new Error('ATXPAccount: /pay did not return txHash');
|
|
43
|
+
}
|
|
44
|
+
return json.txHash;
|
|
45
|
+
}
|
|
46
|
+
async generateJWT(params) {
|
|
47
|
+
const response = await this.fetchFn(`${this.origin}/sign`, {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
headers: {
|
|
50
|
+
'Authorization': toBasicAuth(this.token),
|
|
51
|
+
'Content-Type': 'application/json',
|
|
52
|
+
},
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
paymentRequestId: params.paymentRequestId,
|
|
55
|
+
codeChallenge: params.codeChallenge,
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
const text = await response.text();
|
|
60
|
+
throw new Error(`ATXPAccount: /sign failed: ${response.status} ${response.statusText} ${text}`);
|
|
61
|
+
}
|
|
62
|
+
const json = await response.json();
|
|
63
|
+
if (!json?.jwt) {
|
|
64
|
+
throw new Error('ATXPAccount: /sign did not return jwt');
|
|
65
|
+
}
|
|
66
|
+
return json.jwt;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export class ATXPAccount {
|
|
70
|
+
constructor(connectionString, opts) {
|
|
71
|
+
const { origin, token } = parseConnectionString(connectionString);
|
|
72
|
+
const fetchFn = opts?.fetchFn ?? fetch;
|
|
73
|
+
const network = opts?.network ?? 'solana';
|
|
74
|
+
// Use token as a stable accountId namespace to keep OAuth/ATXP state per-connection
|
|
75
|
+
this.accountId = `atxp:${token}`;
|
|
76
|
+
this.paymentMakers = {
|
|
77
|
+
[network]: new ATXPHttpPaymentMaker(origin, token, fetchFn),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=atxpAccount.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atxpAccount.js","sourceRoot":"","sources":["../src/atxpAccount.ts"],"names":[],"mappings":"AAIA,SAAS,WAAW,CAAC,KAAa;IAChC,+DAA+D;IAC/D,MAAM,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACxD,OAAO,SAAS,GAAG,EAAE,CAAC;AACxB,CAAC;AAED,SAAS,qBAAqB,CAAC,gBAAwB;IAC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CAAC;IAClB,CAAC;CACF;AAED,MAAM,OAAO,WAAW;IAItB,YAAY,gBAAwB,EAAE,IAAgD;QACpF,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;QAClE,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,KAAK,CAAC;QACvC,MAAM,OAAO,GAAG,IAAI,EAAE,OAAO,IAAI,QAAQ,CAAC;QAE1C,oFAAoF;QACpF,IAAI,CAAC,SAAS,GAAG,QAAQ,KAAK,EAAE,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG;YACnB,CAAC,OAAO,CAAC,EAAE,IAAI,oBAAoB,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC;SAC5D,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ClientConfig } from "./types.js";
|
|
2
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
3
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
4
|
+
type RequiredClientConfigFields = 'mcpServer' | 'account';
|
|
5
|
+
type RequiredClientConfig = Pick<ClientConfig, RequiredClientConfigFields>;
|
|
6
|
+
type OptionalClientConfig = Omit<ClientConfig, RequiredClientConfigFields>;
|
|
7
|
+
export type ClientArgs = RequiredClientConfig & Partial<OptionalClientConfig>;
|
|
8
|
+
type BuildableClientConfigFields = 'oAuthDb' | 'logger';
|
|
9
|
+
export declare const DEFAULT_CLIENT_CONFIG: Required<Omit<OptionalClientConfig, BuildableClientConfigFields>>;
|
|
10
|
+
export declare function buildClientConfig(args: ClientArgs): ClientConfig;
|
|
11
|
+
export declare function buildStreamableTransport(args: ClientArgs): StreamableHTTPClientTransport;
|
|
12
|
+
export declare function atxpClient(args: ClientArgs): Promise<Client>;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=atxpClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atxpClient.d.ts","sourceRoot":"","sources":["../src/atxpClient.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAG1C,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AAEnG,KAAK,0BAA0B,GAAG,WAAW,GAAG,SAAS,CAAC;AAC1D,KAAK,oBAAoB,GAAG,IAAI,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAC;AAC3E,KAAK,oBAAoB,GAAG,IAAI,CAAC,YAAY,EAAE,0BAA0B,CAAC,CAAC;AAC3E,MAAM,MAAM,UAAU,GAAG,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;AAC9E,KAAK,2BAA2B,GAAG,SAAS,GAAG,QAAQ,CAAC;AAExD,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,IAAI,CAAC,oBAAoB,EAAE,2BAA2B,CAAC,CAiBnG,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,GAAG,YAAY,CAehE;AAED,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,UAAU,GAAG,6BAA6B,CAoBxF;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAQlE"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { MemoryOAuthDb, ConsoleLogger, DEFAULT_AUTHORIZATION_SERVER } from "@atxp/common";
|
|
2
|
+
import { ATXPFetcher } from "./atxpFetcher.js";
|
|
3
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
4
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
5
|
+
export const DEFAULT_CLIENT_CONFIG = {
|
|
6
|
+
allowedAuthorizationServers: [DEFAULT_AUTHORIZATION_SERVER],
|
|
7
|
+
approvePayment: async (_p) => true,
|
|
8
|
+
fetchFn: fetch,
|
|
9
|
+
oAuthChannelFetch: fetch,
|
|
10
|
+
allowHttp: false, // may be overridden in buildClientConfig by process.env.NODE_ENV
|
|
11
|
+
clientInfo: {
|
|
12
|
+
name: 'ATXPClient',
|
|
13
|
+
version: '0.0.1'
|
|
14
|
+
},
|
|
15
|
+
clientOptions: {
|
|
16
|
+
capabilities: {}
|
|
17
|
+
},
|
|
18
|
+
onAuthorize: async () => { },
|
|
19
|
+
onAuthorizeFailure: async () => { },
|
|
20
|
+
onPayment: async () => { },
|
|
21
|
+
onPaymentFailure: async () => { }
|
|
22
|
+
};
|
|
23
|
+
export function buildClientConfig(args) {
|
|
24
|
+
// Use fetchFn for oAuthChannelFetch if the latter isn't explicitly set
|
|
25
|
+
if (args.fetchFn && !args.oAuthChannelFetch) {
|
|
26
|
+
args.oAuthChannelFetch = args.fetchFn;
|
|
27
|
+
}
|
|
28
|
+
// Read environment variable at runtime, not module load time
|
|
29
|
+
const envDefaults = {
|
|
30
|
+
...DEFAULT_CLIENT_CONFIG,
|
|
31
|
+
allowHttp: process.env.NODE_ENV === 'development',
|
|
32
|
+
};
|
|
33
|
+
const withDefaults = { ...envDefaults, ...args };
|
|
34
|
+
const logger = withDefaults.logger ?? new ConsoleLogger();
|
|
35
|
+
const oAuthDb = withDefaults.oAuthDb ?? new MemoryOAuthDb({ logger });
|
|
36
|
+
const built = { oAuthDb, logger };
|
|
37
|
+
return Object.freeze({ ...withDefaults, ...built });
|
|
38
|
+
}
|
|
39
|
+
;
|
|
40
|
+
export function buildStreamableTransport(args) {
|
|
41
|
+
const config = buildClientConfig(args);
|
|
42
|
+
const fetcher = new ATXPFetcher({
|
|
43
|
+
accountId: args.account.accountId,
|
|
44
|
+
db: config.oAuthDb,
|
|
45
|
+
paymentMakers: args.account.paymentMakers,
|
|
46
|
+
fetchFn: config.fetchFn,
|
|
47
|
+
sideChannelFetch: config.oAuthChannelFetch,
|
|
48
|
+
allowInsecureRequests: config.allowHttp,
|
|
49
|
+
allowedAuthorizationServers: config.allowedAuthorizationServers,
|
|
50
|
+
approvePayment: config.approvePayment,
|
|
51
|
+
logger: config.logger,
|
|
52
|
+
onAuthorize: config.onAuthorize,
|
|
53
|
+
onAuthorizeFailure: config.onAuthorizeFailure,
|
|
54
|
+
onPayment: config.onPayment,
|
|
55
|
+
onPaymentFailure: config.onPaymentFailure
|
|
56
|
+
});
|
|
57
|
+
const transport = new StreamableHTTPClientTransport(new URL(args.mcpServer), { fetch: fetcher.fetch });
|
|
58
|
+
return transport;
|
|
59
|
+
}
|
|
60
|
+
export async function atxpClient(args) {
|
|
61
|
+
const config = buildClientConfig(args);
|
|
62
|
+
const transport = buildStreamableTransport(config);
|
|
63
|
+
const client = new Client(config.clientInfo, config.clientOptions);
|
|
64
|
+
await client.connect(transport);
|
|
65
|
+
return client;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=atxpClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atxpClient.js","sourceRoot":"","sources":["../src/atxpClient.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,4BAA4B,EAAE,MAAM,cAAc,CAAC;AAC1F,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,6BAA6B,EAAE,MAAM,oDAAoD,CAAC;AAQnG,MAAM,CAAC,MAAM,qBAAqB,GAAsE;IACtG,2BAA2B,EAAE,CAAC,4BAA4B,CAAC;IAC3D,cAAc,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,IAAI;IAClC,OAAO,EAAE,KAAK;IACd,iBAAiB,EAAE,KAAK;IACxB,SAAS,EAAE,KAAK,EAAE,iEAAiE;IACnF,UAAU,EAAE;QACV,IAAI,EAAE,YAAY;QAClB,OAAO,E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AAC,OAAO;QAClB,aAAa,EAAE,IAAI,CAAC,OAAO,CAAC,aAAa;QACzC,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,gBAAgB,EAAE,MAAM,CAAC,iBAAiB;QAC1C,qBAAqB,EAAE,MAAM,CAAC,SAAS;QACvC,2BAA2B,EAAE,MAAM,CAAC,2BAA2B;QAC/D,cAAc,EAAE,MAAM,CAAC,cAAc;QACrC,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,kBAAkB,EAAE,MAAM,CAAC,kBAAkB;QAC7C,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,gBAAgB,EAAE,MAAM,CAAC,gBAAgB;KAC1C,CAAC,CAAC;IACH,MAAM,SAAS,GAAG,IAAI,6BAA6B,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAC,KAAK,EAAE,OAAO,CAAC,KAAK,EAAC,CAAC,CAAC;IACrG,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,IAAgB;IAC/C,MAAM,MAAM,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACvC,MAAM,SAAS,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAAC;IAEnD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,aAAa,CAAC,CAAC;IACnE,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAEhC,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { OAuthAuthenticationRequiredError, OAuthClient } from './oAuth.js';
|
|
2
|
+
import { AccessToken, AuthorizationServerUrl, FetchLike, OAuthDb, PaymentRequestData, Logger } from '@atxp/common';
|
|
3
|
+
import type { PaymentMaker, ProspectivePayment } from './types.js';
|
|
4
|
+
import { McpError } from '@modelcontextprotocol/sdk/types.js';
|
|
5
|
+
export interface ATXPFetcherConfig {
|
|
6
|
+
accountId: string;
|
|
7
|
+
db: OAuthDb;
|
|
8
|
+
paymentMakers: {
|
|
9
|
+
[key: string]: PaymentMaker;
|
|
10
|
+
};
|
|
11
|
+
fetchFn?: FetchLike;
|
|
12
|
+
sideChannelFetch?: FetchLike;
|
|
13
|
+
strict?: boolean;
|
|
14
|
+
allowInsecureRequests?: boolean;
|
|
15
|
+
allowedAuthorizationServers?: AuthorizationServerUrl[];
|
|
16
|
+
approvePayment?: (payment: ProspectivePayment) => Promise<boolean>;
|
|
17
|
+
logger?: Logger;
|
|
18
|
+
onAuthorize?: (args: {
|
|
19
|
+
authorizationServer: AuthorizationServerUrl;
|
|
20
|
+
userId: string;
|
|
21
|
+
}) => Promise<void>;
|
|
22
|
+
onAuthorizeFailure?: (args: {
|
|
23
|
+
authorizationServer: AuthorizationServerUrl;
|
|
24
|
+
userId: string;
|
|
25
|
+
error: Error;
|
|
26
|
+
}) => Promise<void>;
|
|
27
|
+
onPayment?: (args: {
|
|
28
|
+
payment: ProspectivePayment;
|
|
29
|
+
}) => Promise<void>;
|
|
30
|
+
onPaymentFailure?: (args: {
|
|
31
|
+
payment: ProspectivePayment;
|
|
32
|
+
error: Error;
|
|
33
|
+
}) => Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export declare function atxpFetch(config: ATXPFetcherConfig): FetchLike;
|
|
36
|
+
export declare class ATXPFetcher {
|
|
37
|
+
protected oauthClient: OAuthClient;
|
|
38
|
+
protected paymentMakers: Map<string, PaymentMaker>;
|
|
39
|
+
protected sideChannelFetch: FetchLike;
|
|
40
|
+
protected db: OAuthDb;
|
|
41
|
+
protected accountId: string;
|
|
42
|
+
protected allowedAuthorizationServers: AuthorizationServerUrl[];
|
|
43
|
+
protected approvePayment: (payment: ProspectivePayment) => Promise<boolean>;
|
|
44
|
+
protected logger: Logger;
|
|
45
|
+
protected onAuthorize: (args: {
|
|
46
|
+
authorizationServer: AuthorizationServerUrl;
|
|
47
|
+
userId: string;
|
|
48
|
+
}) => Promise<void>;
|
|
49
|
+
protected onAuthorizeFailure: (args: {
|
|
50
|
+
authorizationServer: AuthorizationServerUrl;
|
|
51
|
+
userId: string;
|
|
52
|
+
error: Error;
|
|
53
|
+
}) => Promise<void>;
|
|
54
|
+
protected onPayment: (args: {
|
|
55
|
+
payment: ProspectivePayment;
|
|
56
|
+
}) => Promise<void>;
|
|
57
|
+
protected onPaymentFailure: (args: {
|
|
58
|
+
payment: ProspectivePayment;
|
|
59
|
+
error: Error;
|
|
60
|
+
}) => Promise<void>;
|
|
61
|
+
constructor({ accountId, db, paymentMakers, fetchFn, sideChannelFetch, strict, allowInsecureRequests, allowedAuthorizationServers, approvePayment, logger, onAuthorize, onAuthorizeFailure, onPayment, onPaymentFailure }: ATXPFetcherConfig);
|
|
62
|
+
protected handlePaymentRequestError: (paymentRequestError: McpError) => Promise<boolean>;
|
|
63
|
+
protected getPaymentRequestData: (paymentRequestUrl: string) => Promise<PaymentRequestData | null>;
|
|
64
|
+
protected isAllowedAuthServer: (url: string | URL) => boolean;
|
|
65
|
+
protected makeAuthRequestWithPaymentMaker: (authorizationUrl: URL, paymentMaker: PaymentMaker) => Promise<string>;
|
|
66
|
+
protected authToService: (error: OAuthAuthenticationRequiredError) => Promise<void>;
|
|
67
|
+
protected exchangeToken: (myToken: AccessToken, newResourceUrl: string) => Promise<AccessToken>;
|
|
68
|
+
protected checkForATXPResponse: (response: Response) => Promise<void>;
|
|
69
|
+
fetch: FetchLike;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=atxpFetcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atxpFetcher.d.ts","sourceRoot":"","sources":["../src/atxpFetcher.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gCAAgC,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC3E,OAAO,EAAqD,WAAW,EAAE,sBAAsB,EA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