MCPcopy Create free account
hub / github.com/allocnode/oh-my-sage / registerVariableTools

Function registerVariableTools

src/mcp/tools/variable.ts:16–124  ·  view source on GitHub ↗
(
  server: McpServer,
  gatewayManager: GatewayManager
)

Source from the content-addressed store, hash-verified

14 errorResult,
15 formatVariableListMarkdown,
16} from "../utils.js";
17
18export 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
26export const CreateVariableInputSchema = CreateVariableToolInputSchema.refine(
27 ({ type, value }) => typeof value === type,
28 { message: "value 必须与 type 匹配", path: ["value"] }
29);
30
31const VARIABLE_HINT = "变量过多,请用 mijia_get_variable_value 按 id 单独查询";
32
33/** 把核心层返回值包装为对象,便于作为 structuredContent */
34function asRecord(data: unknown): Record<string, unknown> {
35 return data && typeof data === "object" && !Array.isArray(data) ? (data as Record<string, unknown>) : { value: data };
36}
37
38export 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

Callers 1

createServerFunction · 0.85

Calls 6

getVariablesFunction · 0.85
handleErrorFunction · 0.85
formatJsonFunction · 0.85
setVariableFunction · 0.85
ensureConnectedMethod · 0.80

Tested by

no test coverage detected