@commercetools/tools-core 0.0.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/README.md +508 -0
- package/dist/decorators/test/tool.decorator.test.d.ts +1 -0
- package/dist/decorators/tool.decorator.d.ts +26 -0
- package/dist/handlers/base.handler.d.ts +34 -0
- package/dist/handlers/commercetools.handler.d.ts +16 -0
- package/dist/handlers/test/base.handler.test.d.ts +1 -0
- package/dist/handlers/test/commercetools.handler.test.d.ts +1 -0
- package/dist/handlers/test/fixtures.d.ts +24 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +1 -0
- package/dist/resources/customer/customer.handler.d.ts +22 -0
- package/dist/resources/customer/customer.prompt.d.ts +4 -0
- package/dist/resources/customer/customer.schema.d.ts +281 -0
- package/dist/resources/customer/test/customer.hander.test.d.ts +1 -0
- package/dist/resources/customer/test/fixtures.d.ts +2 -0
- package/dist/resources/project/project.handler.d.ts +26 -0
- package/dist/resources/project/project.prompt.d.ts +2 -0
- package/dist/resources/project/project.schema.d.ts +454 -0
- package/dist/resources/project/test/fixtures.d.ts +2 -0
- package/dist/resources/project/test/project.handler.test.d.ts +1 -0
- package/dist/shared/client/client.base.d.ts +9 -0
- package/dist/shared/client/sdk.commercetools.d.ts +31 -0
- package/dist/shared/errors/tool.error.d.ts +34 -0
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/types/interface.d.ts +57 -0
- package/dist/shared/types/types.d.ts +4 -0
- package/dist/utils/constants.d.ts +1 -0
- package/package.json +33 -0
package/README.md
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
# commercetools core MCP tools
|
|
2
|
+
|
|
3
|
+
This package exposes the commercetools resources in the form of MCP (Model Context Protocol) tools that can be used across multiple MCP projects.
|
|
4
|
+
|
|
5
|
+
This docs describes the detailed design decisions, steps to integrate the exposed tools into an existing project and various usage examples depending on the project design choice.
|
|
6
|
+
|
|
7
|
+
## Design Document
|
|
8
|
+
|
|
9
|
+
-- to be added --
|
|
10
|
+
|
|
11
|
+
### Design Decisions
|
|
12
|
+
|
|
13
|
+
#### 1. Layered Architecture
|
|
14
|
+
|
|
15
|
+
The package follows a layered architecture pattern with clear separation of concerns:
|
|
16
|
+
|
|
17
|
+
- **Base Layer (`BaseResourceHandler`)**: Generic, API-agnostic handler that provides tool routing, validation, and naming conventions
|
|
18
|
+
- **Platform Layer (`CommercetoolsResourceHandler`)**: commercetools-specific handler that adds API client management
|
|
19
|
+
- **Resource Layer**: Concrete implementations for specific resources (Customer, Project, etc.)
|
|
20
|
+
|
|
21
|
+
**Rationale**: This separation allows the base handler to be reused for any API platform (Stripe, Shopify, etc.), while the commercetools layer provides platform-specific functionality.
|
|
22
|
+
|
|
23
|
+
#### 2. Decorator-Based Discovery
|
|
24
|
+
|
|
25
|
+
Handlers can be automatically discovered using the `@ToolHandler()` decorator, which registers them in a global registry when the class is defined.
|
|
26
|
+
|
|
27
|
+
**Rationale**: Enables zero-configuration tool discovery, making it easy to add new tools without manual registration.
|
|
28
|
+
|
|
29
|
+
#### 3. Dependency Injection
|
|
30
|
+
|
|
31
|
+
API client factories are injectable, allowing for:
|
|
32
|
+
|
|
33
|
+
- Easy testing with mock factories
|
|
34
|
+
- Custom API client implementations
|
|
35
|
+
- Different authentication strategies
|
|
36
|
+
|
|
37
|
+
**Rationale**: Improves testability and flexibility while maintaining a clean API.
|
|
38
|
+
|
|
39
|
+
#### 4. Schema-Based Validation
|
|
40
|
+
|
|
41
|
+
All tool parameters are validated using Zod schemas defined in the handler metadata.
|
|
42
|
+
|
|
43
|
+
**Rationale**: Type-safe parameter validation with clear error messages and automatic TypeScript type inference.
|
|
44
|
+
|
|
45
|
+
#### 5. Function-Based and Class-Based Support
|
|
46
|
+
|
|
47
|
+
The architecture supports both:
|
|
48
|
+
|
|
49
|
+
- **Class-based**: Full-featured handlers with inheritance and decorators
|
|
50
|
+
- **Function-based**: Simple function wrappers for quick tool creation
|
|
51
|
+
|
|
52
|
+
**Rationale**: Provides flexibility for different use cases - complex tools benefit from classes, simple tools can use functions.
|
|
53
|
+
|
|
54
|
+
### Package Components
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
packages/core/
|
|
58
|
+
├── src/
|
|
59
|
+
│ ├── handlers/
|
|
60
|
+
│ │ ├── base.handler.ts # Generic base handler (API-agnostic)
|
|
61
|
+
│ │ └── commercetools.handler.ts # commercetools-specific handler
|
|
62
|
+
│ ├── decorators/
|
|
63
|
+
│ │ └── tool.decorator.ts # @ToolHandler() decorator
|
|
64
|
+
│ ├── resources/
|
|
65
|
+
│ │ ├── customer/ # Customer resource handler
|
|
66
|
+
│ │ └── project/ # Project resource handler
|
|
67
|
+
│ └── shared/
|
|
68
|
+
│ ├── types/ # TypeScript interfaces
|
|
69
|
+
│ ├── client/ # API client factories
|
|
70
|
+
│ └── errors/ # Error handling
|
|
71
|
+
└── index.ts # Public API exports
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Component Relationships
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
┌────────────────────────────────────────────────────────┐
|
|
78
|
+
│ ToolExecutionContext │
|
|
79
|
+
│ (authToken, configuration, toolName, parameters) │
|
|
80
|
+
└────────────────────┬───────────────────────────────────┘
|
|
81
|
+
│
|
|
82
|
+
▼
|
|
83
|
+
┌─────────────────────────────────────────────────────────┐
|
|
84
|
+
│ BaseResourceHandler<TConfig> │
|
|
85
|
+
│ • Tool routing & validation │
|
|
86
|
+
│ • Schema validation │
|
|
87
|
+
│ • Tool name generation │
|
|
88
|
+
│ • Abstract CRUD methods │
|
|
89
|
+
└────────────────────┬────────────────────────────────────┘
|
|
90
|
+
│
|
|
91
|
+
│ extends
|
|
92
|
+
▼
|
|
93
|
+
┌─────────────────────────────────────────────────────────┐
|
|
94
|
+
│ CommercetoolsResourceHandler │
|
|
95
|
+
│ • API client factory injection │
|
|
96
|
+
│ • getApiRoot() helper │
|
|
97
|
+
│ • commercetools-specific configuration │
|
|
98
|
+
└────────────────────┬────────────────────────────────────┘
|
|
99
|
+
│
|
|
100
|
+
│ extends
|
|
101
|
+
▼
|
|
102
|
+
┌─────────────────────────────────────────────────────────┐
|
|
103
|
+
│ CustomerHandler / ProjectHandler │
|
|
104
|
+
│ • Resource-specific CRUD implementation │
|
|
105
|
+
│ • Resource metadata & schemas │
|
|
106
|
+
│ • @ToolHandler() decorator │
|
|
107
|
+
└────────────────────┬────────────────────────────────────┘
|
|
108
|
+
│
|
|
109
|
+
│ registered via
|
|
110
|
+
▼
|
|
111
|
+
┌─────────────────────────────────────────────────────────┐
|
|
112
|
+
│ Decorator Registry │
|
|
113
|
+
│ • getDecoratorRegisteredHandlers() │
|
|
114
|
+
└────────────────────┬────────────────────────────────────┘
|
|
115
|
+
│
|
|
116
|
+
│ used by
|
|
117
|
+
▼
|
|
118
|
+
┌─────────────────────────────────────────────────────────┐
|
|
119
|
+
│ MCP Server │
|
|
120
|
+
│ • Tool registration │
|
|
121
|
+
│ • Request handling │
|
|
122
|
+
└─────────────────────────────────────────────────────────┘
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Usage Examples
|
|
126
|
+
|
|
127
|
+
#### 1. Custom Tools Using Base Handler (Class-Based)
|
|
128
|
+
|
|
129
|
+
Create a custom tool for any API platform:
|
|
130
|
+
|
|
131
|
+
```typescript
|
|
132
|
+
import { z } from 'zod';
|
|
133
|
+
import {
|
|
134
|
+
BaseResourceHandler,
|
|
135
|
+
type ResourceMetadata,
|
|
136
|
+
type ToolExecutionContext,
|
|
137
|
+
} from '@commercetools/tools-core';
|
|
138
|
+
|
|
139
|
+
// Define your custom configuration type
|
|
140
|
+
interface CustomApiConfig {
|
|
141
|
+
apiKey: string;
|
|
142
|
+
baseUrl: string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Create a custom handler
|
|
146
|
+
class ProductHandler extends BaseResourceHandler<CustomApiConfig> {
|
|
147
|
+
protected readonly metadata: ResourceMetadata = {
|
|
148
|
+
namespace: 'product',
|
|
149
|
+
description: 'Manage products in custom API',
|
|
150
|
+
toolNamePrefix: 'mcp_custom_',
|
|
151
|
+
schemas: {
|
|
152
|
+
read: z.object({
|
|
153
|
+
id: z.string().describe('Product ID'),
|
|
154
|
+
}),
|
|
155
|
+
create: z.object({
|
|
156
|
+
name: z.string(),
|
|
157
|
+
price: z.number(),
|
|
158
|
+
}),
|
|
159
|
+
update: z.object({
|
|
160
|
+
id: z.string(),
|
|
161
|
+
name: z.string().optional(),
|
|
162
|
+
price: z.number().optional(),
|
|
163
|
+
}),
|
|
164
|
+
delete: z.object({
|
|
165
|
+
id: z.string(),
|
|
166
|
+
}),
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
async read(context: ToolExecutionContext<CustomApiConfig>): Promise<unknown> {
|
|
171
|
+
const { id } = context.parameters as { id: string };
|
|
172
|
+
const { apiKey, baseUrl } = context.configuration;
|
|
173
|
+
|
|
174
|
+
// Make API call using your custom API client
|
|
175
|
+
const response = await fetch(`${baseUrl}/products/${id}`, {
|
|
176
|
+
headers: { Authorization: `Bearer ${apiKey}` },
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return response.json();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async create(
|
|
183
|
+
context: ToolExecutionContext<CustomApiConfig>
|
|
184
|
+
): Promise<unknown> {
|
|
185
|
+
const params = context.parameters as { name: string; price: number };
|
|
186
|
+
const { apiKey, baseUrl } = context.configuration;
|
|
187
|
+
|
|
188
|
+
const response = await fetch(`${baseUrl}/products`, {
|
|
189
|
+
method: 'POST',
|
|
190
|
+
headers: {
|
|
191
|
+
Authorization: `Bearer ${apiKey}`,
|
|
192
|
+
'Content-Type': 'application/json',
|
|
193
|
+
},
|
|
194
|
+
body: JSON.stringify(params),
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return response.json();
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async update(
|
|
201
|
+
context: ToolExecutionContext<CustomApiConfig>
|
|
202
|
+
): Promise<unknown> {
|
|
203
|
+
// Implementation similar to create
|
|
204
|
+
return {};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async delete(
|
|
208
|
+
context: ToolExecutionContext<CustomApiConfig>
|
|
209
|
+
): Promise<unknown> {
|
|
210
|
+
// Implementation
|
|
211
|
+
return {};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// other custom functions can go here
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Use in MCP server
|
|
218
|
+
import { createMCPServer } from './mcp.server';
|
|
219
|
+
|
|
220
|
+
const server = createMCPServer(
|
|
221
|
+
'auth-token',
|
|
222
|
+
{ apiKey: 'key', baseUrl: 'https://api.example.com' },
|
|
223
|
+
[new ProductHandler()]
|
|
224
|
+
);
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### 2. Custom Tools Using Base Handler (Function-Based)
|
|
228
|
+
|
|
229
|
+
For simpler tools, use a function-based approach:
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { z } from 'zod';
|
|
233
|
+
import {
|
|
234
|
+
BaseResourceHandler,
|
|
235
|
+
type ToolExecutionContext,
|
|
236
|
+
type ResourceMetadata,
|
|
237
|
+
} from '@commercetools/tools-core';
|
|
238
|
+
|
|
239
|
+
interface CustomConfig {
|
|
240
|
+
apiKey: string;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Create handler instance with function-based CRUD operations
|
|
244
|
+
function createProductHandler(): BaseResourceHandler<CustomConfig> {
|
|
245
|
+
return new (class extends BaseResourceHandler<CustomConfig> {
|
|
246
|
+
protected readonly metadata: ResourceMetadata = {
|
|
247
|
+
namespace: 'product',
|
|
248
|
+
description: 'Manage products',
|
|
249
|
+
toolNamePrefix: 'mcp_custom_',
|
|
250
|
+
schemas: {
|
|
251
|
+
read: z.object({ id: z.string() }),
|
|
252
|
+
create: z.object({ name: z.string(), price: z.number() }),
|
|
253
|
+
update: z.object({ id: z.string(), name: z.string().optional() }),
|
|
254
|
+
delete: z.object({ id: z.string() }),
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
async read(context: ToolExecutionContext<CustomConfig>) {
|
|
259
|
+
const { id } = context.parameters as { id: string };
|
|
260
|
+
// Your API call logic
|
|
261
|
+
return { id, name: 'Product' };
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
async create(context: ToolExecutionContext<CustomConfig>) {
|
|
265
|
+
return { created: true };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async update(context: ToolExecutionContext<CustomConfig>) {
|
|
269
|
+
return { updated: true };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async delete(context: ToolExecutionContext<CustomConfig>) {
|
|
273
|
+
return { deleted: true };
|
|
274
|
+
}
|
|
275
|
+
})();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Use in MCP server
|
|
279
|
+
const server = createMCPServer('auth-token', { apiKey: 'key' }, [
|
|
280
|
+
createProductHandler(),
|
|
281
|
+
]);
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
#### 3. commercetools Core Tools (Class-Based with Decorator)
|
|
285
|
+
|
|
286
|
+
Extend the commercetools handler and use the decorator:
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
import {
|
|
290
|
+
CustomerHandler as BaseCustomerHandler,
|
|
291
|
+
ToolHandler,
|
|
292
|
+
getDecoratorRegisteredHandlers,
|
|
293
|
+
} from '@commercetools/tools-core';
|
|
294
|
+
|
|
295
|
+
// Simply extend and decorate - automatically registered!
|
|
296
|
+
@ToolHandler()
|
|
297
|
+
export class CustomerHandler extends BaseCustomerHandler {
|
|
298
|
+
// Optionally override methods for custom behavior
|
|
299
|
+
override getToolDefinition(
|
|
300
|
+
operation: 'read' | 'create' | 'update' | 'delete'
|
|
301
|
+
) {
|
|
302
|
+
const base = super.getToolDefinition(operation);
|
|
303
|
+
return {
|
|
304
|
+
...base,
|
|
305
|
+
description: `Custom: ${base.description}`,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// In your MCP server setup
|
|
311
|
+
import { createMCPServer } from './mcp.server';
|
|
312
|
+
import { getDecoratorRegisteredHandlers } from '@commercetools/tools-core';
|
|
313
|
+
|
|
314
|
+
// Import handlers to trigger decorator registration
|
|
315
|
+
import './handlers/customer.handler';
|
|
316
|
+
import './handlers/project.handler';
|
|
317
|
+
|
|
318
|
+
// Get all registered handlers
|
|
319
|
+
const handlers = getDecoratorRegisteredHandlers();
|
|
320
|
+
|
|
321
|
+
const server = createMCPServer(authToken, configuration, handlers);
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
#### 4. Commercetools Core Tools (Function-Based)
|
|
325
|
+
|
|
326
|
+
Create commercetools handlers using functions:
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
import {
|
|
330
|
+
CustomerHandler,
|
|
331
|
+
ProjectHandler,
|
|
332
|
+
BaseResourceHandler,
|
|
333
|
+
type Configuration,
|
|
334
|
+
type IApiClientFactory,
|
|
335
|
+
type ToolExecutionContext,
|
|
336
|
+
} from '@commercetools/tools-core';
|
|
337
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
338
|
+
|
|
339
|
+
// Create handler instance with custom factory
|
|
340
|
+
function createCustomerHandler(
|
|
341
|
+
apiClientFactory?: IApiClientFactory<Configuration>
|
|
342
|
+
) {
|
|
343
|
+
return new CustomerHandler(apiClientFactory);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Create MCP server with function-based handlers
|
|
347
|
+
export function createMCPServer(
|
|
348
|
+
authToken: string,
|
|
349
|
+
configuration: Configuration,
|
|
350
|
+
handlers: BaseResourceHandler<Configuration>[] = []
|
|
351
|
+
): McpServer {
|
|
352
|
+
const server = new McpServer({
|
|
353
|
+
name: 'commercetools-mcp-server',
|
|
354
|
+
version: '1.0.0',
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
// Use function-created handlers
|
|
358
|
+
const customerHandler = createCustomerHandler();
|
|
359
|
+
const projectHandler = new ProjectHandler();
|
|
360
|
+
|
|
361
|
+
const allHandlers =
|
|
362
|
+
handlers.length > 0 ? handlers : [customerHandler, projectHandler];
|
|
363
|
+
|
|
364
|
+
// Register each handler's tools
|
|
365
|
+
for (const handler of allHandlers) {
|
|
366
|
+
const toolDefinitions = handler.getAllToolDefinitions();
|
|
367
|
+
|
|
368
|
+
for (const toolDef of toolDefinitions) {
|
|
369
|
+
server.registerTool(
|
|
370
|
+
toolDef.name,
|
|
371
|
+
{
|
|
372
|
+
description: toolDef.description,
|
|
373
|
+
inputSchema: toolDef.inputSchema as any,
|
|
374
|
+
},
|
|
375
|
+
async (args: any): Promise<any> => {
|
|
376
|
+
const context: ToolExecutionContext<Configuration> = {
|
|
377
|
+
authToken,
|
|
378
|
+
configuration,
|
|
379
|
+
toolName: toolDef.name,
|
|
380
|
+
parameters: args,
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
try {
|
|
384
|
+
const result = await handler.execute(context);
|
|
385
|
+
return {
|
|
386
|
+
content: [
|
|
387
|
+
{
|
|
388
|
+
type: 'text' as const,
|
|
389
|
+
text: JSON.stringify(result),
|
|
390
|
+
},
|
|
391
|
+
],
|
|
392
|
+
};
|
|
393
|
+
} catch (e) {
|
|
394
|
+
console.error(e);
|
|
395
|
+
throw e;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return server;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Usage
|
|
406
|
+
const server = createMCPServer(process.env.AUTH_TOKEN!, {
|
|
407
|
+
projectKey: 'my-project',
|
|
408
|
+
allowedTools: ['customer', 'project'],
|
|
409
|
+
});
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
#### 5. Complete MCP Server Example (Combining Both Approaches)
|
|
413
|
+
|
|
414
|
+
```typescript
|
|
415
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
416
|
+
import {
|
|
417
|
+
BaseResourceHandler,
|
|
418
|
+
CustomerHandler,
|
|
419
|
+
ProjectHandler,
|
|
420
|
+
getDecoratorRegisteredHandlers,
|
|
421
|
+
type ToolExecutionContext,
|
|
422
|
+
type Configuration,
|
|
423
|
+
} from '@commercetools/tools-core';
|
|
424
|
+
|
|
425
|
+
// Function to create MCP server with both decorator and manual handlers
|
|
426
|
+
export function createMCPServer(
|
|
427
|
+
authToken: string,
|
|
428
|
+
configuration: Configuration
|
|
429
|
+
): McpServer {
|
|
430
|
+
const server = new McpServer({
|
|
431
|
+
name: 'commercetools-mcp-server',
|
|
432
|
+
version: '1.0.0',
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
// Get decorator-registered handlers (class-based with @ToolHandler())
|
|
436
|
+
const decoratorHandlers = getDecoratorRegisteredHandlers();
|
|
437
|
+
|
|
438
|
+
// Create function-based handlers
|
|
439
|
+
const functionHandlers: BaseResourceHandler<Configuration>[] = [
|
|
440
|
+
new CustomerHandler(),
|
|
441
|
+
new ProjectHandler(),
|
|
442
|
+
];
|
|
443
|
+
|
|
444
|
+
// Combine all handlers
|
|
445
|
+
const allHandlers = [...decoratorHandlers, ...functionHandlers];
|
|
446
|
+
|
|
447
|
+
// Register tools from all handlers
|
|
448
|
+
for (const handler of allHandlers) {
|
|
449
|
+
const toolDefinitions = handler.getAllToolDefinitions();
|
|
450
|
+
|
|
451
|
+
for (const toolDef of toolDefinitions) {
|
|
452
|
+
server.registerTool(
|
|
453
|
+
toolDef.name,
|
|
454
|
+
{
|
|
455
|
+
description: toolDef.description,
|
|
456
|
+
inputSchema: toolDef.inputSchema as any,
|
|
457
|
+
},
|
|
458
|
+
async (args: any) => {
|
|
459
|
+
const context: ToolExecutionContext<Configuration> = {
|
|
460
|
+
authToken,
|
|
461
|
+
configuration,
|
|
462
|
+
toolName: toolDef.name,
|
|
463
|
+
parameters: args,
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
try {
|
|
467
|
+
const result = await handler.execute(context);
|
|
468
|
+
return {
|
|
469
|
+
content: [
|
|
470
|
+
{
|
|
471
|
+
type: 'text' as const,
|
|
472
|
+
text: JSON.stringify(result, null, 2),
|
|
473
|
+
},
|
|
474
|
+
],
|
|
475
|
+
};
|
|
476
|
+
} catch (error) {
|
|
477
|
+
return {
|
|
478
|
+
content: [
|
|
479
|
+
{
|
|
480
|
+
type: 'text' as const,
|
|
481
|
+
text: `Error: ${
|
|
482
|
+
error instanceof Error ? error.message : String(error)
|
|
483
|
+
}`,
|
|
484
|
+
},
|
|
485
|
+
],
|
|
486
|
+
isError: true,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
return server;
|
|
495
|
+
}
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
### Key Design Patterns
|
|
499
|
+
|
|
500
|
+
1. **Template Method Pattern**: `BaseResourceHandler.execute()` defines the algorithm structure, while subclasses implement specific CRUD operations.
|
|
501
|
+
|
|
502
|
+
2. **Strategy Pattern**: `IApiClientFactory` allows different API client creation strategies to be injected.
|
|
503
|
+
|
|
504
|
+
3. **Decorator Pattern**: `@ToolHandler()` decorator adds registration behavior to handler classes.
|
|
505
|
+
|
|
506
|
+
4. **Factory Pattern**: `ApiClientFactory` creates API clients based on configuration.
|
|
507
|
+
|
|
508
|
+
5. **Dependency Injection**: Handlers accept factories in constructors, enabling testability and flexibility.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type IConstructor, type Configuration } from '../shared';
|
|
2
|
+
import { type BaseResourceHandler } from '../handlers/base.handler';
|
|
3
|
+
/**
|
|
4
|
+
* Decorator to automatically register a tool handler
|
|
5
|
+
*
|
|
6
|
+
* Usage:
|
|
7
|
+
* ```typescript
|
|
8
|
+
* @ToolHandler()
|
|
9
|
+
* export class CustomerHandler extends BaseResourceHandler {
|
|
10
|
+
* // ...
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* The handler will be automatically discovered and registered
|
|
15
|
+
* when the module is loaded.
|
|
16
|
+
*/
|
|
17
|
+
export declare function ToolHandler(): <T extends IConstructor<BaseResourceHandler<Configuration>>>(constructor: T) => T;
|
|
18
|
+
/**
|
|
19
|
+
* Get all handlers registered via decorator
|
|
20
|
+
* This is called by ToolDiscovery to register all decorated handlers
|
|
21
|
+
*/
|
|
22
|
+
export declare function getDecoratorRegisteredHandlers(): BaseResourceHandler<Configuration>[];
|
|
23
|
+
/**
|
|
24
|
+
* Clear the decorator registry (mainly for testing)
|
|
25
|
+
*/
|
|
26
|
+
export declare function clearDecoratorRegistry(): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { ToolExecutionContext, ResourceMetadata, ToolHandler, ToolOperation } from '../shared';
|
|
2
|
+
/**
|
|
3
|
+
* Generic base handler - NO API client logic
|
|
4
|
+
* Only handles tool routing, validation, and naming
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class BaseResourceHandler<TConfig = unknown> implements ToolHandler<TConfig> {
|
|
7
|
+
protected abstract readonly metadata: ResourceMetadata;
|
|
8
|
+
abstract read(context: ToolExecutionContext<TConfig>): Promise<unknown>;
|
|
9
|
+
abstract create(context: ToolExecutionContext<TConfig>): Promise<unknown>;
|
|
10
|
+
abstract update(context: ToolExecutionContext<TConfig>): Promise<unknown>;
|
|
11
|
+
abstract delete(context: ToolExecutionContext<TConfig>): Promise<unknown>;
|
|
12
|
+
protected getToolName(operation: ToolOperation): string;
|
|
13
|
+
protected getOperation(toolName: string): ToolOperation | null;
|
|
14
|
+
execute(context: ToolExecutionContext<TConfig>): Promise<unknown>;
|
|
15
|
+
getAllToolNames(): string[];
|
|
16
|
+
getToolDefinition(operation: ToolOperation): {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
inputSchema: import("zod").ZodObject<Record<string, import("zod").ZodTypeAny>, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
|
|
20
|
+
[x: string]: any;
|
|
21
|
+
}, {
|
|
22
|
+
[x: string]: any;
|
|
23
|
+
}>;
|
|
24
|
+
};
|
|
25
|
+
getAllToolDefinitions(): {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
inputSchema: import("zod").ZodObject<Record<string, import("zod").ZodTypeAny>, import("zod").UnknownKeysParam, import("zod").ZodTypeAny, {
|
|
29
|
+
[x: string]: any;
|
|
30
|
+
}, {
|
|
31
|
+
[x: string]: any;
|
|
32
|
+
}>;
|
|
33
|
+
}[];
|
|
34
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ApiRoot } from '@commercetools/platform-sdk';
|
|
2
|
+
import { type Configuration, type IApiClientFactory, type ToolExecutionContext } from '../shared';
|
|
3
|
+
import { BaseResourceHandler } from './base.handler';
|
|
4
|
+
/**
|
|
5
|
+
* Commercetools-specific resource handler
|
|
6
|
+
* Extends BaseResourceHandler with commercetools API client functionality
|
|
7
|
+
* This is an abstract class - subclasses must implement the CRUD methods
|
|
8
|
+
*/
|
|
9
|
+
export declare abstract class CommercetoolsResourceHandler extends BaseResourceHandler<Configuration> {
|
|
10
|
+
protected apiClientFactory: IApiClientFactory<Configuration>;
|
|
11
|
+
constructor(apiClientFactory?: IApiClientFactory<Configuration>);
|
|
12
|
+
/**
|
|
13
|
+
* Get the API root instance for commercetools
|
|
14
|
+
*/
|
|
15
|
+
protected getApiRoot(context: ToolExecutionContext<Configuration>): ApiRoot;
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="jest" />
|
|
2
|
+
import { ApiRoot } from '@commercetools/platform-sdk';
|
|
3
|
+
import { ResourceMetadata, ToolExecutionContext } from '../../shared';
|
|
4
|
+
import { type Configuration, type IApiClientFactory } from '../../shared';
|
|
5
|
+
import { CommercetoolsResourceHandler } from '../../handlers/commercetools.handler';
|
|
6
|
+
import { BaseResourceHandler } from '../..';
|
|
7
|
+
export declare class TestHandler extends BaseResourceHandler {
|
|
8
|
+
protected readonly metadata: ResourceMetadata;
|
|
9
|
+
read(_context: ToolExecutionContext): Promise<unknown>;
|
|
10
|
+
create(_context: ToolExecutionContext): Promise<unknown>;
|
|
11
|
+
update(_context: ToolExecutionContext): Promise<unknown>;
|
|
12
|
+
delete(_context: ToolExecutionContext): Promise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
export declare const mockApiRoot: ApiRoot;
|
|
15
|
+
export declare class MockApiClientFactory implements IApiClientFactory<Configuration> {
|
|
16
|
+
createApiClient: jest.Mock<ApiRoot, [_config: Configuration, _token: string], any>;
|
|
17
|
+
}
|
|
18
|
+
export declare class TestCommercetoolsHandler extends CommercetoolsResourceHandler {
|
|
19
|
+
protected readonly metadata: ResourceMetadata;
|
|
20
|
+
read(context: ToolExecutionContext<Configuration>): Promise<unknown>;
|
|
21
|
+
create(_context: ToolExecutionContext<Configuration>): Promise<unknown>;
|
|
22
|
+
update(_context: ToolExecutionContext<Configuration>): Promise<unknown>;
|
|
23
|
+
delete(_context: ToolExecutionContext<Configuration>): Promise<unknown>;
|
|
24
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { BaseResourceHandler } from './handlers/base.handler';
|
|
2
|
+
export { ToolHandler, getDecoratorRegisteredHandlers, } from './decorators/tool.decorator';
|
|
3
|
+
export { CustomerHandler } from './resources/customer/customer.handler';
|
|
4
|
+
export { ProjectHandler } from './resources/project/project.handler';
|
|
5
|
+
export * from './shared';
|