@lushly-dev/afd-client 0.1.0 → 0.3.1
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 +21 -26
- package/README.md +297 -240
- package/dist/client.d.ts +30 -4
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +139 -10
- package/dist/client.js.map +1 -1
- package/dist/direct.d.ts +210 -11
- package/dist/direct.d.ts.map +1 -1
- package/dist/direct.js +439 -17
- package/dist/direct.js.map +1 -1
- package/dist/handlers.d.ts +59 -0
- package/dist/handlers.d.ts.map +1 -0
- package/dist/handlers.js +332 -0
- package/dist/handlers.js.map +1 -0
- package/dist/handoff.d.ts +335 -0
- package/dist/handoff.d.ts.map +1 -0
- package/dist/handoff.js +419 -0
- package/dist/handoff.js.map +1 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/transport.d.ts +3 -4
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +3 -4
- package/dist/transport.js.map +1 -1
- package/dist/types.d.ts +8 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +12 -3
package/LICENSE
CHANGED
|
@@ -1,26 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Lushly
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
22
|
-
|
|
23
|
-
---
|
|
24
|
-
|
|
25
|
-
NOTE: This license applies to the compiled npm packages (dist/).
|
|
26
|
-
Source code repository access is separate and not included.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lushly
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,240 +1,297 @@
|
|
|
1
|
-
# @afd
|
|
2
|
-
|
|
3
|
-
MCP client library for Agent-First Development.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install @afd
|
|
9
|
-
# or
|
|
10
|
-
pnpm add @afd
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Overview
|
|
14
|
-
|
|
15
|
-
This package provides a client for connecting to MCP (Model Context Protocol) servers. It supports:
|
|
16
|
-
|
|
17
|
-
- **SSE Transport**: Real-time streaming with Server-Sent Events
|
|
18
|
-
- **HTTP Transport**: Simple request/response communication
|
|
19
|
-
- **Direct Transport**: Zero-overhead in-process execution for co-located agents
|
|
20
|
-
- **Auto-reconnection**: Automatic reconnection with exponential backoff
|
|
21
|
-
- **Type-safe API**: Full TypeScript support with CommandResult integration
|
|
22
|
-
|
|
23
|
-
## Usage
|
|
24
|
-
|
|
25
|
-
### Basic Connection
|
|
26
|
-
|
|
27
|
-
```typescript
|
|
28
|
-
import { createClient } from '@afd
|
|
29
|
-
|
|
30
|
-
// Create client
|
|
31
|
-
const client = createClient({
|
|
32
|
-
url: 'http://localhost:3100/sse',
|
|
33
|
-
transport: 'sse', // or 'http'
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
// Connect
|
|
37
|
-
await client.connect();
|
|
38
|
-
|
|
39
|
-
// List available tools
|
|
40
|
-
const tools = await client.listTools();
|
|
41
|
-
console.log('Available tools:', tools.map(t => t.name));
|
|
42
|
-
|
|
43
|
-
// Disconnect when done
|
|
44
|
-
await client.disconnect();
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### Calling Commands
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
import { createClient, type CommandResult } from '@afd
|
|
51
|
-
import { isSuccess, isFailure } from '@afd
|
|
52
|
-
|
|
53
|
-
const client = createClient({ url: 'http://localhost:3100/sse' });
|
|
54
|
-
await client.connect();
|
|
55
|
-
|
|
56
|
-
// Call a command - returns CommandResult
|
|
57
|
-
const result = await client.call<Document>('document
|
|
58
|
-
title: 'My Document',
|
|
59
|
-
content: 'Hello, world!'
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (isSuccess(result)) {
|
|
63
|
-
console.log('Created document:', result.data);
|
|
64
|
-
console.log('Confidence:', result.confidence);
|
|
65
|
-
console.log('Reasoning:', result.reasoning);
|
|
66
|
-
} else {
|
|
67
|
-
console.error('Error:', result.error.message);
|
|
68
|
-
console.error('Suggestion:', result.error.suggestion);
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
### Event Handling
|
|
73
|
-
|
|
74
|
-
```typescript
|
|
75
|
-
const client = createClient({
|
|
76
|
-
url: 'http://localhost:3100/sse',
|
|
77
|
-
autoReconnect: true,
|
|
78
|
-
maxReconnectAttempts: 5,
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// Subscribe to events
|
|
82
|
-
client.on('connected', (info) => {
|
|
83
|
-
console.log('Connected to:', info.serverInfo.name);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
client.on('disconnected', (reason) => {
|
|
87
|
-
console.log('Disconnected:', reason);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
client.on('reconnecting', (attempt, max) => {
|
|
91
|
-
console.log(`Reconnecting... (${attempt}/${max})`);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
client.on('error', (error) => {
|
|
95
|
-
console.error('Client error:', error);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
client.on('toolsChanged', (tools) => {
|
|
99
|
-
console.log('Tools updated:', tools.length);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
await client.connect();
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### Client Status
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
const status = client.getStatus();
|
|
109
|
-
|
|
110
|
-
console.log('State:', status.state);
|
|
111
|
-
console.log('Server:', status.serverInfo?.name);
|
|
112
|
-
console.log('Connected at:', status.connectedAt);
|
|
113
|
-
console.log('Pending requests:', status.pendingRequests);
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
### Raw Tool Calls
|
|
117
|
-
|
|
118
|
-
For low-level access, use `callTool` which returns the raw MCP response:
|
|
119
|
-
|
|
120
|
-
```typescript
|
|
121
|
-
// Raw MCP response
|
|
122
|
-
const rawResult = await client.callTool('document
|
|
123
|
-
title: 'Test'
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
console.log('Content:', rawResult.content);
|
|
127
|
-
console.log('Is error:', rawResult.isError);
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
## Configuration
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
interface McpClientConfig {
|
|
134
|
-
// Required
|
|
135
|
-
url: string; // Server URL
|
|
136
|
-
|
|
137
|
-
// Optional
|
|
138
|
-
transport?: 'sse' | 'http'; // Default: 'sse'
|
|
139
|
-
clientName?: string; // Default: '@afd
|
|
140
|
-
clientVersion?: string; // Default: '0.1.0'
|
|
141
|
-
timeout?: number; // Default: 30000 (30s)
|
|
142
|
-
autoReconnect?: boolean; // Default: true
|
|
143
|
-
maxReconnectAttempts?: number; // Default: 5
|
|
144
|
-
reconnectDelay?: number; // Default: 1000 (1s)
|
|
145
|
-
headers?: Record<string, string>;
|
|
146
|
-
debug?: boolean; // Default: false
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
## Transports
|
|
151
|
-
|
|
152
|
-
### SSE Transport (Default)
|
|
153
|
-
|
|
154
|
-
Best for real-time applications. Maintains a persistent connection for server-pushed events.
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
const client = createClient({
|
|
158
|
-
url: 'http://localhost:3100/sse',
|
|
159
|
-
transport: 'sse',
|
|
160
|
-
});
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
### HTTP Transport
|
|
164
|
-
|
|
165
|
-
Simple request/response. Good for serverless environments or when SSE isn't supported.
|
|
166
|
-
|
|
167
|
-
```typescript
|
|
168
|
-
const client = createClient({
|
|
169
|
-
url: 'http://localhost:3100/message',
|
|
170
|
-
transport: 'http',
|
|
171
|
-
});
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### Direct Transport (Zero Overhead)
|
|
175
|
-
|
|
176
|
-
For co-located agents (same runtime as the application), use `
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
import {
|
|
180
|
-
import { registry } from '@my-app/commands';
|
|
181
|
-
|
|
182
|
-
// Direct execution - ~0.
|
|
183
|
-
const client =
|
|
184
|
-
|
|
185
|
-
const result = await client.call<Todo>('todo-create', { title: 'Fast!' });
|
|
186
|
-
if (result.success) {
|
|
187
|
-
console.log('Created:', result.data.id);
|
|
188
|
-
}
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
**Performance comparison:**
|
|
192
|
-
|
|
193
|
-
| Transport | Avg Latency | Use Case |
|
|
194
|
-
|-----------|-------------|----------|
|
|
195
|
-
| Direct | ~0.
|
|
196
|
-
|
|
|
197
|
-
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
```typescript
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
1
|
+
# @lushly-dev/afd-client
|
|
2
|
+
|
|
3
|
+
MCP client library for Agent-First Development.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @lushly-dev/afd-client
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @lushly-dev/afd-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Overview
|
|
14
|
+
|
|
15
|
+
This package provides a client for connecting to MCP (Model Context Protocol) servers. It supports:
|
|
16
|
+
|
|
17
|
+
- **SSE Transport**: Real-time streaming with Server-Sent Events
|
|
18
|
+
- **HTTP Transport**: Simple request/response communication
|
|
19
|
+
- **Direct Transport**: Zero-overhead in-process execution for co-located agents
|
|
20
|
+
- **Auto-reconnection**: Automatic reconnection with exponential backoff
|
|
21
|
+
- **Type-safe API**: Full TypeScript support with CommandResult integration
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Basic Connection
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { createClient } from '@lushly-dev/afd-client';
|
|
29
|
+
|
|
30
|
+
// Create client
|
|
31
|
+
const client = createClient({
|
|
32
|
+
url: 'http://localhost:3100/sse',
|
|
33
|
+
transport: 'sse', // or 'http'
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Connect
|
|
37
|
+
await client.connect();
|
|
38
|
+
|
|
39
|
+
// List available tools
|
|
40
|
+
const tools = await client.listTools();
|
|
41
|
+
console.log('Available tools:', tools.map(t => t.name));
|
|
42
|
+
|
|
43
|
+
// Disconnect when done
|
|
44
|
+
await client.disconnect();
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Calling Commands
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import { createClient, type CommandResult } from '@lushly-dev/afd-client';
|
|
51
|
+
import { isSuccess, isFailure } from '@lushly-dev/afd-core';
|
|
52
|
+
|
|
53
|
+
const client = createClient({ url: 'http://localhost:3100/sse' });
|
|
54
|
+
await client.connect();
|
|
55
|
+
|
|
56
|
+
// Call a command - returns CommandResult
|
|
57
|
+
const result = await client.call<Document>('document-create', {
|
|
58
|
+
title: 'My Document',
|
|
59
|
+
content: 'Hello, world!'
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
if (isSuccess(result)) {
|
|
63
|
+
console.log('Created document:', result.data);
|
|
64
|
+
console.log('Confidence:', result.confidence);
|
|
65
|
+
console.log('Reasoning:', result.reasoning);
|
|
66
|
+
} else {
|
|
67
|
+
console.error('Error:', result.error.message);
|
|
68
|
+
console.error('Suggestion:', result.error.suggestion);
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Event Handling
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const client = createClient({
|
|
76
|
+
url: 'http://localhost:3100/sse',
|
|
77
|
+
autoReconnect: true,
|
|
78
|
+
maxReconnectAttempts: 5,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Subscribe to events
|
|
82
|
+
client.on('connected', (info) => {
|
|
83
|
+
console.log('Connected to:', info.serverInfo.name);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
client.on('disconnected', (reason) => {
|
|
87
|
+
console.log('Disconnected:', reason);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
client.on('reconnecting', (attempt, max) => {
|
|
91
|
+
console.log(`Reconnecting... (${attempt}/${max})`);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
client.on('error', (error) => {
|
|
95
|
+
console.error('Client error:', error);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
client.on('toolsChanged', (tools) => {
|
|
99
|
+
console.log('Tools updated:', tools.length);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
await client.connect();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Client Status
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const status = client.getStatus();
|
|
109
|
+
|
|
110
|
+
console.log('State:', status.state);
|
|
111
|
+
console.log('Server:', status.serverInfo?.name);
|
|
112
|
+
console.log('Connected at:', status.connectedAt);
|
|
113
|
+
console.log('Pending requests:', status.pendingRequests);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Raw Tool Calls
|
|
117
|
+
|
|
118
|
+
For low-level access, use `callTool` which returns the raw MCP response:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// Raw MCP response
|
|
122
|
+
const rawResult = await client.callTool('document-create', {
|
|
123
|
+
title: 'Test'
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
console.log('Content:', rawResult.content);
|
|
127
|
+
console.log('Is error:', rawResult.isError);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Configuration
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
interface McpClientConfig {
|
|
134
|
+
// Required
|
|
135
|
+
url: string; // Server URL
|
|
136
|
+
|
|
137
|
+
// Optional
|
|
138
|
+
transport?: 'sse' | 'http'; // Default: 'sse'
|
|
139
|
+
clientName?: string; // Default: '@lushly-dev/afd-client'
|
|
140
|
+
clientVersion?: string; // Default: '0.1.0'
|
|
141
|
+
timeout?: number; // Default: 30000 (30s)
|
|
142
|
+
autoReconnect?: boolean; // Default: true
|
|
143
|
+
maxReconnectAttempts?: number; // Default: 5
|
|
144
|
+
reconnectDelay?: number; // Default: 1000 (1s)
|
|
145
|
+
headers?: Record<string, string>;
|
|
146
|
+
debug?: boolean; // Default: false
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Transports
|
|
151
|
+
|
|
152
|
+
### SSE Transport (Default)
|
|
153
|
+
|
|
154
|
+
Best for real-time applications. Maintains a persistent connection for server-pushed events.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
const client = createClient({
|
|
158
|
+
url: 'http://localhost:3100/sse',
|
|
159
|
+
transport: 'sse',
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### HTTP Transport
|
|
164
|
+
|
|
165
|
+
Simple request/response. Good for serverless environments or when SSE isn't supported.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
const client = createClient({
|
|
169
|
+
url: 'http://localhost:3100/message',
|
|
170
|
+
transport: 'http',
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Direct Transport (Zero Overhead)
|
|
175
|
+
|
|
176
|
+
For co-located agents (same runtime as the application), use `createDirectClient` to bypass all transport overhead:
|
|
177
|
+
|
|
178
|
+
```typescript
|
|
179
|
+
import { createDirectClient } from '@lushly-dev/afd-client';
|
|
180
|
+
import { registry } from '@my-app/commands';
|
|
181
|
+
|
|
182
|
+
// Direct execution - ~0.03-0.1ms latency vs 2-10ms for MCP
|
|
183
|
+
const client = createDirectClient(registry);
|
|
184
|
+
|
|
185
|
+
const result = await client.call<Todo>('todo-create', { title: 'Fast!' });
|
|
186
|
+
if (result.success) {
|
|
187
|
+
console.log('Created:', result.data.id);
|
|
188
|
+
}
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**Performance comparison:**
|
|
192
|
+
|
|
193
|
+
| Transport | Avg Latency | Use Case |
|
|
194
|
+
|-----------|-------------|----------|
|
|
195
|
+
| Direct | ~0.03-0.1ms | Same runtime, max performance |
|
|
196
|
+
| MCP HTTP | ~2-5ms | External services |
|
|
197
|
+
| MCP SSE | ~5-10ms | Remote agents, real-time |
|
|
198
|
+
|
|
199
|
+
#### DirectClient Options
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
const client = createDirectClient(registry, {
|
|
203
|
+
source: 'my-agent', // Identifier propagated to handlers
|
|
204
|
+
debug: true, // Enable debug logging
|
|
205
|
+
validateInputs: true, // Validate inputs against schemas (default: true)
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### Context Propagation
|
|
210
|
+
|
|
211
|
+
Pass context to individual calls for tracing and cancellation:
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
// Custom trace ID
|
|
215
|
+
const result = await client.call('command', args, {
|
|
216
|
+
traceId: 'custom-trace-123',
|
|
217
|
+
timeout: 5000,
|
|
218
|
+
signal: abortController.signal,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Context is propagated to command handlers
|
|
222
|
+
// Handler receives: { traceId, source, timeout, signal, ... }
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
#### Input Validation
|
|
226
|
+
|
|
227
|
+
If your registry implements `getCommand()`, DirectClient validates inputs:
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
class MyRegistry implements DirectRegistry {
|
|
231
|
+
// ... other methods ...
|
|
232
|
+
|
|
233
|
+
getCommand(name: string): CommandDefinition | undefined {
|
|
234
|
+
return this.commands.get(name);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const client = createDirectClient(registry, { validateInputs: true });
|
|
239
|
+
|
|
240
|
+
// Missing required parameter
|
|
241
|
+
const result = await client.call('todo-create', {});
|
|
242
|
+
// Result: { success: false, error: { code: 'VALIDATION_ERROR', ... } }
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
#### DirectRegistry Interface
|
|
246
|
+
|
|
247
|
+
Your registry must implement:
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
import { DirectRegistry, CommandDefinition } from '@lushly-dev/afd-client';
|
|
251
|
+
import type { CommandResult, CommandContext } from '@lushly-dev/afd-core';
|
|
252
|
+
|
|
253
|
+
class MyRegistry implements DirectRegistry {
|
|
254
|
+
// Required methods
|
|
255
|
+
async execute<T>(
|
|
256
|
+
name: string,
|
|
257
|
+
input?: unknown,
|
|
258
|
+
context?: CommandContext // Receives traceId, source, etc.
|
|
259
|
+
): Promise<CommandResult<T>>;
|
|
260
|
+
|
|
261
|
+
listCommandNames(): string[];
|
|
262
|
+
listCommands(): Array<{ name: string; description: string }>;
|
|
263
|
+
hasCommand(name: string): boolean;
|
|
264
|
+
|
|
265
|
+
// Optional - enables input validation
|
|
266
|
+
getCommand?(name: string): CommandDefinition | undefined;
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Error Handling
|
|
271
|
+
|
|
272
|
+
The client wraps all errors in the standard `CommandResult` format:
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
const result = await client.call('document-get', { id: 'not-found' });
|
|
276
|
+
|
|
277
|
+
if (isFailure(result)) {
|
|
278
|
+
// Error contains:
|
|
279
|
+
// - code: Machine-readable error code
|
|
280
|
+
// - message: Human-readable message
|
|
281
|
+
// - suggestion: What the user can do
|
|
282
|
+
// - retryable: Whether retrying might help
|
|
283
|
+
console.error(`[${result.error.code}] ${result.error.message}`);
|
|
284
|
+
|
|
285
|
+
if (result.error.suggestion) {
|
|
286
|
+
console.log('Suggestion:', result.error.suggestion);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (result.error.retryable) {
|
|
290
|
+
// Consider retrying
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
MIT
|
package/dist/client.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview MCP Client implementation
|
|
3
3
|
*/
|
|
4
|
-
import type { BatchCommand, BatchOptions, BatchResult, CommandResult, McpInitializeResult, McpTool, McpToolCallResult, StreamCallbacks, StreamChunk, StreamOptions } from '@lushly-dev/afd-core';
|
|
4
|
+
import type { BatchCommand, BatchOptions, BatchResult, CommandResult, McpInitializeResult, McpTool, McpToolCallResult, PipelineOptions, PipelineRequest, PipelineResult, PipelineStep, StreamCallbacks, StreamChunk, StreamOptions } from '@lushly-dev/afd-core';
|
|
5
5
|
import type { ClientStatus, McpClientConfig, McpClientEvents } from './types.js';
|
|
6
6
|
/**
|
|
7
7
|
* Type-safe event emitter mixin.
|
|
@@ -96,9 +96,9 @@ export declare class McpClient {
|
|
|
96
96
|
* @example
|
|
97
97
|
* ```typescript
|
|
98
98
|
* const result = await client.batch([
|
|
99
|
-
* { name: 'todo
|
|
100
|
-
* { name: 'todo
|
|
101
|
-
* { name: 'todo
|
|
99
|
+
* { name: 'todo-create', input: { title: 'First' } },
|
|
100
|
+
* { name: 'todo-create', input: { title: 'Second' } },
|
|
101
|
+
* { name: 'todo-list', input: {} }
|
|
102
102
|
* ], { stopOnError: false });
|
|
103
103
|
*
|
|
104
104
|
* console.log(`${result.summary.successCount}/${result.summary.total} succeeded`);
|
|
@@ -106,6 +106,32 @@ export declare class McpClient {
|
|
|
106
106
|
* ```
|
|
107
107
|
*/
|
|
108
108
|
batch<T = unknown>(commands: BatchCommand[], options?: BatchOptions): Promise<BatchResult<T>>;
|
|
109
|
+
/**
|
|
110
|
+
* Execute a pipeline of chained commands.
|
|
111
|
+
*
|
|
112
|
+
* Pipelines enable declarative composition of commands where the output
|
|
113
|
+
* of one becomes the input of the next via variable resolution.
|
|
114
|
+
*
|
|
115
|
+
* @param stepsOrRequest - Array of pipeline steps or full PipelineRequest
|
|
116
|
+
* @param options - Optional pipeline options
|
|
117
|
+
* @returns Pipeline result with aggregated metadata
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const result = await client.pipe([
|
|
122
|
+
* { command: 'user-get', input: { id: 123 }, as: 'user' },
|
|
123
|
+
* { command: 'order-list', input: { userId: '$prev.id' } },
|
|
124
|
+
* { command: 'order-summarize', input: {
|
|
125
|
+
* orders: '$prev',
|
|
126
|
+
* userName: '$steps.user.name'
|
|
127
|
+
* }}
|
|
128
|
+
* ]);
|
|
129
|
+
*
|
|
130
|
+
* console.log(`Confidence: ${(result.metadata.confidence * 100).toFixed(1)}%`);
|
|
131
|
+
* console.log(`Completed: ${result.metadata.completedSteps}/${result.metadata.totalSteps}`);
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
pipe<T = unknown>(stepsOrRequest: PipelineStep[] | PipelineRequest, options?: PipelineOptions): Promise<PipelineResult<T>>;
|
|
109
135
|
/**
|
|
110
136
|
* Stream command execution results with real-time progress.
|
|
111
137
|
*
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,aAAa,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACX,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,aAAa,EACb,mBAAmB,EAEnB,OAAO,EACP,iBAAiB,EAEjB,eAAe,EACf,eAAe,EACf,cAAc,EACd,YAAY,EACZ,eAAe,EACf,WAAW,EACX,aAAa,EACb,MAAM,sBAAsB,CAAC;AAa9B,OAAO,KAAK,EACX,YAAY,EAEZ,eAAe,EACf,eAAe,EAEf,MAAM,YAAY,CAAC;AAEpB;;GAEG;AACH,KAAK,YAAY,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,KAAK,EAAE,KAAK,IAAI,IAAI,CAAC,CAAC;AAW5D;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,SAAS;IACrB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiB;IACxC,OAAO,CAAC,SAAS,CAA0B;IAC3C,OAAO,CAAC,KAAK,CAAmC;IAChD,OAAO,CAAC,UAAU,CAAkD;IACpE,OAAO,CAAC,YAAY,CAAoD;IACxE,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,WAAW,CAAqB;IAC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