Files
Arcrun/mcp/src/tools/arcrun_get_gui_context.ts
T
Claude 5fc7277c3c refactor(mcp): u6u_* tool 全面 rename 為 arcrun_* + 前綴收斂單一來源
零 u6u 洩漏(leo 要求 1):connector tool 清單只剩 arcrun_*,並移除壞掉/
重複的 u6u_ workflow tool(deploy/execute/get/list_workflows 由 arcrun_
push/run/get/list_workflows 取代);MCP server name 也由 u6u-mcp-server
改 arcrun-mcp-server。

前綴單一真相源(leo 要求 2):新增 mcp/src/brand.ts(TOOL_PREFIX + toolName()
helper),所有 server.tool() 註冊一律走 toolName("suffix"),前綴不再明碼散寫。
未來 rebrand 只改 brand.ts 一行。

- 12 個獨有能力 tool rename(component/tag/gui/search_workflows)
- 4 個重複/壞掉 tool 移除
- inter-tool hint、描述字串、註解內 u6u_ 名字全數更新
- tsc exit 0;vitest 19/19 綠

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015d5jDbuqT5Htwv3Q88XXKk
2026-07-07 05:21:34 +00:00

86 lines
2.9 KiB
TypeScript

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { toolName } from "../brand.js";
import { z } from "zod";
import { Env } from "../types.js";
import { kbdbFetch } from "../lib/kbdb-client.js";
interface ActionLogSlots {
org_namespace?: string;
action_type?: string;
payload?: string;
occurred_at?: string;
}
interface ActionLogRecord {
id: string;
slots?: ActionLogSlots;
}
export function registerGetGuiContext(server: McpServer, env: Env, orgNamespace: string) {
server.tool(
toolName("get_gui_context"),
"查詢用戶在 GUI 上的最近操作記錄,了解用戶的當前意圖與操作上下文。" +
"回傳最近 N 條操作記錄(從新到舊),以及用戶當前所在頁面和正在編輯的 Workflow ID。",
{
limit: z.number().int().min(1).max(100).default(20)
.describe("要取回的最近操作數量(預設 20,最大 100)"),
},
async ({ limit }) => {
try {
if (!env.KBDB) {
return {
content: [{ type: "text", text: "Error: KBDB service binding unavailable" }],
isError: true,
};
}
const resp = await kbdbFetch(
env,
`/records/search?template_id=tpl-action-log&user_id=${encodeURIComponent(orgNamespace)}&limit=${limit ?? 20}`
);
if (!resp.ok) {
return {
content: [{ type: "text", text: `Error querying action log: ${await resp.text()}` }],
isError: true,
};
}
const data = await resp.json<{ records: ActionLogRecord[] }>();
const records = data.records ?? [];
// 按 occurred_at 降序排列(最新在前)
const sorted = records
.map(r => ({
action_type: r.slots?.action_type ?? '',
payload: (() => {
try { return JSON.parse(r.slots?.payload ?? '{}'); } catch { return {}; }
})(),
occurred_at: r.slots?.occurred_at ?? '',
}))
.sort((a, b) => b.occurred_at.localeCompare(a.occurred_at))
.slice(0, limit ?? 20);
// 提取當前頁面和正在操作的 Workflow
const lastNavigate = sorted.find(a => a.action_type === 'NAVIGATE');
const lastOpenWorkflow = sorted.find(a => a.action_type === 'OPEN_WORKFLOW');
const context = {
recent_actions: sorted,
current_page: (lastNavigate?.payload as { page?: string })?.page ?? null,
open_workflow_id: (lastOpenWorkflow?.payload as { workflow_id?: string })?.workflow_id ?? null,
};
return {
content: [{ type: "text", text: JSON.stringify(context, null, 2) }],
};
} catch (error) {
return {
content: [{ type: "text", text: `Internal Error: ${error instanceof Error ? error.message : String(error)}` }],
isError: true,
};
}
}
);
}