@multi-agent-protocol/sdk 0.0.4 → 0.0.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/README.md +164 -5
- package/dist/{index-Z76qC_Us.d.cts → index-BQXp4_rd.d.cts} +1898 -142
- package/dist/{index-Z76qC_Us.d.ts → index-BQXp4_rd.d.ts} +1898 -142
- package/dist/index.cjs +2906 -1104
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +641 -24
- package/dist/index.d.ts +641 -24
- package/dist/index.js +2889 -1102
- package/dist/index.js.map +1 -1
- package/dist/testing.cjs +1092 -8
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +2 -1
- package/dist/testing.d.ts +2 -1
- package/dist/testing.js +1092 -8
- package/dist/testing.js.map +1 -1
- package/package.json +8 -1
package/README.md
CHANGED
|
@@ -33,7 +33,7 @@ await client.connect();
|
|
|
33
33
|
// Subscribe to events
|
|
34
34
|
const subscription = await client.subscribe({
|
|
35
35
|
eventTypes: ['agent.registered', 'agent.state.changed'],
|
|
36
|
-
|
|
36
|
+
fromAgents: ['agent-1', 'agent-2'],
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
// Async iteration
|
|
@@ -156,6 +156,140 @@ buffer.on('ready', (event) => {
|
|
|
156
156
|
buffer.add(event); // Buffered until causedBy dependencies are released
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
+
## Server SDK
|
|
160
|
+
|
|
161
|
+
Build MAP servers using the `MAPServer` class and building blocks.
|
|
162
|
+
|
|
163
|
+
### Basic Server Setup
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
import { MAPServer } from '@multi-agent-protocol/sdk/server';
|
|
167
|
+
import { WebSocketServer } from 'ws';
|
|
168
|
+
|
|
169
|
+
// Create a MAP server
|
|
170
|
+
const server = new MAPServer({
|
|
171
|
+
name: 'My MAP Server',
|
|
172
|
+
version: '1.0.0',
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// Accept WebSocket connections
|
|
176
|
+
const wss = new WebSocketServer({ port: 8080 });
|
|
177
|
+
wss.on('connection', (ws) => {
|
|
178
|
+
const router = server.accept(ws, { role: 'client' });
|
|
179
|
+
router.start();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
console.log('MAP server listening on ws://localhost:8080');
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Server with Authentication
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
import {
|
|
189
|
+
MAPServer,
|
|
190
|
+
createSimpleAPIKeyAuthenticator,
|
|
191
|
+
JWTAuthenticator,
|
|
192
|
+
NoAuthAuthenticator,
|
|
193
|
+
} from '@multi-agent-protocol/sdk/server';
|
|
194
|
+
|
|
195
|
+
const server = new MAPServer({
|
|
196
|
+
name: 'Authenticated Server',
|
|
197
|
+
auth: {
|
|
198
|
+
required: true,
|
|
199
|
+
authenticators: [
|
|
200
|
+
// API key authentication
|
|
201
|
+
createSimpleAPIKeyAuthenticator({
|
|
202
|
+
'api-key-1': 'user-1',
|
|
203
|
+
'api-key-2': 'user-2',
|
|
204
|
+
}),
|
|
205
|
+
|
|
206
|
+
// JWT authentication
|
|
207
|
+
new JWTAuthenticator({
|
|
208
|
+
jwksUrl: 'https://auth.example.com/.well-known/jwks.json',
|
|
209
|
+
issuer: 'https://auth.example.com',
|
|
210
|
+
audience: 'my-api',
|
|
211
|
+
}),
|
|
212
|
+
|
|
213
|
+
// Allow unauthenticated for local stdio connections
|
|
214
|
+
new NoAuthAuthenticator({ defaultPrincipalId: 'local-agent' }),
|
|
215
|
+
],
|
|
216
|
+
// Bypass auth for trusted transports
|
|
217
|
+
bypassForTransports: { stdio: true },
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Custom Handlers
|
|
223
|
+
|
|
224
|
+
Extend the server with application-specific handlers:
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { MAPServer } from '@multi-agent-protocol/sdk/server';
|
|
228
|
+
|
|
229
|
+
const server = new MAPServer({ name: 'Custom Server' });
|
|
230
|
+
|
|
231
|
+
// Add custom handlers alongside built-in ones
|
|
232
|
+
const customHandlers = {
|
|
233
|
+
'myapp/echo': async (params: unknown) => {
|
|
234
|
+
return { echo: params };
|
|
235
|
+
},
|
|
236
|
+
|
|
237
|
+
'myapp/getStatus': async (params: unknown, ctx) => {
|
|
238
|
+
// Access session context
|
|
239
|
+
return {
|
|
240
|
+
sessionId: ctx.session.id,
|
|
241
|
+
principal: ctx.session.principal,
|
|
242
|
+
};
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// Merge with server handlers when accepting connections
|
|
247
|
+
wss.on('connection', (ws) => {
|
|
248
|
+
const router = server.accept(ws, {
|
|
249
|
+
role: 'client',
|
|
250
|
+
additionalHandlers: customHandlers,
|
|
251
|
+
});
|
|
252
|
+
router.start();
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### Building Blocks
|
|
257
|
+
|
|
258
|
+
For advanced customization, use individual building blocks:
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import {
|
|
262
|
+
AgentRegistryImpl,
|
|
263
|
+
ScopeManagerImpl,
|
|
264
|
+
SessionManagerImpl,
|
|
265
|
+
SubscriptionManagerImpl,
|
|
266
|
+
MessageRouterImpl,
|
|
267
|
+
InMemoryAgentStore,
|
|
268
|
+
InMemorySessionStore,
|
|
269
|
+
InMemorySubscriptionStore,
|
|
270
|
+
InMemoryScopeStore,
|
|
271
|
+
createAgentHandlers,
|
|
272
|
+
createSubscriptionHandlers,
|
|
273
|
+
createMessageHandlers,
|
|
274
|
+
} from '@multi-agent-protocol/sdk/server';
|
|
275
|
+
|
|
276
|
+
// Create stores (swap with database-backed stores in production)
|
|
277
|
+
const agents = new AgentRegistryImpl({ store: new InMemoryAgentStore() });
|
|
278
|
+
const sessions = new SessionManagerImpl({ store: new InMemorySessionStore() });
|
|
279
|
+
const scopes = new ScopeManagerImpl({ store: new InMemoryScopeStore() });
|
|
280
|
+
const subscriptions = new SubscriptionManagerImpl({
|
|
281
|
+
store: new InMemorySubscriptionStore(),
|
|
282
|
+
scopes, // For scope hierarchy filtering
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Create handlers
|
|
286
|
+
const handlers = {
|
|
287
|
+
...createAgentHandlers({ agents, sessions }),
|
|
288
|
+
...createSubscriptionHandlers({ subscriptions, sessions }),
|
|
289
|
+
...createMessageHandlers({ agents, scopes }),
|
|
290
|
+
};
|
|
291
|
+
```
|
|
292
|
+
|
|
159
293
|
## Testing
|
|
160
294
|
|
|
161
295
|
The SDK includes a `TestServer` for integration testing:
|
|
@@ -186,6 +320,24 @@ await client.connect();
|
|
|
186
320
|
| `AgentConnection` | Agent participant with registration, scope, and message handling |
|
|
187
321
|
| `GatewayConnection` | Federation gateway with routing and buffering |
|
|
188
322
|
|
|
323
|
+
### Connection States
|
|
324
|
+
|
|
325
|
+
All connections go through these lifecycle states:
|
|
326
|
+
|
|
327
|
+
| State | Description |
|
|
328
|
+
|-------|-------------|
|
|
329
|
+
| `initial` | Connection created but not yet started |
|
|
330
|
+
| `connecting` | Connect handshake in progress |
|
|
331
|
+
| `connected` | Successfully connected and ready |
|
|
332
|
+
| `reconnecting` | Disconnected, attempting to reconnect |
|
|
333
|
+
| `closed` | Connection permanently closed |
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
client.onStateChange((newState, oldState) => {
|
|
337
|
+
console.log(`State changed: ${oldState} -> ${newState}`);
|
|
338
|
+
});
|
|
339
|
+
```
|
|
340
|
+
|
|
189
341
|
### Key Methods
|
|
190
342
|
|
|
191
343
|
**ClientConnection:**
|
|
@@ -195,6 +347,9 @@ await client.connect();
|
|
|
195
347
|
- `send(to, payload)` - Send messages
|
|
196
348
|
- `listAgents()` / `getAgent(id)` - Query agents
|
|
197
349
|
- `listScopes()` - Query scopes
|
|
350
|
+
- `getResumeToken()` - Get token for session resumption
|
|
351
|
+
- `reconnect(token?)` - Reconnect using resume token
|
|
352
|
+
- `authenticate(auth)` / `refreshAuth(auth)` - Authentication
|
|
198
353
|
|
|
199
354
|
**AgentConnection:**
|
|
200
355
|
- `register(options)` - Register agent
|
|
@@ -204,12 +359,15 @@ await client.connect();
|
|
|
204
359
|
|
|
205
360
|
**Subscription:**
|
|
206
361
|
- `pause()` / `resume()` - Flow control
|
|
207
|
-
- `ack(sequenceNumber?)` - Acknowledge events
|
|
362
|
+
- `ack(sequenceNumber?)` - Acknowledge events (requires server support)
|
|
363
|
+
- `supportsAck` - Whether server advertises ack support
|
|
208
364
|
- `close()` - End subscription
|
|
209
365
|
- Async iterable - `for await (const event of subscription)`
|
|
210
366
|
|
|
211
367
|
### Event Types
|
|
212
368
|
|
|
369
|
+
Event types use dot notation (e.g., `agent.registered`). Subscription filters accept both dot notation and underscore notation for compatibility.
|
|
370
|
+
|
|
213
371
|
```typescript
|
|
214
372
|
type EventType =
|
|
215
373
|
| 'agent.registered'
|
|
@@ -220,9 +378,10 @@ type EventType =
|
|
|
220
378
|
| 'scope.left'
|
|
221
379
|
| 'message.sent'
|
|
222
380
|
| 'message.delivered'
|
|
223
|
-
| '
|
|
224
|
-
| '
|
|
225
|
-
|
|
381
|
+
| 'session.started'
|
|
382
|
+
| 'session.ended'
|
|
383
|
+
| 'system.error'
|
|
384
|
+
| 'system.shutdown'
|
|
226
385
|
```
|
|
227
386
|
|
|
228
387
|
## Protocol Methods
|