@jaypie/mcp 0.2.3 → 0.2.5

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.
@@ -1,17 +1,26 @@
1
1
  ---
2
- description: Complete guide to using Jaypie Vocabulary for type coercion, service handlers, and Commander CLI integration
2
+ description: Core guide to Jaypie Vocabulary for type coercion, service handlers, and entity types
3
3
  ---
4
4
 
5
5
  # Jaypie Vocabulary Package
6
6
 
7
- Jaypie Vocabulary (`@jaypie/vocabulary`) provides type coercion utilities and service handler patterns for consistent input handling across Jaypie applications. It includes adapters for integrating service handlers with Commander.js CLIs.
7
+ Jaypie Vocabulary (`@jaypie/vocabulary`) provides type coercion utilities and service handler patterns for consistent input handling across Jaypie applications.
8
+
9
+ ## Related Adapter Guides
10
+
11
+ Vocabulary includes adapters for integrating service handlers with various platforms. See these guides for platform-specific integration:
12
+
13
+ | Guide | Import | Description |
14
+ |-------|--------|-------------|
15
+ | [Jaypie_Vocabulary_Commander.md](Jaypie_Vocabulary_Commander.md) | `@jaypie/vocabulary/commander` | Commander.js CLI integration with callbacks |
16
+ | [Jaypie_Vocabulary_Lambda.md](Jaypie_Vocabulary_Lambda.md) | `@jaypie/vocabulary/lambda` | AWS Lambda handler wrapping |
17
+ | [Jaypie_Vocabulary_LLM.md](Jaypie_Vocabulary_LLM.md) | `@jaypie/vocabulary/llm` | LLM tool creation for `@jaypie/llm` Toolkit |
18
+ | [Jaypie_Vocabulary_MCP.md](Jaypie_Vocabulary_MCP.md) | `@jaypie/vocabulary/mcp` | MCP server tool registration |
8
19
 
9
20
  ## Installation
10
21
 
11
22
  ```bash
12
23
  npm install @jaypie/vocabulary
13
- # For CLI integration:
14
- npm install commander
15
24
  ```
16
25
 
17
26
  ## Core Concepts
@@ -104,6 +113,46 @@ await validateUser({ age: "25", email: "bob@example.com" });
104
113
  // → { age: 25, email: "bob@example.com", role: "user" }
105
114
  ```
106
115
 
116
+ ### ServiceContext
117
+
118
+ Services receive an optional second parameter with context utilities:
119
+
120
+ ```typescript
121
+ interface ServiceContext {
122
+ onError?: (error: unknown) => void | Promise<void>;
123
+ onFatal?: (error: unknown) => void | Promise<void>;
124
+ sendMessage?: (message: Message) => void | Promise<void>;
125
+ }
126
+
127
+ const handler = serviceHandler({
128
+ input: { jobId: { type: String } },
129
+ service: async ({ jobId }, context) => {
130
+ context?.sendMessage?.({ content: `Starting job ${jobId}` });
131
+
132
+ // Handle recoverable errors without throwing
133
+ try {
134
+ await riskyOperation();
135
+ } catch (err) {
136
+ context?.onError?.(err); // Reports error but continues
137
+ }
138
+
139
+ // Or report fatal errors explicitly
140
+ if (criticalFailure) {
141
+ context?.onFatal?.(new Error("Cannot continue"));
142
+ }
143
+
144
+ return { jobId, status: "complete" };
145
+ },
146
+ });
147
+ ```
148
+
149
+ Context callbacks connect to adapter registration:
150
+ - `sendMessage` → `onMessage` callback
151
+ - `onError` → `onError` callback (recoverable errors)
152
+ - `onFatal` → `onFatal` callback (fatal errors)
153
+
154
+ **Note:** Any error that escapes the service (is thrown) is treated as fatal and routes to `onFatal`.
155
+
107
156
  ## Type Coercion
108
157
 
109
158
  ### Supported Types
@@ -187,185 +236,90 @@ input: {
187
236
  }
188
237
  ```
189
238
 
190
- ## Commander.js Integration
191
-
192
- ### registerServiceCommand (Recommended)
239
+ ## Entity Types
193
240
 
194
- The simplest way to register a service handler as a Commander command:
241
+ The vocabulary provides standard entity types for consistent data modeling:
195
242
 
196
243
  ```typescript
197
- import { Command } from "commander";
198
- import { serviceHandler } from "@jaypie/vocabulary";
199
- import { registerServiceCommand } from "@jaypie/vocabulary/commander";
200
-
201
- const handler = serviceHandler({
202
- alias: "greet",
203
- description: "Greet a user",
204
- input: {
205
- userName: { type: String, flag: "user", letter: "u" },
206
- loud: { type: Boolean, letter: "l", default: false },
207
- },
208
- service: ({ loud, userName }) => {
209
- const greeting = `Hello, ${userName}!`;
210
- console.log(loud ? greeting.toUpperCase() : greeting);
211
- },
212
- });
213
-
214
- const program = new Command();
215
- registerServiceCommand({ handler, program });
216
- program.parse();
217
- // Usage: greet --user Alice -l
218
- // Output: HELLO, ALICE!
244
+ import type {
245
+ BaseEntity,
246
+ BaseEntityInput,
247
+ BaseEntityUpdate,
248
+ BaseEntityFilter,
249
+ HistoryEntry,
250
+ Job,
251
+ MessageEntity,
252
+ Progress,
253
+ } from "@jaypie/vocabulary";
219
254
  ```
