( server: McpServer, gatewayManager: GatewayManager )
| 14 | errorResult, |
| 15 | formatVariableListMarkdown, |
| 16 | } from "../utils.js"; |
| 17 | |
| 18 | export const CreateVariableToolInputSchema = z.object({ |
| 19 | id: z.string().regex(/^[a-zA-Z0-9]+$/, "id 必须是纯字母数字,不能含下划线/连字符").describe("变量ID,纯字母数字"), |
| 20 | type: z.enum(["number", "string"]).describe("变量类型"), |
| 21 | value: z.union([z.string(), z.number()]).describe("初始值"), |
| 22 | name: z.string().trim().min(1, "name 不能为空").optional().describe("显示名称,可选,默认与 id 相同"), |
| 23 | scope: z.string().default("global").describe("变量作用域,默认 global"), |
| 24 | }); |
| 25 | |
| 26 | export const CreateVariableInputSchema = CreateVariableToolInputSchema.refine( |
| 27 | ({ type, value }) => typeof value === type, |
| 28 | { message: "value 必须与 type 匹配", path: ["value"] } |
| 29 | ); |
| 30 | |
| 31 | const VARIABLE_HINT = "变量过多,请用 mijia_get_variable_value 按 id 单独查询"; |
| 32 | |
| 33 | /** 把核心层返回值包装为对象,便于作为 structuredContent */ |
| 34 | function asRecord(data: unknown): Record<string, unknown> { |
| 35 | return data && typeof data === "object" && !Array.isArray(data) ? (data as Record<string, unknown>) : { value: data }; |
| 36 | } |
| 37 | |
| 38 | export function registerVariableTools( |
| 39 | server: McpServer, |
| 40 | gatewayManager: GatewayManager |
| 41 | ): void { |
| 42 | // ==================== mijia_get_variables ==================== |
| 43 | server.registerTool( |
| 44 | "mijia_get_variables", |
| 45 | { |
| 46 | title: "获取变量列表", |
| 47 | description: `获取自动化变量列表。 |
| 48 | |
| 49 | 变量可用于规则间的数据传递和状态存储。 |
| 50 | |
| 51 | 参数: |
| 52 | - scope (string, optional): 变量作用域,默认 "global" |
| 53 | - response_format: markdown 每个变量一行;json 为紧凑 JSON {count, scope, variables}`, |
| 54 | inputSchema: z.object({ |
| 55 | scope: z.string().default("global").describe("变量作用域,默认 global"), |
| 56 | response_format: ResponseFormatSchema.optional().default("markdown").describe("输出格式"), |
| 57 | }), |
| 58 | annotations: { |
| 59 | readOnlyHint: true, |
| 60 | destructiveHint: false, |
| 61 | idempotentHint: true, |
| 62 | openWorldHint: true, |
| 63 | }, |
| 64 | }, |
| 65 | async ({ scope = "global", response_format = "markdown" }) => { |
| 66 | try { |
| 67 | gatewayManager.ensureConnected(); |
| 68 | const result = await getVariables(gatewayManager.gateway!, scope); |
| 69 | if (!result.success) return errorResult(new Error(result.error), "get_variables"); |
| 70 | |
| 71 | const variables = result.data ?? []; |
| 72 | const output = { count: variables.length, scope, variables }; |
| 73 |
no test coverage detected