@atxp/common 0.2.14 → 0.2.16
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 +2 -172
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,176 +6,6 @@ ATXP Core - Shared utilities and types for Authorization Token Exchange Protocol
|
|
|
6
6
|
|
|
7
7
|
The ATXP Common package provides shared utilities, types, and core functionality used by both ATXP client and server implementations. It contains the foundational components for OAuth authentication, JWT token handling, payment processing, and MCP protocol utilities.
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
## Getting Started:
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- 🔐 **JWT Token Management**: Secure token creation, validation, and parsing
|
|
14
|
-
- 🗄️ **OAuth Database**: Abstract database interface with memory implementation
|
|
15
|
-
- 📡 **MCP JSON Utilities**: Type-safe MCP protocol message handling
|
|
16
|
-
- 💰 **Payment Error Handling**: Structured payment requirement error types
|
|
17
|
-
- 🛠️ **Platform Utilities**: Cross-platform compatibility helpers
|
|
18
|
-
- 📊 **SSE Parser**: Server-sent events parsing for real-time communication
|
|
19
|
-
- 🔧 **Logger**: Configurable logging system
|
|
20
|
-
|
|
21
|
-
## Installation
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
npm install @atxp/common
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
This package is typically installed automatically as a dependency of `@atxp/client` or `@atxp/server`.
|
|
28
|
-
|
|
29
|
-
## Key Components
|
|
30
|
-
|
|
31
|
-
### JWT Token Management
|
|
32
|
-
|
|
33
|
-
```typescript
|
|
34
|
-
import { createJWT, verifyJWT, parseJWT } from '@atxp/common';
|
|
35
|
-
|
|
36
|
-
// Create signed JWT
|
|
37
|
-
const token = await createJWT({ userId: '123' }, secretKey);
|
|
38
|
-
|
|
39
|
-
// Verify and parse JWT
|
|
40
|
-
const payload = await verifyJWT(token, secretKey);
|
|
41
|
-
|
|
42
|
-
// Parse without verification (for debugging)
|
|
43
|
-
const { header, payload } = parseJWT(token);
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
### OAuth Database Interface
|
|
47
|
-
|
|
48
|
-
```typescript
|
|
49
|
-
import { OAuthDb, MemoryOAuthDb } from '@atxp/common';
|
|
50
|
-
|
|
51
|
-
// Use in-memory database for development
|
|
52
|
-
const oAuthDb = new MemoryOAuthDb();
|
|
53
|
-
|
|
54
|
-
// Store OAuth tokens
|
|
55
|
-
await oAuthDb.storeTokens('client123', {
|
|
56
|
-
access_token: 'token',
|
|
57
|
-
refresh_token: 'refresh',
|
|
58
|
-
expires_in: 3600
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Retrieve tokens
|
|
62
|
-
const tokens = await oAuthDb.getTokens('client123');
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
### MCP Protocol Utilities
|
|
66
|
-
|
|
67
|
-
```typescript
|
|
68
|
-
import {
|
|
69
|
-
MCPRequest,
|
|
70
|
-
MCPResponse,
|
|
71
|
-
MCPError,
|
|
72
|
-
createMCPResponse,
|
|
73
|
-
createMCPError
|
|
74
|
-
} from '@atxp/common';
|
|
75
|
-
|
|
76
|
-
// Type-safe MCP request handling
|
|
77
|
-
function handleMCPRequest(request: MCPRequest): MCPResponse {
|
|
78
|
-
if (request.method === 'tools/call') {
|
|
79
|
-
return createMCPResponse(request.id, { result: 'success' });
|
|
80
|
-
}
|
|
81
|
-
return createMCPError(request.id, -32601, 'Method not found');
|
|
82
|
-
}
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
### Payment Error Handling
|
|
86
|
-
|
|
87
|
-
```typescript
|
|
88
|
-
import { PaymentRequiredError } from '@atxp/common';
|
|
89
|
-
|
|
90
|
-
// Throw structured payment requirement
|
|
91
|
-
throw new PaymentRequiredError({
|
|
92
|
-
amount: '0.01',
|
|
93
|
-
currency: 'USDC',
|
|
94
|
-
destination: 'solana-wallet-address',
|
|
95
|
-
reference: 'unique-payment-reference'
|
|
96
|
-
});
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### Platform Utilities
|
|
100
|
-
|
|
101
|
-
```typescript
|
|
102
|
-
import { crypto, isNode, getIsReactNative } from '@atxp/common';
|
|
103
|
-
|
|
104
|
-
// Detect runtime environment
|
|
105
|
-
if (isNode) {
|
|
106
|
-
// Node.js specific code - can use SQLite or Redis
|
|
107
|
-
} else if (getIsReactNative()) {
|
|
108
|
-
// React Native/Expo - use MemoryOAuthDb
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// Use platform-specific crypto
|
|
112
|
-
const hash = await crypto.digest(new TextEncoder().encode('data'));
|
|
113
|
-
const uuid = crypto.randomUUID();
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## Type Definitions
|
|
117
|
-
|
|
118
|
-
The package exports comprehensive TypeScript types for:
|
|
119
|
-
|
|
120
|
-
- **OAuth Types**: Token responses, authorization flows, client configurations
|
|
121
|
-
- **MCP Types**: Protocol messages, tool definitions, error structures
|
|
122
|
-
- **Payment Types**: Payment requirements, transaction references, currency info
|
|
123
|
-
- **Server Types**: Authentication contexts, middleware definitions
|
|
124
|
-
|
|
125
|
-
## Database Implementations
|
|
126
|
-
|
|
127
|
-
### MemoryOAuthDb
|
|
128
|
-
|
|
129
|
-
In-memory OAuth token storage for development and testing:
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
import { MemoryOAuthDb } from '@atxp/common';
|
|
133
|
-
|
|
134
|
-
const db = new MemoryOAuthDb();
|
|
135
|
-
// Tokens stored in memory, cleared on process restart
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
### Additional Database Options
|
|
139
|
-
|
|
140
|
-
For production use cases requiring persistent storage, see separate database packages:
|
|
141
|
-
|
|
142
|
-
- **`@atxp/sqlite`**: SQLite implementation using `better-sqlite3` (Node.js only)
|
|
143
|
-
- **`@atxp/redis`**: Redis implementation using `ioredis` for distributed applications
|
|
144
|
-
|
|
145
|
-
```bash
|
|
146
|
-
npm install @atxp/sqlite # For SQLite storage
|
|
147
|
-
npm install @atxp/redis # For Redis storage
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
## Logging
|
|
151
|
-
|
|
152
|
-
Configurable logging system with multiple levels:
|
|
153
|
-
|
|
154
|
-
```typescript
|
|
155
|
-
import { Logger } from '@atxp/common';
|
|
156
|
-
|
|
157
|
-
const logger = new Logger('MyComponent');
|
|
158
|
-
|
|
159
|
-
logger.debug('Debug message');
|
|
160
|
-
logger.info('Info message');
|
|
161
|
-
logger.warn('Warning message');
|
|
162
|
-
logger.error('Error message');
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
## Utilities
|
|
166
|
-
|
|
167
|
-
Various utility functions for:
|
|
168
|
-
- Async operation helpers
|
|
169
|
-
- Data validation and sanitization
|
|
170
|
-
- Error handling and formatting
|
|
171
|
-
- Time and date operations
|
|
172
|
-
|
|
173
|
-
## Examples
|
|
174
|
-
|
|
175
|
-
This package is used internally by the client and server packages. See:
|
|
176
|
-
- `@atxp/client` for client-side usage examples
|
|
177
|
-
- `@atxp/server` for server-side usage examples
|
|
178
|
-
|
|
179
|
-
## License
|
|
180
|
-
|
|
181
|
-
MIT
|
|
11
|
+
Learn more about ATXP in [the docs](https://docs.atxp.ai/atxp).
|