220
255
 
221
- ### registerServiceCommand Options
222
-
223
- | Option | Type | Description |
224
- |--------|------|-------------|
225
- | `handler` | `ServiceHandlerFunction` | Required. The service handler |
226
- | `program` | `Command` | Required. Commander program or command |
227
- | `name` | `string` | Override command name (default: handler.alias) |
228
- | `description` | `string` | Override description (default: handler.description) |
229
- | `exclude` | `string[]` | Field names to exclude from options |
230
- | `overrides` | `Record<string, override>` | Per-field option overrides |
256
+ ### BaseEntity (base for all entities)
231
257
 
232
- ### Input Flag and Letter Properties
233
-
234
- Define CLI flags directly in input definitions:
235
-
236
- ```typescript
237
- input: {
238
- userName: {
239
- type: String,
240
- flag: "user", // Long flag: --user (instead of --user-name)
241
- letter: "u", // Short flag: -u
242
- description: "User name to greet",
243
- },
244
- verbose: {
245
- type: Boolean,
246
- letter: "v", // -v
247
- },
248
- }
249
- // Generates: --user <userName>, -u and --verbose, -v
250
258
  ```
251
-
252
- ### Manual Integration
253
-
254
- For more control, use `createCommanderOptions` and `parseCommanderOptions`:
255
-
256
- ```typescript
257
- import { Command } from "commander";
258
- import { serviceHandler } from "@jaypie/vocabulary";
259
- import {
260
- createCommanderOptions,
261
- parseCommanderOptions,
262
- } from "@jaypie/vocabulary/commander";
263
-
264
- const handler = serviceHandler({
265
- input: {
266
- userName: { type: String, description: "User name" },
267
- maxRetries: { type: Number, default: 3 },
268
- verbose: { type: Boolean },
269
- },
270
- service: (input) => console.log(input),
271
- });
272
-
273
- const program = new Command();
274
-
275
- // Create options from handler input
276
- const { options } = createCommanderOptions(handler.input, {
277
- exclude: ["internalField"],
278
- overrides: {
279
- userName: { short: "u", description: "Override description" },
280
- },
281
- });
282
- options.forEach((opt) => program.addOption(opt));
283
-
284
- // Wire up action
285
- program.action(async (opts) => {
286
- const input = parseCommanderOptions(opts, { input: handler.input });
287
- await handler(input);
288
- });
289
-
290
- program.parse();
259
+ model: <varies>
260
+ id: String (auto)
261
+ createdAt: Date (auto)
262
+ updatedAt: Date (auto)
263
+ history?: [HistoryEntry] (auto)
264
+ name?: String
265
+ label?: String
266
+ abbreviation?: String
267
+ alias?: String
268
+ xid?: String
269
+ description?: String
270
+ class?: String
271
+ type?: String
272
+ content?: String
273
+ metadata?: Object
274
+ emoji?: String
275
+ icon?: String
276
+ archivedAt?: Date
277
+ deletedAt?: Date
291
278
  ```
292
279
 
293
- ### Boolean Flag Behavior
294
-
295
- Commander.js automatically handles boolean flags:
296
- - `--verbose` sets to `true`
297
- - `--no-verbose` sets to `false` (for flags with `default: true`)
298
-
299
- ## Complete CLI Example
300
-
301
- ```typescript
302
- // src/commands/evaluate.ts
303
- import { Command } from "commander";
304
- import { serviceHandler } from "@jaypie/vocabulary";
305
- import { registerServiceCommand } from "@jaypie/vocabulary/commander";
306
-
307
- export const evaluateHandler = serviceHandler({
308
- alias: "evaluate",
309
- description: "Run an evaluation job",
310
- input: {
311
- jobId: {
312
- type: String,
313
- flag: "job",
314
- letter: "j",
315
- description: "Job identifier",
316
- },
317
- priority: {
318
- type: [1, 2, 3, 4, 5],
319
- default: 3,
320
- letter: "p",
321
- description: "Job priority (1-5)",
322
- },
323
- tags: {
324
- type: [String],
325
- letter: "t",
326
- required: false,
327
- description: "Tags to apply",
328
- },
329
- dryRun: {
330
- type: Boolean,
331
- letter: "d",
332
- default: false,
333
- description: "Run without executing",
334
- },
335
- },
336
- service: async ({ dryRun, jobId, priority, tags }) => {
337
- console.log(`Running job ${jobId} with priority ${priority}`);
338
- if (tags?.length) console.log(`Tags: ${tags.join(", ")}`);
339
- if (dryRun) {
340
- console.log("(dry run - no changes made)");
341
- return;
342
- }
343
- // Execute job...
344
- },
345
- });
280
+ ### MessageEntity (extends BaseEntity)
346
281
 
347
- export function evaluateCommand(program: Command): Command {
348
- registerServiceCommand({ handler: evaluateHandler, program });
349
- return program;
350
- }
282
+ ```
283
+ model: message
284
+ content: String (required)
285
+ type?: String (e.g., "assistant", "user", "system")
351
286
  ```
352
287
 
353
- Usage:
354
- ```bash
355
- # Required job, default priority
356
- cli evaluate --job abc123
357
-
358
- # With all options
359
- cli evaluate -j abc123 -p 1 -t urgent -t critical --dry-run
288
+ ### Job (extends BaseEntity)
360
289
 
