|
| 1 | +import type { AnyCommand, FlagDef, FlagsDef } from "bailian-cli-core"; |
| 2 | +import { z } from "zod"; |
| 3 | + |
| 4 | +/** JSON Schema object shape (used in unit tests / descriptor snapshots). */ |
| 5 | +export interface JsonSchemaObject { |
| 6 | + type: "object"; |
| 7 | + properties: Record<string, Record<string, unknown>>; |
| 8 | + required?: string[]; |
| 9 | + additionalProperties?: boolean; |
| 10 | +} |
| 11 | + |
| 12 | +/** Zod object schema for `McpServer.registerTool({ inputSchema })`. */ |
| 13 | +export function flagsToZodObject(flags: FlagsDef | undefined) { |
| 14 | + const shape: Record<string, z.ZodTypeAny> = {}; |
| 15 | + |
| 16 | + for (const key of Object.keys(flags ?? {})) { |
| 17 | + const def: FlagDef = flags![key]!; |
| 18 | + let schema: z.ZodTypeAny; |
| 19 | + |
| 20 | + if (def.type === "switch" || def.type === "boolean") { |
| 21 | + schema = z.boolean(); |
| 22 | + } else if (def.type === "number") { |
| 23 | + schema = z.number(); |
| 24 | + } else if (def.type === "array") { |
| 25 | + const item = |
| 26 | + def.choices && def.choices.length > 0 |
| 27 | + ? z.enum(def.choices as [string, ...string[]]) |
| 28 | + : z.string(); |
| 29 | + schema = z.array(item); |
| 30 | + } else if (def.choices && def.choices.length > 0) { |
| 31 | + schema = z.enum(def.choices as [string, ...string[]]); |
| 32 | + } else { |
| 33 | + schema = z.string(); |
| 34 | + } |
| 35 | + |
| 36 | + schema = schema.describe(def.description); |
| 37 | + const required = def.type !== "switch" && "required" in def && !!def.required; |
| 38 | + shape[key] = required ? schema : schema.optional(); |
| 39 | + } |
| 40 | + |
| 41 | + return z.object(shape); |
| 42 | +} |
| 43 | + |
| 44 | +/** Stable MCP tool name from a space-separated command path, e.g. `text chat` → `bailian_text_chat`. */ |
| 45 | +export function pathToToolName(path: string, prefix = "bailian"): string { |
| 46 | + const slug = path |
| 47 | + .trim() |
| 48 | + .split(/\s+/) |
| 49 | + .join("_") |
| 50 | + .replace(/[^a-zA-Z0-9_-]/g, "_"); |
| 51 | + return `${prefix}_${slug}`; |
| 52 | +} |
| 53 | + |
| 54 | +function flagToProperty(def: FlagDef): Record<string, unknown> { |
| 55 | + if (def.type === "switch") { |
| 56 | + return { type: "boolean", description: def.description }; |
| 57 | + } |
| 58 | + if (def.type === "number") { |
| 59 | + const property: Record<string, unknown> = { type: "number", description: def.description }; |
| 60 | + if (def.choices?.length) property.enum = def.choices.map((choice) => Number(choice)); |
| 61 | + return property; |
| 62 | + } |
| 63 | + if (def.type === "boolean") { |
| 64 | + return { type: "boolean", description: def.description }; |
| 65 | + } |
| 66 | + if (def.type === "array") { |
| 67 | + const items: Record<string, unknown> = { type: "string" }; |
| 68 | + if (def.choices?.length) items.enum = [...def.choices]; |
| 69 | + return { type: "array", items, description: def.description }; |
| 70 | + } |
| 71 | + const property: Record<string, unknown> = { type: "string", description: def.description }; |
| 72 | + if (def.choices?.length) property.enum = [...def.choices]; |
| 73 | + return property; |
| 74 | +} |
| 75 | + |
| 76 | +/** Build MCP `inputSchema` from a command's own flags (no global / credential flags). */ |
| 77 | +export function flagsToInputSchema(flags: FlagsDef | undefined): JsonSchemaObject { |
| 78 | + const properties: Record<string, Record<string, unknown>> = {}; |
| 79 | + const required: string[] = []; |
| 80 | + |
| 81 | + for (const [key, def] of Object.entries(flags ?? {})) { |
| 82 | + properties[key] = flagToProperty(def); |
| 83 | + if (def.type !== "switch" && "required" in def && def.required) { |
| 84 | + required.push(key); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const schema: JsonSchemaObject = { |
| 89 | + type: "object", |
| 90 | + properties, |
| 91 | + additionalProperties: false, |
| 92 | + }; |
| 93 | + if (required.length > 0) schema.required = required; |
| 94 | + return schema; |
| 95 | +} |
| 96 | + |
| 97 | +export interface McpToolDescriptor { |
| 98 | + name: string; |
| 99 | + description: string; |
| 100 | + inputSchema: JsonSchemaObject; |
| 101 | + /** Original CLI path, e.g. `text chat`. */ |
| 102 | + path: string; |
| 103 | + command: AnyCommand; |
| 104 | +} |
| 105 | + |
| 106 | +/** Map leaf commands to MCP tool descriptors. */ |
| 107 | +export function buildToolDescriptors( |
| 108 | + leaves: Array<{ path: string; command: AnyCommand }>, |
| 109 | + options?: { toolNamePrefix?: string; skipPaths?: ReadonlySet<string> }, |
| 110 | +): McpToolDescriptor[] { |
| 111 | + const prefix = options?.toolNamePrefix ?? "bailian"; |
| 112 | + const skipPaths = options?.skipPaths ?? new Set<string>(); |
| 113 | + const tools: McpToolDescriptor[] = []; |
| 114 | + |
| 115 | + for (const leaf of leaves) { |
| 116 | + if (skipPaths.has(leaf.path)) continue; |
| 117 | + const name = pathToToolName(leaf.path, prefix); |
| 118 | + const usage = leaf.command.usageArgs ? ` Usage: ${leaf.command.usageArgs}` : ""; |
| 119 | + tools.push({ |
| 120 | + name, |
| 121 | + description: `${leaf.command.description} (bl ${leaf.path}).${usage}`, |
| 122 | + inputSchema: flagsToInputSchema(leaf.command.flags), |
| 123 | + path: leaf.path, |
| 124 | + command: leaf.command, |
| 125 | + }); |
| 126 | + } |
| 127 | + |
| 128 | + return tools; |
| 129 | +} |
0 commit comments