@fangcuntai/fangcuntai-mcp 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -0
- package/dist/client/api-client.d.ts +27 -0
- package/dist/client/api-client.js +102 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +20 -0
- package/dist/context/family-context.d.ts +55 -0
- package/dist/context/family-context.js +183 -0
- package/dist/context/write-lock.d.ts +31 -0
- package/dist/context/write-lock.js +87 -0
- package/dist/errors/user-messages.d.ts +2 -0
- package/dist/errors/user-messages.js +30 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +21 -0
- package/dist/tools/account.d.ts +4 -0
- package/dist/tools/account.js +14 -0
- package/dist/tools/context.d.ts +3 -0
- package/dist/tools/context.js +57 -0
- package/dist/tools/drafts.d.ts +3 -0
- package/dist/tools/drafts.js +147 -0
- package/dist/tools/index.d.ts +3 -0
- package/dist/tools/index.js +14 -0
- package/dist/tools/ledger.d.ts +18 -0
- package/dist/tools/ledger.js +392 -0
- package/dist/tools/notes.d.ts +3 -0
- package/dist/tools/notes.js +307 -0
- package/dist/tools/safety.d.ts +2 -0
- package/dist/tools/safety.js +24 -0
- package/dist/utils/money.d.ts +10 -0
- package/dist/utils/money.js +28 -0
- package/dist/utils/response.d.ts +5 -0
- package/dist/utils/response.js +21 -0
- package/package.json +50 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
/** 会话解锁有效期(无写操作也会到期) */
|
|
3
|
+
const UNLOCK_TTL_MS = 15 * 60 * 1000;
|
|
4
|
+
/** 单次确认码有效期 */
|
|
5
|
+
const CONFIRM_TTL_MS = 2 * 60 * 1000;
|
|
6
|
+
let unlockedUntil = 0;
|
|
7
|
+
const pending = new Map();
|
|
8
|
+
export function clearWriteLockState() {
|
|
9
|
+
unlockedUntil = 0;
|
|
10
|
+
pending.clear();
|
|
11
|
+
}
|
|
12
|
+
export function isWritesUnlocked() {
|
|
13
|
+
return Date.now() < unlockedUntil;
|
|
14
|
+
}
|
|
15
|
+
export function unlockWrites(ttlMs = UNLOCK_TTL_MS) {
|
|
16
|
+
unlockedUntil = Date.now() + ttlMs;
|
|
17
|
+
return {
|
|
18
|
+
unlocked: true,
|
|
19
|
+
expires_in_seconds: Math.floor(ttlMs / 1000),
|
|
20
|
+
hint: "已解锁写操作。入账/删除/清空回收站/确认草稿仍须 prepare → 向用户展示摘要 → confirm。",
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export function lockWrites() {
|
|
24
|
+
unlockedUntil = 0;
|
|
25
|
+
pending.clear();
|
|
26
|
+
return { unlocked: false, hint: "已重新上锁,危险写操作已禁用。" };
|
|
27
|
+
}
|
|
28
|
+
export function getWriteLockStatus() {
|
|
29
|
+
const remain = Math.max(0, unlockedUntil - Date.now());
|
|
30
|
+
// 清理过期 pending
|
|
31
|
+
const now = Date.now();
|
|
32
|
+
for (const [k, v] of pending) {
|
|
33
|
+
if (v.expiresAt <= now)
|
|
34
|
+
pending.delete(k);
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
unlocked: remain > 0,
|
|
38
|
+
expires_in_seconds: Math.floor(remain / 1000),
|
|
39
|
+
pending_count: pending.size,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** 写操作前门:未解锁则抛错。 */
|
|
43
|
+
export function requireWritesUnlocked() {
|
|
44
|
+
if (!isWritesUnlocked()) {
|
|
45
|
+
throw new Error("写操作已上锁。请先向用户确认后调用 unlock_writes,再 prepare → confirm。查账与建草稿不受影响。");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export function createPending(kind, familyId, summary, exec) {
|
|
49
|
+
requireWritesUnlocked();
|
|
50
|
+
// 续期解锁时间,避免确认过程中突然上锁
|
|
51
|
+
unlockedUntil = Math.max(unlockedUntil, Date.now() + UNLOCK_TTL_MS);
|
|
52
|
+
const token = `cfm_${randomBytes(16).toString("hex")}`;
|
|
53
|
+
const item = {
|
|
54
|
+
kind,
|
|
55
|
+
token,
|
|
56
|
+
expiresAt: Date.now() + CONFIRM_TTL_MS,
|
|
57
|
+
familyId,
|
|
58
|
+
summary,
|
|
59
|
+
exec,
|
|
60
|
+
};
|
|
61
|
+
pending.set(token, item);
|
|
62
|
+
return item;
|
|
63
|
+
}
|
|
64
|
+
export function takePending(token, expectedKind) {
|
|
65
|
+
const item = pending.get(token);
|
|
66
|
+
if (!item) {
|
|
67
|
+
throw new Error("确认码无效或已使用,请重新 prepare");
|
|
68
|
+
}
|
|
69
|
+
if (item.expiresAt <= Date.now()) {
|
|
70
|
+
pending.delete(token);
|
|
71
|
+
throw new Error("确认码已过期(约 2 分钟),请重新 prepare");
|
|
72
|
+
}
|
|
73
|
+
if (expectedKind && item.kind !== expectedKind) {
|
|
74
|
+
throw new Error(`确认码类型不匹配:期望 ${expectedKind},实际 ${item.kind}`);
|
|
75
|
+
}
|
|
76
|
+
pending.delete(token);
|
|
77
|
+
return item;
|
|
78
|
+
}
|
|
79
|
+
export function pendingPublicView(item) {
|
|
80
|
+
return {
|
|
81
|
+
confirm_token: item.token,
|
|
82
|
+
kind: item.kind,
|
|
83
|
+
expires_in_seconds: Math.max(0, Math.floor((item.expiresAt - Date.now()) / 1000)),
|
|
84
|
+
summary: item.summary,
|
|
85
|
+
hint: "请向用户展示 summary,得到明确同意后再调用对应的 confirm_* 工具并传入 confirm_token。",
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** API 业务码 → 面向家长的中文提示(不暴露内部细节)。 */
|
|
2
|
+
export function mapApiError(code, message) {
|
|
3
|
+
switch (code) {
|
|
4
|
+
case 40101:
|
|
5
|
+
case 40102:
|
|
6
|
+
return "连接已失效,请在方寸台 App 重新生成桌面助手令牌";
|
|
7
|
+
case 40201:
|
|
8
|
+
return "试用已结束,请在 App 内订阅后继续使用";
|
|
9
|
+
case 40202:
|
|
10
|
+
return "AI 额度不足,请在 App 内购买增量包";
|
|
11
|
+
case 40301:
|
|
12
|
+
return "您不是该家庭成员";
|
|
13
|
+
case 40302:
|
|
14
|
+
return "当前角色没有此操作权限";
|
|
15
|
+
case 40304:
|
|
16
|
+
return "游客无法使用桌面助手写入能力,请先在 App 绑定邮箱";
|
|
17
|
+
case 40305:
|
|
18
|
+
return "当前令牌未勾选此项权限。请在方寸台 App 生成新令牌并勾选对应权限;已有令牌不能改权限";
|
|
19
|
+
case 40401:
|
|
20
|
+
return "未找到相关数据";
|
|
21
|
+
case 40907:
|
|
22
|
+
return "这本账已经挂过了";
|
|
23
|
+
case 40908:
|
|
24
|
+
return "这本账里已经记过这个订单号,不会重复入账";
|
|
25
|
+
case 42901:
|
|
26
|
+
return "操作过于频繁,请稍后再试";
|
|
27
|
+
default:
|
|
28
|
+
return message || "操作失败,请稍后重试";
|
|
29
|
+
}
|
|
30
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
4
|
+
import { FangcuntaiApiClient } from "./client/api-client.js";
|
|
5
|
+
import { loadConfig } from "./config.js";
|
|
6
|
+
import { registerTools } from "./tools/index.js";
|
|
7
|
+
async function main() {
|
|
8
|
+
const config = loadConfig();
|
|
9
|
+
const client = new FangcuntaiApiClient(config.apiBase, config.token);
|
|
10
|
+
const server = new McpServer({
|
|
11
|
+
name: "fangcuntai",
|
|
12
|
+
version: "1.5.0",
|
|
13
|
+
});
|
|
14
|
+
registerTools(server, client);
|
|
15
|
+
const transport = new StdioServerTransport();
|
|
16
|
+
await server.connect(transport);
|
|
17
|
+
}
|
|
18
|
+
main().catch((err) => {
|
|
19
|
+
console.error(err);
|
|
20
|
+
process.exit(1);
|
|
21
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { FangcuntaiApiClient } from "../client/api-client.js";
|
|
3
|
+
/** V2 只读:订阅与 AI 积分(需后端助手令牌白名单放行)。 */
|
|
4
|
+
export declare function registerAccountTools(server: McpServer, client: FangcuntaiApiClient): void;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { okJson, runTool } from "../utils/response.js";
|
|
2
|
+
/** V2 只读:订阅与 AI 积分(需后端助手令牌白名单放行)。 */
|
|
3
|
+
export function registerAccountTools(server, client) {
|
|
4
|
+
server.tool("get_subscription_info", "查询当前账号的订阅状态(试用/会员等),只读。", {}, async () => runTool(async () => {
|
|
5
|
+
client.setToolName("get_subscription_info");
|
|
6
|
+
const data = await client.get("/api/subscriptions/info");
|
|
7
|
+
return okJson(data);
|
|
8
|
+
}));
|
|
9
|
+
server.tool("get_credit_balance", "查询当前账号的 AI 积分余额,只读。", {}, async () => runTool(async () => {
|
|
10
|
+
client.setToolName("get_credit_balance");
|
|
11
|
+
const data = await client.get("/api/credits/balance");
|
|
12
|
+
return okJson(data);
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFamilyId, familyPath, getAssistantContext, getWorkspaceContext, selectFamily, } from "../context/family-context.js";
|
|
3
|
+
import { okJson, runTool } from "../utils/response.js";
|
|
4
|
+
export function registerContextTools(server, client) {
|
|
5
|
+
server.tool("get_assistant_context", "获取助手令牌上下文(默认家庭、scopes)。连接后建议先调用,再按需 list_my_families / select_family。", {}, async () => runTool(async () => {
|
|
6
|
+
client.setToolName("get_assistant_context");
|
|
7
|
+
const data = await getAssistantContext(client);
|
|
8
|
+
const workspace = await getWorkspaceContext(client);
|
|
9
|
+
return okJson({ ...data, workspace });
|
|
10
|
+
}));
|
|
11
|
+
server.tool("get_active_workspace", "查看当前会话选中的家庭与账本(未选账本时使用该家庭默认账本)。", {}, async () => runTool(async () => {
|
|
12
|
+
client.setToolName("get_active_workspace");
|
|
13
|
+
return okJson(await getWorkspaceContext(client));
|
|
14
|
+
}));
|
|
15
|
+
server.tool("list_my_families", "列出当前用户已加入的家庭,用于切换家庭前查阅 family_id。", {
|
|
16
|
+
page: z.number().int().min(1).optional(),
|
|
17
|
+
page_size: z.number().int().min(1).max(50).optional(),
|
|
18
|
+
}, async ({ page, page_size }) => runTool(async () => {
|
|
19
|
+
client.setToolName("list_my_families");
|
|
20
|
+
const data = await client.get("/api/families", {
|
|
21
|
+
page: page ?? 1,
|
|
22
|
+
page_size: page_size ?? 20,
|
|
23
|
+
});
|
|
24
|
+
const workspace = await getWorkspaceContext(client);
|
|
25
|
+
return okJson({ ...data, workspace });
|
|
26
|
+
}));
|
|
27
|
+
server.tool("select_family", "切换当前会话操作的家庭。须为 list_my_families 中的家庭;切换后账本重置为该家庭默认账本。", {
|
|
28
|
+
family_id: z.number().int().positive().describe("目标家庭 ID"),
|
|
29
|
+
}, async ({ family_id }) => runTool(async () => {
|
|
30
|
+
client.setToolName("select_family");
|
|
31
|
+
const workspace = await selectFamily(client, family_id);
|
|
32
|
+
return okJson({
|
|
33
|
+
ok: true,
|
|
34
|
+
hint: "已切换家庭;如需指定账本请先 list_ledger_books 再 select_ledger_book。",
|
|
35
|
+
workspace,
|
|
36
|
+
});
|
|
37
|
+
}));
|
|
38
|
+
server.tool("get_bound_family", "获取当前会话家庭详情(名称、角色等)。未 select_family 时为令牌默认家庭。", {}, async () => runTool(async () => {
|
|
39
|
+
client.setToolName("get_bound_family");
|
|
40
|
+
const familyId = await ensureFamilyId(client);
|
|
41
|
+
const data = await client.get(familyPath(familyId, ""));
|
|
42
|
+
const workspace = await getWorkspaceContext(client);
|
|
43
|
+
return okJson({ family_id: familyId, ...data, workspace });
|
|
44
|
+
}));
|
|
45
|
+
server.tool("get_family_entitlements", "查询当前会话家庭的能力权限(can_use_ledger / can_view_notes 等),写操作前应先调用。", {}, async () => runTool(async () => {
|
|
46
|
+
client.setToolName("get_family_entitlements");
|
|
47
|
+
const familyId = await ensureFamilyId(client);
|
|
48
|
+
const data = await client.get(familyPath(familyId, "/entitlements"));
|
|
49
|
+
return okJson(data);
|
|
50
|
+
}));
|
|
51
|
+
server.tool("list_family_members", "列出当前会话家庭的成员昵称与角色(不含敏感信息)。", {}, async () => runTool(async () => {
|
|
52
|
+
client.setToolName("list_family_members");
|
|
53
|
+
const familyId = await ensureFamilyId(client);
|
|
54
|
+
const data = await client.get(familyPath(familyId, "/members"));
|
|
55
|
+
return okJson(data);
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { ensureFamilyId, familyPath, requireTokenScope, TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER, } from "../context/family-context.js";
|
|
3
|
+
import { createPending, pendingPublicView, takePending, } from "../context/write-lock.js";
|
|
4
|
+
import { fenToYuan } from "../utils/money.js";
|
|
5
|
+
import { okJson, runTool } from "../utils/response.js";
|
|
6
|
+
export function registerDraftTools(server, client) {
|
|
7
|
+
server.tool("list_ai_drafts", "列出当前账号自己创建的待确认 AI 草稿(记账/记事)。家长也不能看家人的。可用 session_id=mcp 只看桌面助手草稿。", {
|
|
8
|
+
status: z.string().optional().describe("草稿状态筛选,如 pending"),
|
|
9
|
+
session_id: z.string().optional().describe("可选:mcp 或小家师会话 id"),
|
|
10
|
+
}, async ({ status, session_id }) => runTool(async () => {
|
|
11
|
+
client.setToolName("list_ai_drafts");
|
|
12
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
13
|
+
const familyId = await ensureFamilyId(client);
|
|
14
|
+
const data = await client.get(familyPath(familyId, "/ai-drafts"), {
|
|
15
|
+
status,
|
|
16
|
+
session_id,
|
|
17
|
+
});
|
|
18
|
+
return okJson(data);
|
|
19
|
+
}));
|
|
20
|
+
server.tool("get_ai_draft", "获取单个 AI 草稿详情。", { draft_id: z.string().min(1) }, async ({ draft_id }) => runTool(async () => {
|
|
21
|
+
client.setToolName("get_ai_draft");
|
|
22
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
23
|
+
const familyId = await ensureFamilyId(client);
|
|
24
|
+
const data = await client.get(familyPath(familyId, `/ai-drafts/${draft_id}`));
|
|
25
|
+
return okJson(data);
|
|
26
|
+
}));
|
|
27
|
+
server.tool("prepare_confirm_ai_draft", "准备确认草稿(不写入)。须先 unlock_writes。返回摘要 + confirm_token,用户同意后再 confirm_ai_draft。", { draft_id: z.string().min(1) }, async ({ draft_id }) => runTool(async () => {
|
|
28
|
+
client.setToolName("prepare_confirm_ai_draft");
|
|
29
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
30
|
+
const familyId = await ensureFamilyId(client);
|
|
31
|
+
const draft = await client.get(familyPath(familyId, `/ai-drafts/${draft_id}`));
|
|
32
|
+
if (draft?.draft_type === "reminder") {
|
|
33
|
+
throw new Error("事情提醒功能已下线,无法确认提醒草稿,请丢弃该草稿");
|
|
34
|
+
}
|
|
35
|
+
const payload = draft.payload && typeof draft.payload === "object"
|
|
36
|
+
? draft.payload
|
|
37
|
+
: {};
|
|
38
|
+
const amount = Number(payload.amount ?? 0);
|
|
39
|
+
const summary = {
|
|
40
|
+
action: "confirm_ai_draft",
|
|
41
|
+
draft_id,
|
|
42
|
+
draft_type: draft.draft_type,
|
|
43
|
+
payload_preview: payload,
|
|
44
|
+
};
|
|
45
|
+
if (draft.draft_type === "ledger" && amount > 0) {
|
|
46
|
+
summary.amount_yuan = fenToYuan(amount);
|
|
47
|
+
}
|
|
48
|
+
const pending = createPending("confirm_draft", familyId, summary, { draft_id });
|
|
49
|
+
return okJson(pendingPublicView(pending));
|
|
50
|
+
}));
|
|
51
|
+
server.tool("confirm_ai_draft", "确认 AI 草稿并写入正式数据。必须传入 prepare_confirm_ai_draft 返回的 confirm_token(不再接受仅 draft_id)。", {
|
|
52
|
+
confirm_token: z.string().min(1).describe("来自 prepare_confirm_ai_draft"),
|
|
53
|
+
}, async ({ confirm_token }) => runTool(async () => {
|
|
54
|
+
client.setToolName("confirm_ai_draft");
|
|
55
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
56
|
+
const pending = takePending(confirm_token, "confirm_draft");
|
|
57
|
+
const draftId = String(pending.exec.draft_id ?? "");
|
|
58
|
+
if (!draftId)
|
|
59
|
+
throw new Error("确认码缺少 draft_id");
|
|
60
|
+
const data = await client.post(familyPath(pending.familyId, `/ai-drafts/${draftId}/confirm`));
|
|
61
|
+
return okJson({ ...data, hint: "草稿已确认并写入正式数据。" });
|
|
62
|
+
}));
|
|
63
|
+
server.tool("discard_ai_draft", "丢弃 AI 草稿(不可恢复)。丢弃草稿不改正式账本,无需解锁。", { draft_id: z.string().min(1) }, async ({ draft_id }) => runTool(async () => {
|
|
64
|
+
client.setToolName("discard_ai_draft");
|
|
65
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
66
|
+
const familyId = await ensureFamilyId(client);
|
|
67
|
+
await client.post(familyPath(familyId, `/ai-drafts/${draft_id}/discard`));
|
|
68
|
+
return okJson({ ok: true, draft_id });
|
|
69
|
+
}));
|
|
70
|
+
server.tool("prepare_batch_confirm_ai_drafts", "准备批量确认草稿(不写入)。须先 unlock_writes。返回摘要 + confirm_token,用户同意后再 batch_confirm_ai_drafts。", {
|
|
71
|
+
draft_ids: z.array(z.string().min(1)).min(1).max(50).describe("待确认的草稿 ID 列表"),
|
|
72
|
+
}, async ({ draft_ids }) => runTool(async () => {
|
|
73
|
+
client.setToolName("prepare_batch_confirm_ai_drafts");
|
|
74
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
75
|
+
const familyId = await ensureFamilyId(client);
|
|
76
|
+
const drafts = [];
|
|
77
|
+
for (const draft_id of draft_ids) {
|
|
78
|
+
try {
|
|
79
|
+
const draft = await client.get(familyPath(familyId, `/ai-drafts/${draft_id}`));
|
|
80
|
+
if (draft?.draft_type === "reminder") {
|
|
81
|
+
drafts.push({
|
|
82
|
+
draft_id,
|
|
83
|
+
draft_type: "reminder",
|
|
84
|
+
error: "事情提醒已下线,无法确认,请丢弃",
|
|
85
|
+
});
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const payload = draft.payload && typeof draft.payload === "object"
|
|
89
|
+
? draft.payload
|
|
90
|
+
: {};
|
|
91
|
+
const item = {
|
|
92
|
+
draft_id,
|
|
93
|
+
draft_type: draft.draft_type,
|
|
94
|
+
payload_preview: payload,
|
|
95
|
+
};
|
|
96
|
+
const amount = Number(payload.amount ?? 0);
|
|
97
|
+
if (draft.draft_type === "ledger" && amount > 0) {
|
|
98
|
+
item.amount_yuan = fenToYuan(amount);
|
|
99
|
+
}
|
|
100
|
+
drafts.push(item);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
drafts.push({ draft_id, error: "not_found_or_inaccessible" });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const confirmableIds = drafts
|
|
107
|
+
.filter((d) => !d.error)
|
|
108
|
+
.map((d) => String(d.draft_id));
|
|
109
|
+
if (confirmableIds.length === 0) {
|
|
110
|
+
throw new Error("没有可确认的草稿(均不可访问或为已下线类型)");
|
|
111
|
+
}
|
|
112
|
+
const summary = {
|
|
113
|
+
action: "batch_confirm_ai_drafts",
|
|
114
|
+
requested_count: draft_ids.length,
|
|
115
|
+
confirmable_count: confirmableIds.length,
|
|
116
|
+
drafts,
|
|
117
|
+
};
|
|
118
|
+
const pending = createPending("batch_confirm_draft", familyId, summary, {
|
|
119
|
+
draft_ids: confirmableIds,
|
|
120
|
+
});
|
|
121
|
+
return okJson(pendingPublicView(pending));
|
|
122
|
+
}));
|
|
123
|
+
server.tool("batch_confirm_ai_drafts", "批量确认 AI 草稿并写入正式记账/记事。必须传入 prepare_batch_confirm_ai_drafts 返回的 confirm_token(不再接受仅 draft_ids)。", {
|
|
124
|
+
confirm_token: z.string().min(1).describe("来自 prepare_batch_confirm_ai_drafts"),
|
|
125
|
+
}, async ({ confirm_token }) => runTool(async () => {
|
|
126
|
+
client.setToolName("batch_confirm_ai_drafts");
|
|
127
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
128
|
+
const pending = takePending(confirm_token, "batch_confirm_draft");
|
|
129
|
+
const draftIds = pending.exec.draft_ids;
|
|
130
|
+
if (!Array.isArray(draftIds) || draftIds.length === 0) {
|
|
131
|
+
throw new Error("确认码缺少 draft_ids");
|
|
132
|
+
}
|
|
133
|
+
const data = await client.post(familyPath(pending.familyId, "/ai-drafts/batch-confirm"), { draft_ids: draftIds });
|
|
134
|
+
return okJson({ ...data, hint: "批量确认已完成;请查看 succeeded/failed。" });
|
|
135
|
+
}));
|
|
136
|
+
server.tool("batch_discard_ai_drafts", "批量丢弃 AI 草稿(不可恢复)。", {
|
|
137
|
+
draft_ids: z.array(z.string().min(1)).min(1).max(50).describe("待丢弃的草稿 ID 列表"),
|
|
138
|
+
}, async ({ draft_ids }) => runTool(async () => {
|
|
139
|
+
client.setToolName("batch_discard_ai_drafts");
|
|
140
|
+
await requireTokenScope(client, [TOKEN_SCOPE_WRITE_DRAFT, TOKEN_SCOPE_WRITE_LEDGER]);
|
|
141
|
+
const familyId = await ensureFamilyId(client);
|
|
142
|
+
const data = await client.post(familyPath(familyId, "/ai-drafts/batch-discard"), {
|
|
143
|
+
draft_ids,
|
|
144
|
+
});
|
|
145
|
+
return okJson(data);
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { registerAccountTools } from "./account.js";
|
|
2
|
+
import { registerContextTools } from "./context.js";
|
|
3
|
+
import { registerDraftTools } from "./drafts.js";
|
|
4
|
+
import { registerLedgerTools } from "./ledger.js";
|
|
5
|
+
import { registerNoteTools } from "./notes.js";
|
|
6
|
+
import { registerSafetyTools } from "./safety.js";
|
|
7
|
+
export function registerTools(server, client) {
|
|
8
|
+
registerSafetyTools(server);
|
|
9
|
+
registerContextTools(server, client);
|
|
10
|
+
registerLedgerTools(server, client);
|
|
11
|
+
registerNoteTools(server, client);
|
|
12
|
+
registerDraftTools(server, client);
|
|
13
|
+
registerAccountTools(server, client);
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { FangcuntaiApiClient } from "../client/api-client.js";
|
|
3
|
+
export type PrepareCreateLedgerInput = {
|
|
4
|
+
type?: "expense" | "income";
|
|
5
|
+
amount_yuan: number;
|
|
6
|
+
category?: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
date?: string;
|
|
9
|
+
currency?: string;
|
|
10
|
+
ledger_book_id?: number;
|
|
11
|
+
tags?: string[];
|
|
12
|
+
external_order_id?: string;
|
|
13
|
+
};
|
|
14
|
+
/** 准备记一笔:须先 unlock_writes。不打入账 API。 */
|
|
15
|
+
export declare function prepareCreateLedgerEntry(client: FangcuntaiApiClient, input: PrepareCreateLedgerInput): Promise<Record<string, unknown>>;
|
|
16
|
+
/** 确认记一笔:一次性 token,POST 金额为分,并标记 source=desktop_mcp。 */
|
|
17
|
+
export declare function confirmCreateLedgerEntry(client: FangcuntaiApiClient, confirmToken: string): Promise<Record<string, unknown>>;
|
|
18
|
+
export declare function registerLedgerTools(server: McpServer, client: FangcuntaiApiClient): void;
|