| 34 | } |
| 35 | |
| 36 | export function generateToolSchema(cmd: Command): Record<string, unknown> { |
| 37 | const toolName = `mmx_${cmd.name.replace(/ /g, '_')}`; |
| 38 | |
| 39 | const schema: Record<string, unknown> = { |
| 40 | name: toolName, |
| 41 | description: cmd.description, |
| 42 | input_schema: { |
| 43 | type: 'object', |
| 44 | properties: {} as Record<string, unknown>, |
| 45 | required: [] as string[], |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | if (cmd.options) { |
| 50 | for (const opt of cmd.options) { |
| 51 | const { name, inferredType, isArray } = parseFlag(opt.flag); |
| 52 | if (!name) continue; |
| 53 | |
| 54 | // Explicit type from OptionDef takes precedence; fall back to inference |
| 55 | const explicitType = opt.type; |
| 56 | const effectiveType = isArray |
| 57 | ? 'array' |
| 58 | : (explicitType ?? inferredType); |
| 59 | |
| 60 | const propSchema: Record<string, unknown> = { description: opt.description }; |
| 61 | |
| 62 | if (effectiveType === 'array') { |
| 63 | propSchema.type = 'array'; |
| 64 | propSchema.items = { type: 'string' }; |
| 65 | } else { |
| 66 | propSchema.type = effectiveType; |
| 67 | } |
| 68 | |
| 69 | const inputSchema = schema.input_schema as Record<string, unknown>; |
| 70 | (inputSchema.properties as Record<string, unknown>)[name] = propSchema; |
| 71 | |
| 72 | if (opt.required) { |
| 73 | (inputSchema.required as string[]).push(name); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | return schema; |
| 79 | } |