atproto-mcp 0.1.0
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/LICENSE +22 -0
- package/README.md +293 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +261 -0
- package/dist/cli.js.map +1 -0
- package/dist/health-check.d.ts +7 -0
- package/dist/health-check.d.ts.map +1 -0
- package/dist/health-check.js +57 -0
- package/dist/health-check.js.map +1 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +612 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts/index.d.ts +77 -0
- package/dist/prompts/index.d.ts.map +1 -0
- package/dist/prompts/index.js +186 -0
- package/dist/prompts/index.js.map +1 -0
- package/dist/resources/index.d.ts +75 -0
- package/dist/resources/index.d.ts.map +1 -0
- package/dist/resources/index.js +218 -0
- package/dist/resources/index.js.map +1 -0
- package/dist/test/setup.d.ts +78 -0
- package/dist/test/setup.d.ts.map +1 -0
- package/dist/test/setup.js +138 -0
- package/dist/test/setup.js.map +1 -0
- package/dist/tools/implementations/advanced-social-tools.d.ts +250 -0
- package/dist/tools/implementations/advanced-social-tools.d.ts.map +1 -0
- package/dist/tools/implementations/advanced-social-tools.js +380 -0
- package/dist/tools/implementations/advanced-social-tools.js.map +1 -0
- package/dist/tools/implementations/base-tool.d.ts +73 -0
- package/dist/tools/implementations/base-tool.d.ts.map +1 -0
- package/dist/tools/implementations/base-tool.js +225 -0
- package/dist/tools/implementations/base-tool.js.map +1 -0
- package/dist/tools/implementations/content-management-tools.d.ts +81 -0
- package/dist/tools/implementations/content-management-tools.d.ts.map +1 -0
- package/dist/tools/implementations/content-management-tools.js +236 -0
- package/dist/tools/implementations/content-management-tools.js.map +1 -0
- package/dist/tools/implementations/create-post-tool.d.ts +131 -0
- package/dist/tools/implementations/create-post-tool.d.ts.map +1 -0
- package/dist/tools/implementations/create-post-tool.js +182 -0
- package/dist/tools/implementations/create-post-tool.js.map +1 -0
- package/dist/tools/implementations/follow-user-tool.d.ts +69 -0
- package/dist/tools/implementations/follow-user-tool.d.ts.map +1 -0
- package/dist/tools/implementations/follow-user-tool.js +200 -0
- package/dist/tools/implementations/follow-user-tool.js.map +1 -0
- package/dist/tools/implementations/get-user-profile-tool.d.ts +61 -0
- package/dist/tools/implementations/get-user-profile-tool.d.ts.map +1 -0
- package/dist/tools/implementations/get-user-profile-tool.js +139 -0
- package/dist/tools/implementations/get-user-profile-tool.js.map +1 -0
- package/dist/tools/implementations/index.d.ts +22 -0
- package/dist/tools/implementations/index.d.ts.map +1 -0
- package/dist/tools/implementations/index.js +32 -0
- package/dist/tools/implementations/index.js.map +1 -0
- package/dist/tools/implementations/like-post-tool.d.ts +68 -0
- package/dist/tools/implementations/like-post-tool.d.ts.map +1 -0
- package/dist/tools/implementations/like-post-tool.js +184 -0
- package/dist/tools/implementations/like-post-tool.js.map +1 -0
- package/dist/tools/implementations/media-tools.d.ts +360 -0
- package/dist/tools/implementations/media-tools.d.ts.map +1 -0
- package/dist/tools/implementations/media-tools.js +444 -0
- package/dist/tools/implementations/media-tools.js.map +1 -0
- package/dist/tools/implementations/moderation-tools.d.ts +189 -0
- package/dist/tools/implementations/moderation-tools.d.ts.map +1 -0
- package/dist/tools/implementations/moderation-tools.js +289 -0
- package/dist/tools/implementations/moderation-tools.js.map +1 -0
- package/dist/tools/implementations/oauth-tools.d.ts +108 -0
- package/dist/tools/implementations/oauth-tools.d.ts.map +1 -0
- package/dist/tools/implementations/oauth-tools.js +183 -0
- package/dist/tools/implementations/oauth-tools.js.map +1 -0
- package/dist/tools/implementations/reply-to-post-tool.d.ts +52 -0
- package/dist/tools/implementations/reply-to-post-tool.d.ts.map +1 -0
- package/dist/tools/implementations/reply-to-post-tool.js +167 -0
- package/dist/tools/implementations/reply-to-post-tool.js.map +1 -0
- package/dist/tools/implementations/repost-tool.d.ts +76 -0
- package/dist/tools/implementations/repost-tool.d.ts.map +1 -0
- package/dist/tools/implementations/repost-tool.js +181 -0
- package/dist/tools/implementations/repost-tool.js.map +1 -0
- package/dist/tools/implementations/search-posts-tool.d.ts +95 -0
- package/dist/tools/implementations/search-posts-tool.d.ts.map +1 -0
- package/dist/tools/implementations/search-posts-tool.js +208 -0
- package/dist/tools/implementations/search-posts-tool.js.map +1 -0
- package/dist/tools/implementations/social-graph-tools.d.ts +119 -0
- package/dist/tools/implementations/social-graph-tools.d.ts.map +1 -0
- package/dist/tools/implementations/social-graph-tools.js +262 -0
- package/dist/tools/implementations/social-graph-tools.js.map +1 -0
- package/dist/tools/implementations/streaming-tools.d.ts +130 -0
- package/dist/tools/implementations/streaming-tools.d.ts.map +1 -0
- package/dist/tools/implementations/streaming-tools.js +241 -0
- package/dist/tools/implementations/streaming-tools.js.map +1 -0
- package/dist/tools/implementations/timeline-tools.d.ts +69 -0
- package/dist/tools/implementations/timeline-tools.d.ts.map +1 -0
- package/dist/tools/implementations/timeline-tools.js +194 -0
- package/dist/tools/implementations/timeline-tools.js.map +1 -0
- package/dist/tools/index.d.ts +23 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +73 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/types/index.d.ts +242 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +73 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/atp-client.d.ts +109 -0
- package/dist/utils/atp-client.d.ts.map +1 -0
- package/dist/utils/atp-client.js +451 -0
- package/dist/utils/atp-client.js.map +1 -0
- package/dist/utils/config.d.ts +74 -0
- package/dist/utils/config.d.ts.map +1 -0
- package/dist/utils/config.js +311 -0
- package/dist/utils/config.js.map +1 -0
- package/dist/utils/firehose-client.d.ts +96 -0
- package/dist/utils/firehose-client.d.ts.map +1 -0
- package/dist/utils/firehose-client.js +252 -0
- package/dist/utils/firehose-client.js.map +1 -0
- package/dist/utils/logger.d.ts +74 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +142 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/oauth-client.d.ts +61 -0
- package/dist/utils/oauth-client.d.ts.map +1 -0
- package/dist/utils/oauth-client.js +224 -0
- package/dist/utils/oauth-client.js.map +1 -0
- package/dist/utils/performance.d.ts +102 -0
- package/dist/utils/performance.d.ts.map +1 -0
- package/dist/utils/performance.js +302 -0
- package/dist/utils/performance.js.map +1 -0
- package/dist/utils/security.d.ts +154 -0
- package/dist/utils/security.d.ts.map +1 -0
- package/dist/utils/security.js +358 -0
- package/dist/utils/security.js.map +1 -0
- package/package.json +93 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AT Protocol client wrapper with authentication and session management
|
|
3
|
+
* Supports both authenticated and unauthenticated modes
|
|
4
|
+
*/
|
|
5
|
+
import { AtpAgent } from '@atproto/api';
|
|
6
|
+
import { AtpError, AuthenticationError, RateLimitError, ValidationError, } from '../types/index.js';
|
|
7
|
+
import { Logger } from './logger.js';
|
|
8
|
+
/**
|
|
9
|
+
* AT Protocol client wrapper with comprehensive authentication and session management
|
|
10
|
+
* Supports both authenticated and unauthenticated modes for maximum flexibility
|
|
11
|
+
*/
|
|
12
|
+
export class AtpClient {
|
|
13
|
+
agent;
|
|
14
|
+
publicAgent;
|
|
15
|
+
logger;
|
|
16
|
+
session = null;
|
|
17
|
+
config;
|
|
18
|
+
sessionRefreshPromise = null;
|
|
19
|
+
isAuthenticationRequired;
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.config = config;
|
|
22
|
+
this.logger = new Logger('AtpClient');
|
|
23
|
+
// Determine if authentication is required based on available credentials
|
|
24
|
+
this.isAuthenticationRequired = this.hasAuthenticationCredentials();
|
|
25
|
+
// Create authenticated agent (for user's PDS)
|
|
26
|
+
this.agent = new AtpAgent({
|
|
27
|
+
service: config.service,
|
|
28
|
+
persistSession: (evt, sess) => {
|
|
29
|
+
this.handleSessionEvent(evt, sess);
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
// Create public agent for unauthenticated requests
|
|
33
|
+
this.publicAgent = new AtpAgent({
|
|
34
|
+
service: 'https://public.api.bsky.app',
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check if authentication credentials are available
|
|
39
|
+
*/
|
|
40
|
+
hasAuthenticationCredentials() {
|
|
41
|
+
if (this.config.authMethod === 'app-password') {
|
|
42
|
+
return !!(this.config.identifier && this.config.password);
|
|
43
|
+
}
|
|
44
|
+
else if (this.config.authMethod === 'oauth') {
|
|
45
|
+
return !!(this.config.clientId && this.config.clientSecret);
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Initialize the AT Protocol client
|
|
51
|
+
* In unauthenticated mode, this will skip authentication
|
|
52
|
+
* In authenticated mode, this will perform authentication
|
|
53
|
+
*/
|
|
54
|
+
async initialize() {
|
|
55
|
+
try {
|
|
56
|
+
this.logger.info('Initializing AT Protocol client', {
|
|
57
|
+
service: this.config.service,
|
|
58
|
+
authMethod: this.config.authMethod,
|
|
59
|
+
authenticationRequired: this.isAuthenticationRequired,
|
|
60
|
+
});
|
|
61
|
+
if (this.isAuthenticationRequired) {
|
|
62
|
+
await this.authenticate();
|
|
63
|
+
this.logger.info('AT Protocol client initialized successfully with authentication');
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
this.logger.info('AT Protocol client initialized successfully in unauthenticated mode');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
this.logger.error('Failed to initialize AT Protocol client', error);
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Authenticate with AT Protocol using configured method
|
|
76
|
+
*/
|
|
77
|
+
async authenticate() {
|
|
78
|
+
if (this.config.authMethod === 'app-password') {
|
|
79
|
+
await this.authenticateWithAppPassword();
|
|
80
|
+
}
|
|
81
|
+
else if (this.config.authMethod === 'oauth') {
|
|
82
|
+
await this.authenticateWithOAuth();
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
throw new AuthenticationError(`Unsupported authentication method: ${this.config.authMethod}`, undefined, { authMethod: this.config.authMethod });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Authenticate using app password
|
|
90
|
+
*/
|
|
91
|
+
async authenticateWithAppPassword() {
|
|
92
|
+
if (!this.config.identifier || !this.config.password) {
|
|
93
|
+
throw new AuthenticationError('App password authentication requires identifier and password', undefined, { authMethod: 'app-password' });
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
this.logger.debug('Authenticating with app password');
|
|
97
|
+
const response = await this.agent.login({
|
|
98
|
+
identifier: this.config.identifier,
|
|
99
|
+
password: this.config.password,
|
|
100
|
+
});
|
|
101
|
+
if (!response.success) {
|
|
102
|
+
throw new AuthenticationError('App password authentication failed', response, {
|
|
103
|
+
identifier: this.config.identifier,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
this.logger.info('App password authentication successful', {
|
|
107
|
+
did: response.data.did,
|
|
108
|
+
handle: response.data.handle,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
if (error instanceof AuthenticationError) {
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
this.logger.error('App password authentication error', error);
|
|
116
|
+
throw new AuthenticationError('App password authentication failed', error, {
|
|
117
|
+
identifier: this.config.identifier,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Authenticate using OAuth
|
|
123
|
+
*/
|
|
124
|
+
async authenticateWithOAuth() {
|
|
125
|
+
if (!this.config.clientId || !this.config.clientSecret) {
|
|
126
|
+
throw new AuthenticationError('OAuth authentication requires clientId and clientSecret', undefined, { authMethod: 'oauth' });
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
this.logger.debug('Authenticating with OAuth');
|
|
130
|
+
const { AtpOAuthClient } = await import('./oauth-client.js');
|
|
131
|
+
const oauthClient = new AtpOAuthClient(this.config);
|
|
132
|
+
// For server environments, we need a different flow
|
|
133
|
+
// This implementation supports both interactive and programmatic flows
|
|
134
|
+
const identifier = this.config.identifier;
|
|
135
|
+
if (!identifier) {
|
|
136
|
+
throw new AuthenticationError('OAuth requires an identifier (handle or DID)', undefined, {
|
|
137
|
+
authMethod: 'oauth',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
// Check if we have stored OAuth tokens
|
|
141
|
+
const storedSession = await this.loadStoredOAuthSession();
|
|
142
|
+
if (storedSession && storedSession.expiresAt > new Date()) {
|
|
143
|
+
// Use stored session
|
|
144
|
+
this.logger.info('Using stored OAuth session', {
|
|
145
|
+
did: storedSession.did,
|
|
146
|
+
handle: storedSession.handle,
|
|
147
|
+
});
|
|
148
|
+
await this.agent.resumeSession({
|
|
149
|
+
accessJwt: storedSession.accessToken,
|
|
150
|
+
refreshJwt: storedSession.refreshToken,
|
|
151
|
+
did: storedSession.did,
|
|
152
|
+
handle: storedSession.handle,
|
|
153
|
+
active: true,
|
|
154
|
+
});
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
// Check if we have a refresh token to use
|
|
158
|
+
if (storedSession?.refreshToken) {
|
|
159
|
+
try {
|
|
160
|
+
this.logger.info('Refreshing OAuth tokens');
|
|
161
|
+
const newSession = await oauthClient.refreshTokens(storedSession.refreshToken);
|
|
162
|
+
await this.storeOAuthSession(newSession);
|
|
163
|
+
await this.agent.resumeSession({
|
|
164
|
+
accessJwt: newSession.accessToken,
|
|
165
|
+
refreshJwt: newSession.refreshToken,
|
|
166
|
+
did: newSession.did,
|
|
167
|
+
handle: newSession.handle,
|
|
168
|
+
active: true,
|
|
169
|
+
});
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
catch (refreshError) {
|
|
173
|
+
this.logger.warn('OAuth token refresh failed, starting new flow', refreshError);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// Start new OAuth flow
|
|
177
|
+
const authRequest = await oauthClient.startAuthorization(identifier);
|
|
178
|
+
this.logger.info('OAuth authorization required', {
|
|
179
|
+
authUrl: authRequest.authUrl,
|
|
180
|
+
state: `${authRequest.state.substring(0, 8)}...`,
|
|
181
|
+
});
|
|
182
|
+
// In a real implementation, you would:
|
|
183
|
+
// 1. Open the authUrl in a browser or redirect user
|
|
184
|
+
// 2. Handle the callback with the authorization code
|
|
185
|
+
// 3. Complete the OAuth flow
|
|
186
|
+
// For now, we'll throw an informative error with the auth URL
|
|
187
|
+
throw new AuthenticationError(`OAuth flow requires user interaction. Please visit: ${authRequest.authUrl}`, undefined, {
|
|
188
|
+
authMethod: 'oauth',
|
|
189
|
+
authUrl: authRequest.authUrl,
|
|
190
|
+
state: authRequest.state,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
if (error instanceof AuthenticationError) {
|
|
195
|
+
throw error;
|
|
196
|
+
}
|
|
197
|
+
this.logger.error('OAuth authentication error', error);
|
|
198
|
+
throw new AuthenticationError('OAuth authentication failed', error, { authMethod: 'oauth' });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Handle session events from AtpAgent
|
|
203
|
+
*/
|
|
204
|
+
handleSessionEvent(event, sessionData) {
|
|
205
|
+
this.logger.debug('Session event received', { event, hasSessionData: !!sessionData });
|
|
206
|
+
switch (event) {
|
|
207
|
+
case 'create':
|
|
208
|
+
case 'update':
|
|
209
|
+
if (sessionData) {
|
|
210
|
+
this.session = {
|
|
211
|
+
did: sessionData.did,
|
|
212
|
+
handle: sessionData.handle,
|
|
213
|
+
accessJwt: sessionData.accessJwt,
|
|
214
|
+
refreshJwt: sessionData.refreshJwt,
|
|
215
|
+
active: sessionData.active ?? true,
|
|
216
|
+
};
|
|
217
|
+
this.logger.debug('Session updated', {
|
|
218
|
+
did: this.session.did,
|
|
219
|
+
handle: this.session.handle,
|
|
220
|
+
active: this.session.active,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
break;
|
|
224
|
+
case 'expired':
|
|
225
|
+
this.logger.warn('Session expired, attempting refresh');
|
|
226
|
+
this.refreshSession().catch(error => {
|
|
227
|
+
this.logger.error('Failed to refresh expired session', error);
|
|
228
|
+
});
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Refresh the current session
|
|
234
|
+
*/
|
|
235
|
+
async refreshSession() {
|
|
236
|
+
if (this.sessionRefreshPromise) {
|
|
237
|
+
return this.sessionRefreshPromise;
|
|
238
|
+
}
|
|
239
|
+
this.sessionRefreshPromise = this.performSessionRefresh();
|
|
240
|
+
try {
|
|
241
|
+
await this.sessionRefreshPromise;
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
this.sessionRefreshPromise = null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Perform the actual session refresh
|
|
249
|
+
*/
|
|
250
|
+
async performSessionRefresh() {
|
|
251
|
+
try {
|
|
252
|
+
this.logger.debug('Refreshing session');
|
|
253
|
+
// Note: refreshSession method may not exist in current @atproto/api version
|
|
254
|
+
// This is a placeholder for when the method becomes available
|
|
255
|
+
if ('refreshSession' in this.agent && typeof this.agent.refreshSession === 'function') {
|
|
256
|
+
const response = await this.agent.refreshSession();
|
|
257
|
+
if (!response.success) {
|
|
258
|
+
throw new AuthenticationError('Session refresh failed', response, {
|
|
259
|
+
sessionActive: this.session?.active,
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
this.logger.info('Session refreshed successfully');
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
// Fallback: re-authenticate if refresh is not available
|
|
266
|
+
this.logger.info('Session refresh not available, re-authenticating');
|
|
267
|
+
await this.authenticate();
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
catch (error) {
|
|
271
|
+
this.logger.error('Session refresh failed', error);
|
|
272
|
+
// If refresh fails, try to re-authenticate
|
|
273
|
+
if (this.config.authMethod === 'app-password') {
|
|
274
|
+
this.logger.info('Attempting re-authentication after refresh failure');
|
|
275
|
+
await this.authenticateWithAppPassword();
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
throw new AuthenticationError('Session refresh failed and re-authentication not available', error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Get the current session
|
|
284
|
+
*/
|
|
285
|
+
getSession() {
|
|
286
|
+
return this.session;
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Check if client is authenticated
|
|
290
|
+
*/
|
|
291
|
+
isAuthenticated() {
|
|
292
|
+
return this.session?.active === true;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Check if authentication is required for this client instance
|
|
296
|
+
*/
|
|
297
|
+
requiresAuthentication() {
|
|
298
|
+
return this.isAuthenticationRequired;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Get the underlying AtpAgent instance
|
|
302
|
+
* Returns the authenticated agent if available, otherwise the public agent
|
|
303
|
+
*/
|
|
304
|
+
getAgent() {
|
|
305
|
+
return this.isAuthenticated() ? this.agent : this.publicAgent;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Get the authenticated agent (for operations that require authentication)
|
|
309
|
+
*/
|
|
310
|
+
getAuthenticatedAgent() {
|
|
311
|
+
if (!this.isAuthenticated()) {
|
|
312
|
+
throw new AuthenticationError('Authentication required for this operation', undefined, {
|
|
313
|
+
operation: 'getAuthenticatedAgent',
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return this.agent;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Get the public agent (for operations that don't require authentication)
|
|
320
|
+
*/
|
|
321
|
+
getPublicAgent() {
|
|
322
|
+
return this.publicAgent;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Execute a request with automatic retry and error handling
|
|
326
|
+
* Supports both authenticated and unauthenticated operations
|
|
327
|
+
*/
|
|
328
|
+
async executeRequest(operation, context, requiresAuth = false) {
|
|
329
|
+
try {
|
|
330
|
+
// Only authenticate if required and authentication is available
|
|
331
|
+
if (requiresAuth) {
|
|
332
|
+
if (!this.isAuthenticationRequired) {
|
|
333
|
+
return {
|
|
334
|
+
success: false,
|
|
335
|
+
error: new AuthenticationError('This operation requires authentication, but no credentials were provided', undefined, { ...context, operation: 'executeRequest', requiresAuth }),
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
if (!this.isAuthenticated()) {
|
|
339
|
+
await this.authenticate();
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const result = await operation();
|
|
343
|
+
return { success: true, data: result };
|
|
344
|
+
}
|
|
345
|
+
catch (error) {
|
|
346
|
+
return { success: false, error: this.handleError(error, context) };
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Execute a public request that doesn't require authentication
|
|
351
|
+
*/
|
|
352
|
+
async executePublicRequest(operation, context) {
|
|
353
|
+
try {
|
|
354
|
+
const result = await operation();
|
|
355
|
+
return { success: true, data: result };
|
|
356
|
+
}
|
|
357
|
+
catch (error) {
|
|
358
|
+
return { success: false, error: this.handleError(error, context) };
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Execute an authenticated request that requires authentication
|
|
363
|
+
*/
|
|
364
|
+
async executeAuthenticatedRequest(operation, context) {
|
|
365
|
+
return this.executeRequest(operation, context, true);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Handle and transform errors from AT Protocol operations
|
|
369
|
+
*/
|
|
370
|
+
handleError(error, context) {
|
|
371
|
+
this.logger.error('AT Protocol operation failed', error, context);
|
|
372
|
+
if (error instanceof AtpError) {
|
|
373
|
+
return error;
|
|
374
|
+
}
|
|
375
|
+
// Handle common AT Protocol errors
|
|
376
|
+
if (typeof error === 'object' && error !== null) {
|
|
377
|
+
const errorObj = error;
|
|
378
|
+
if (errorObj.status === 401) {
|
|
379
|
+
return new AuthenticationError('Authentication required or invalid', errorObj, context);
|
|
380
|
+
}
|
|
381
|
+
if (errorObj.status === 429) {
|
|
382
|
+
const retryAfter = errorObj.headers?.['retry-after'];
|
|
383
|
+
return new RateLimitError('Rate limit exceeded', retryAfter ? parseInt(retryAfter, 10) : undefined, context);
|
|
384
|
+
}
|
|
385
|
+
if (errorObj.status >= 400 && errorObj.status < 500) {
|
|
386
|
+
return new ValidationError(errorObj.message || 'Client error', undefined, errorObj, context);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return new AtpError(error instanceof Error ? error.message : 'Unknown AT Protocol error', 'UNKNOWN_ERROR', undefined, error, context);
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Cleanup resources
|
|
393
|
+
*/
|
|
394
|
+
async cleanup() {
|
|
395
|
+
this.logger.info('Cleaning up AT Protocol client');
|
|
396
|
+
try {
|
|
397
|
+
// Cancel any pending session refresh
|
|
398
|
+
if (this.sessionRefreshPromise) {
|
|
399
|
+
await this.sessionRefreshPromise;
|
|
400
|
+
}
|
|
401
|
+
this.session = null;
|
|
402
|
+
this.logger.info('AT Protocol client cleanup completed');
|
|
403
|
+
}
|
|
404
|
+
catch (error) {
|
|
405
|
+
this.logger.error('Error during AT Protocol client cleanup', error);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
/**
|
|
409
|
+
* Load stored OAuth session from secure storage
|
|
410
|
+
*/
|
|
411
|
+
async loadStoredOAuthSession() {
|
|
412
|
+
try {
|
|
413
|
+
// In a real implementation, this would load from secure storage
|
|
414
|
+
// For now, we'll use environment variables or return null
|
|
415
|
+
const storedSession = process.env['OAUTH_STORED_SESSION'];
|
|
416
|
+
if (storedSession) {
|
|
417
|
+
const session = JSON.parse(storedSession);
|
|
418
|
+
return {
|
|
419
|
+
...session,
|
|
420
|
+
expiresAt: new Date(session.expiresAt),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
this.logger.warn('Failed to load stored OAuth session', error);
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Store OAuth session to secure storage
|
|
432
|
+
*/
|
|
433
|
+
async storeOAuthSession(session) {
|
|
434
|
+
try {
|
|
435
|
+
// In a real implementation, this would store to secure storage
|
|
436
|
+
// For now, we'll log the session (without sensitive data)
|
|
437
|
+
this.logger.info('OAuth session stored', {
|
|
438
|
+
did: session.did,
|
|
439
|
+
handle: session.handle,
|
|
440
|
+
expiresAt: session.expiresAt.toISOString(),
|
|
441
|
+
});
|
|
442
|
+
// Store in environment variable for demo purposes
|
|
443
|
+
// In production, use proper secure storage
|
|
444
|
+
process.env['OAUTH_STORED_SESSION'] = JSON.stringify(session);
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
this.logger.warn('Failed to store OAuth session', error);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
//# sourceMappingURL=atp-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"atp-client.js","sourceRoot":"","sources":["../../src/utils/atp-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAA6C,MAAM,cAAc,CAAC;AACnF,OAAO,EACL,QAAQ,EACR,mBAAmB,EAInB,cAAc,EAEd,eAAe,GAChB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC;;;GAGG;AACH,MAAM,OAAO,SAAS;IACZ,KAAK,CAAW;IAChB,WAAW,CAAW;IACtB,MAAM,CAAS;IACf,OAAO,GAAuB,IAAI,CAAC;IACnC,MAAM,CAAa;IACnB,qBAAqB,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OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QAChE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,+BAA+B,EAAE,KAAK,CAAC,CAAC;QAC3D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration management system for the AT Protocol MCP Server
|
|
3
|
+
* Handles environment variables, command-line arguments, and default settings
|
|
4
|
+
*/
|
|
5
|
+
import { type IAtpConfig, type IMcpServerConfig } from '../types/index.js';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration manager class
|
|
8
|
+
*/
|
|
9
|
+
export declare class ConfigManager {
|
|
10
|
+
private config;
|
|
11
|
+
constructor(overrides?: Partial<IMcpServerConfig>);
|
|
12
|
+
/**
|
|
13
|
+
* Build configuration from defaults, environment variables, and overrides
|
|
14
|
+
*/
|
|
15
|
+
private buildConfig;
|
|
16
|
+
/**
|
|
17
|
+
* Apply environment variables to configuration
|
|
18
|
+
*/
|
|
19
|
+
private applyEnvironmentVariables;
|
|
20
|
+
/**
|
|
21
|
+
* Parse environment variable value to appropriate type
|
|
22
|
+
*/
|
|
23
|
+
private parseEnvValue;
|
|
24
|
+
/**
|
|
25
|
+
* Set nested property using dot notation
|
|
26
|
+
*/
|
|
27
|
+
private setNestedProperty;
|
|
28
|
+
/**
|
|
29
|
+
* Deep merge configuration objects
|
|
30
|
+
*/
|
|
31
|
+
private mergeConfig;
|
|
32
|
+
/**
|
|
33
|
+
* Validate configuration using Zod schema
|
|
34
|
+
*/
|
|
35
|
+
private validateConfig;
|
|
36
|
+
/**
|
|
37
|
+
* Validate authentication configuration
|
|
38
|
+
* Authentication is now optional - only validate if auth method is specified
|
|
39
|
+
*/
|
|
40
|
+
private validateAuthConfiguration;
|
|
41
|
+
/**
|
|
42
|
+
* Log configuration (excluding sensitive data)
|
|
43
|
+
*/
|
|
44
|
+
private logConfiguration;
|
|
45
|
+
/**
|
|
46
|
+
* Get the current configuration
|
|
47
|
+
*/
|
|
48
|
+
getConfig(): IMcpServerConfig;
|
|
49
|
+
/**
|
|
50
|
+
* Get AT Protocol configuration
|
|
51
|
+
*/
|
|
52
|
+
getAtpConfig(): IAtpConfig;
|
|
53
|
+
/**
|
|
54
|
+
* Update configuration at runtime
|
|
55
|
+
*/
|
|
56
|
+
updateConfig(updates: Partial<IMcpServerConfig>): void;
|
|
57
|
+
/**
|
|
58
|
+
* Check if configuration is valid for the specified auth method
|
|
59
|
+
*/
|
|
60
|
+
isValidForAuth(authMethod: 'app-password' | 'oauth'): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Check if authentication is configured
|
|
63
|
+
*/
|
|
64
|
+
hasAuthentication(): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Get authentication mode description
|
|
67
|
+
*/
|
|
68
|
+
getAuthMode(): 'unauthenticated' | 'app-password' | 'oauth';
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Create a configuration manager instance
|
|
72
|
+
*/
|
|
73
|
+
export declare function createConfig(overrides?: Partial<IMcpServerConfig>): ConfigManager;
|
|
74
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/utils/config.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAsB,KAAK,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAoG/F;;GAEG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,MAAM,CAAmB;gBAErB,SAAS,GAAE,OAAO,CAAC,gBAAgB,CAAM;IAMrD;;OAEG;IACH,OAAO,CAAC,WAAW;IAanB;;OAEG;IACH,OAAO,CAAC,yBAAyB;IAajC;;OAEG;IACH,OAAO,CAAC,aAAa;IASrB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAgBzB;;OAEG;IACH,OAAO,CAAC,WAAW;IAmBnB;;OAEG;IACH,OAAO,CAAC,cAAc;IAetB;;;OAGG;IACH,OAAO,CAAC,yBAAyB;IA4BjC;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAaxB;;OAEG;IACI,SAAS,IAAI,gBAAgB;IAIpC;;OAEG;IACI,YAAY,IAAI,UAAU;IAIjC;;OAEG;IACI,YAAY,CAAC,OAAO,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI;IAqB7D;;OAEG;IACI,cAAc,CAAC,UAAU,EAAE,cAAc,GAAG,OAAO,GAAG,OAAO;IAkBpE;;OAEG;IACI,iBAAiB,IAAI,OAAO;IAgBnC;;OAEG;IACI,WAAW,IAAI,iBAAiB,GAAG,cAAc,GAAG,OAAO;CAMnE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,SAAS,GAAE,OAAO,CAAC,gBAAgB,CAAM,GAAG,aAAa,CAErF"}
|