361
- # Help
362
- cli evaluate --help
290
+ ```
291
+ model: job
292
+ type?: String (e.g., "batch", "realtime", "scheduled")
293
+ class?: String (e.g., "evaluation", "export", "import")
294
+ status: String (required)
295
+ startedAt?: Date
296
+ completedAt?: Date
297
+ messages?: [MessageEntity]
298
+ progress?:
299
+ elapsedTime?: Number
300
+ estimatedTime?: Number
301
+ percentageComplete?: Number
302
+ nextPercentageCheckpoint?: Number
363
303
  ```
364
304
 
365
305
  ## TypeScript Types
366
306
 
367
307
  ```typescript
368
308
  import type {
309
+ // Entity types
310
+ BaseEntity,
311
+ BaseEntityFilter,
312
+ BaseEntityInput,
313
+ BaseEntityUpdate,
314
+ HistoryEntry,
315
+ Job,
316
+ MessageEntity,
317
+ Progress,
318
+
319
+ // Message types
320
+ Message,
321
+ MessageLevel,
322
+
369
323
  // Coercion types
370
324
  CoercionType,
371
325
  ScalarType,
@@ -378,21 +332,25 @@ import type {
378
332
 
379
333
  // Service handler types
380
334
  InputFieldDefinition,
381
- ValidateFunction,
335
+ ServiceContext,
382
336
  ServiceFunction,
383
337
  ServiceHandlerConfig,
384
338
  ServiceHandlerFunction,
339
+ ValidateFunction,
385
340
  } from "@jaypie/vocabulary";
341
+ ```
386
342
 
387
- import type {
388
- // Commander adapter types
389
- CommanderOptionOverride,
390
- CreateCommanderOptionsConfig,
391
- CreateCommanderOptionsResult,
392
- ParseCommanderOptionsConfig,
393
- RegisterServiceCommandConfig,
394
- RegisterServiceCommandResult,
395
- } from "@jaypie/vocabulary/commander";
343
+ ### Message Type
344
+
345
+ Standard message structure for callbacks and notifications:
346
+
347
+ ```typescript
348
+ type MessageLevel = "trace" | "debug" | "info" | "warn" | "error";
349
+
350
+ interface Message {
351
+ content: string;
352
+ level?: MessageLevel; // Defaults to "info" if not specified
353
+ }
396
354
  ```
397
355
 
398
356
  ## Exports
@@ -415,21 +373,19 @@ export {
415
373
  // Service Handler
416
374
  export { serviceHandler } from "./serviceHandler.js";
417
375
 
418
- // Commander namespace
376
+ // Adapter namespaces
419
377
  export * as commander from "./commander/index.js";
378
+ export * as lambda from "./lambda/index.js";
379
+ export * as llm from "./llm/index.js";
380
+ export * as mcp from "./mcp/index.js";
381
+
382
+ // Types (including Message vocabulary and ServiceContext)
383
+ export type { Message, MessageLevel, ServiceContext } from "./types.js";
420
384
 
421
385
  // Version
422
386
  export const VOCABULARY_VERSION: string;
423
387
  ```
424
388
 
425
- ### Commander Export (`@jaypie/vocabulary/commander`)
426
-
427
- ```typescript
428
- export { createCommanderOptions } from "./createCommanderOptions.js";
429
- export { parseCommanderOptions } from "./parseCommanderOptions.js";
430
- export { registerServiceCommand } from "./registerServiceCommand.js";
431
- ```
432
-
433
389
  ## Error Handling
434
390
 
435
391
  Invalid coercions throw `BadRequestError` from `@jaypie/errors`:
@@ -448,4 +404,6 @@ await handler({}); // Missing required field
448
404
  Vocabulary is designed to be consumed by:
449
405
  - **`@jaypie/lambda`** - Lambda handler input processing
450
406
  - **`@jaypie/express`** - Express route input validation
407
+ - **`@jaypie/llm`** - LLM tool parameter coercion via the llm adapter
408
+ - **`@jaypie/mcp`** - MCP server tool registration via the mcp adapter
451
409
  - **CLI packages** - Commander.js integration via the commander adapter