@multi-agent-protocol/sdk 0.0.2

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 ADDED
@@ -0,0 +1,248 @@
1
+ # @anthropic/map-sdk
2
+
3
+ TypeScript SDK for the Multi-Agent Protocol (MAP) - a JSON-RPC based protocol for observing, coordinating, and routing messages within multi-agent AI systems.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @anthropic/map-sdk
9
+ ```
10
+
11
+ ## Features
12
+
13
+ ### Connection Types
14
+
15
+ The SDK provides specialized connection classes for different participant types:
16
+
17
+ - **`ClientConnection`** - For external clients observing and interacting with agents
18
+ - **`AgentConnection`** - For agents participating in the system
19
+ - **`GatewayConnection`** - For federation between MAP systems
20
+
21
+ ```typescript
22
+ import { ClientConnection, AgentConnection, GatewayConnection } from '@anthropic/map-sdk';
23
+ ```
24
+
25
+ ### Streaming & Subscriptions
26
+
27
+ Subscribe to real-time events with full backpressure support:
28
+
29
+ ```typescript
30
+ const client = new ClientConnection(stream, { name: 'My Client' });
31
+ await client.connect();
32
+
33
+ // Subscribe to events
34
+ const subscription = await client.subscribe({
35
+ eventTypes: ['agent.registered', 'agent.state.changed'],
36
+ agents: ['agent-1', 'agent-2'],
37
+ });
38
+
39
+ // Async iteration
40
+ for await (const event of subscription) {
41
+ console.log(event.type, event.data);
42
+ }
43
+
44
+ // Pause/resume for backpressure
45
+ subscription.pause();
46
+ // ... process batch
47
+ subscription.resume();
48
+
49
+ // Acknowledge events (if server supports)
50
+ if (subscription.supportsAck) {
51
+ subscription.ack();
52
+ }
53
+ ```
54
+
55
+ ### Auto-Reconnection
56
+
57
+ Built-in reconnection with exponential backoff:
58
+
59
+ ```typescript
60
+ const client = new ClientConnection(stream, {
61
+ name: 'My Client',
62
+ reconnection: {
63
+ enabled: true,
64
+ maxRetries: 10,
65
+ baseDelayMs: 1000,
66
+ maxDelayMs: 30000,
67
+ jitter: true,
68
+ },
69
+ createStream: async () => createNewWebSocketStream(),
70
+ });
71
+
72
+ client.onReconnection((event) => {
73
+ switch (event.type) {
74
+ case 'disconnected':
75
+ console.log('Connection lost');
76
+ break;
77
+ case 'reconnecting':
78
+ console.log(`Reconnecting (attempt ${event.attempt})`);
79
+ break;
80
+ case 'reconnected':
81
+ console.log('Reconnected successfully');
82
+ break;
83
+ case 'subscriptionRestored':
84
+ console.log(`Subscription restored, replayed ${event.replayedCount} events`);
85
+ break;
86
+ }
87
+ });
88
+ ```
89
+
90
+ ### Permissions
91
+
92
+ 4-layer permission system for fine-grained access control:
93
+
94
+ ```typescript
95
+ import {
96
+ canSeeAgent,
97
+ canMessageAgent,
98
+ filterVisibleAgents,
99
+ resolveAgentPermissions
100
+ } from '@anthropic/map-sdk';
101
+
102
+ // Check if a participant can see an agent
103
+ const canSee = canSeeAgent(systemConfig, callerAgent, targetAgent);
104
+
105
+ // Filter agents by visibility
106
+ const visibleAgents = filterVisibleAgents(allAgents, permissionContext);
107
+
108
+ // Resolve effective permissions for an agent
109
+ const permissions = resolveAgentPermissions(agent, permissionConfig);
110
+ ```
111
+
112
+ ### Federation
113
+
114
+ Connect multiple MAP systems with envelope-based routing:
115
+
116
+ ```typescript
117
+ import { GatewayConnection, createFederationEnvelope } from '@anthropic/map-sdk';
118
+
119
+ const gateway = new GatewayConnection(stream, {
120
+ name: 'Federation Gateway',
121
+ routing: {
122
+ systemId: 'system-a',
123
+ maxHops: 10,
124
+ trackPath: true,
125
+ },
126
+ buffer: {
127
+ enabled: true,
128
+ maxMessages: 1000,
129
+ },
130
+ });
131
+
132
+ await gateway.connect();
133
+
134
+ // Route message to another system
135
+ const message = { id: 'msg-1', to: { agent: 'remote-agent' }, payload: { ... } };
136
+ await gateway.routeToSystem('system-b', message);
137
+ ```
138
+
139
+ ### Causal Event Ordering
140
+
141
+ Buffer and release events in causal order:
142
+
143
+ ```typescript
144
+ import { CausalEventBuffer } from '@anthropic/map-sdk';
145
+
146
+ const buffer = new CausalEventBuffer({
147
+ maxSize: 1000,
148
+ timeoutMs: 5000,
149
+ });
150
+
151
+ // Events are released only when their dependencies are satisfied
152
+ buffer.on('ready', (event) => {
153
+ processEvent(event);
154
+ });
155
+
156
+ buffer.add(event); // Buffered until causedBy dependencies are released
157
+ ```
158
+
159
+ ## Testing
160
+
161
+ The SDK includes a `TestServer` for integration testing:
162
+
163
+ ```typescript
164
+ import { TestServer } from '@anthropic/map-sdk/testing';
165
+ import { ClientConnection } from '@anthropic/map-sdk';
166
+ import { createStreamPair } from '@anthropic/map-sdk';
167
+
168
+ const server = new TestServer({ name: 'Test Server' });
169
+ const [clientStream, serverStream] = createStreamPair();
170
+
171
+ server.acceptConnection(serverStream);
172
+ const client = new ClientConnection(clientStream, { name: 'Test Client' });
173
+
174
+ await client.connect();
175
+ // ... run tests
176
+ ```
177
+
178
+ ## API Reference
179
+
180
+ ### Connection Classes
181
+
182
+ | Class | Description |
183
+ |-------|-------------|
184
+ | `BaseConnection` | Low-level JSON-RPC connection with request/response correlation |
185
+ | `ClientConnection` | Client participant with subscribe, query, and messaging methods |
186
+ | `AgentConnection` | Agent participant with registration, scope, and message handling |
187
+ | `GatewayConnection` | Federation gateway with routing and buffering |
188
+
189
+ ### Key Methods
190
+
191
+ **ClientConnection:**
192
+ - `connect()` / `disconnect()` - Lifecycle management
193
+ - `subscribe(filter?)` - Subscribe to event stream
194
+ - `replay(options)` - Replay historical events
195
+ - `send(to, payload)` - Send messages
196
+ - `listAgents()` / `getAgent(id)` - Query agents
197
+ - `listScopes()` - Query scopes
198
+
199
+ **AgentConnection:**
200
+ - `register(options)` - Register agent
201
+ - `joinScope(scopeId)` / `leaveScope(scopeId)` - Scope membership
202
+ - `onMessage(handler)` - Handle incoming messages
203
+ - `updateState(state)` - Update agent state
204
+
205
+ **Subscription:**
206
+ - `pause()` / `resume()` - Flow control
207
+ - `ack(sequenceNumber?)` - Acknowledge events
208
+ - `close()` - End subscription
209
+ - Async iterable - `for await (const event of subscription)`
210
+
211
+ ### Event Types
212
+
213
+ ```typescript
214
+ type EventType =
215
+ | 'agent.registered'
216
+ | 'agent.unregistered'
217
+ | 'agent.state.changed'
218
+ | 'scope.created'
219
+ | 'scope.joined'
220
+ | 'scope.left'
221
+ | 'message.sent'
222
+ | 'message.delivered'
223
+ | 'permissions.client.updated'
224
+ | 'permissions.agent.updated'
225
+ // ... and more
226
+ ```
227
+
228
+ ## Protocol Methods
229
+
230
+ The SDK implements the full MAP wire protocol:
231
+
232
+ | Category | Methods |
233
+ |----------|---------|
234
+ | Core | `map/connect`, `map/disconnect`, `map/send`, `map/subscribe`, `map/unsubscribe`, `map/replay` |
235
+ | Observation | `map/agents/list`, `map/agents/get`, `map/scopes/list`, `map/structure/graph` |
236
+ | Lifecycle | `map/agents/register`, `map/agents/unregister`, `map/agents/spawn` |
237
+ | State | `map/agents/update`, `map/agents/stop`, `map/agents/suspend`, `map/agents/resume` |
238
+ | Scope | `map/scopes/create`, `map/scopes/join`, `map/scopes/leave` |
239
+ | Federation | `map/federation/connect`, `map/federation/route` |
240
+
241
+ ## Requirements
242
+
243
+ - Node.js >= 18.0.0
244
+ - TypeScript >= 5.0 (for development)
245
+
246
+ ## License
247
+
248
+ MIT