opc-finance-mcp 0.5.0 → 0.7.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.
Files changed (3) hide show
  1. package/README.md +6 -0
  2. package/package.json +1 -1
  3. package/src/index.js +303 -1
package/README.md CHANGED
@@ -75,6 +75,12 @@ npm run inspect
75
75
  | `finance_get_tax_form` | **申报底稿**:按栏次/行次铺开,每格带来源科目与勾稽(只算不提交) | 只读 |
76
76
  | `finance_save_tax_params` | 保存平台报送额、已开票额等人工参数 | 可写 |
77
77
  | `finance_create_tax_period` | 补建季度申报期(最早记账季度 → 当季) | 可写 |
78
+ | `finance_close_period` | 期末结账(先校验借贷与资产负债表平衡) | 可写 |
79
+ | `finance_reopen_period` | 反结账 / 撤销封账,必须写原因 | 可写 |
80
+ | `finance_delete_voucher` | 作废凭证(软删除,可恢复) | 可写 |
81
+ | `finance_restore_voucher` | 恢复已作废凭证 | 可写 |
82
+ | `finance_create_account` | 新增明细科目(挂在指定父级下) | 可写 |
83
+ | `finance_save_employee` | 新增 / 修改员工档案(含离职) | 可写 |
78
84
  | `finance_list_attachments` | 票据资料 | 只读 |
79
85
  | `finance_set_attachment_kind` | 给票据打业务标签(进项/销项/流水…) | 传票 |
80
86
  | `finance_list_employees` | 员工档案(社保基数、公积金比例、专项附加扣除) | 只读 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opc-finance-mcp",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "OPC智能财务 MCP Server —— 让 AI 客户端直接查账、记账、传票",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
package/src/index.js CHANGED
@@ -137,6 +137,27 @@ tool('finance_whoami', {
137
137
  // 版本信息挂在最后,且不参与上面的主体内容:查不到时整行消失,不留空行
138
138
  const ver = await updateCheck();
139
139
  if (ver) lines.push('', `—— MCP ${ver}`);
140
+
141
+ /* 政策到期提醒:税率、免征额、优惠幅度都带失效日,到期后会自动回落到基线
142
+ *(1% 恢复 3%、六税两费不再减半)。不提醒的话,用户会在某个月发现税额突然变了,
143
+ * 而那时已经按错的口径报出去了。 */
144
+ try {
145
+ const tp = await request('/api/tax-params');
146
+ const soon = (tp.items || [])
147
+ .filter(i => i.effective_to)
148
+ .map(i => ({ ...i, days: Math.round((new Date(i.effective_to) - Date.now()) / 86400000) }))
149
+ .filter(i => i.days >= 0 && i.days <= 60);
150
+ if (soon.length) {
151
+ lines.push('', '⚠️ 政策参数即将到期:');
152
+ for (const i of soon) {
153
+ lines.push(` · ${i.label} ${i.value},${i.effective_to} 到期(还剩 ${i.days} 天)`);
154
+ }
155
+ lines.push('到期后自动回落到基线(征收率恢复法定、优惠取消),请确认新政策后新增一条参数。');
156
+ }
157
+ } catch (e) {
158
+ /* 老部署没有这个接口,静默跳过 —— 提醒不该变成 whoami 的失败原因 */
159
+ }
160
+
140
161
  return ok(lines.join('\n'));
141
162
  });
142
163
 
@@ -163,10 +184,48 @@ tool('finance_list_periods', {
163
184
  ].join('\n'));
164
185
  });
165
186
 
187
+ tool('finance_close_period', {
188
+ title: '期末结账',
189
+ description: '⚠️ 会锁期间。结账后该期间的凭证不能再改,只能红冲;也会存一份报表快照供日后核对。'
190
+ + '后端会先校验全账借贷平衡与资产负债表平衡,不平直接拒绝 —— 被拒不是权限问题,是账确实没平。'
191
+ + '结账不可逆到「再开」为止(用 finance_reopen_period),调用前必须跟用户确认期间。',
192
+ inputSchema: {
193
+ period: z.string().regex(/^\d{4}-\d{2}$/).describe('要结账的期间,如 2026-10'),
194
+ note: z.string().optional().describe('结账备注,比如「本月凭证已全部入账」'),
195
+ },
196
+ }, async ({ period, note }) => {
197
+ const d = await request(`/api/periods/${period}/close`, { method: 'POST', body: { note } });
198
+ const s = d.snapshot || {};
199
+ const b = s.balance || {};
200
+ return ok([
201
+ `✅ ${period} 已结账(状态:已结账)`,
202
+ b.totalAssets !== undefined ? `快照:资产 ${fmt(b.totalAssets)}|负债和权益 ${fmt(b.totalLE)}|平衡差 ${fmt(b.check)}` : '',
203
+ s.voucher ? `凭证 ${s.voucher.count} 张,发生额 ${fmt(s.voucher.amount)}` : '',
204
+ '这一期的凭证现在只能红冲,不能再改。',
205
+ ].filter(Boolean).join('\n'));
206
+ });
207
+
208
+ tool('finance_reopen_period', {
209
+ title: '反结账 / 撤销封账',
210
+ description: '⚠️ 会解锁期间,之后报表数字就能被改动。必须写原因(至少 2 个字),原因会进期间操作日志。'
211
+ + '若后面还有已结账/已申报的期间,后端会拒绝,要求从最新一期往前逐期反结账。',
212
+ inputSchema: {
213
+ period: z.string().regex(/^\d{4}-\d{2}$/).describe('要解锁的期间,如 2026-10'),
214
+ reason: z.string().min(2).describe('为什么需要解锁,这段文字将来是唯一的解释'),
215
+ },
216
+ }, async ({ period, reason }) => {
217
+ const d = await request(`/api/periods/${period}/reopen`, { method: 'POST', body: { reason } });
218
+ if (d.already) return ok(`${period} 本来就是未结账状态,无需解锁。`);
219
+ return ok([
220
+ `✅ ${period} 已解锁(原状态:${PERIOD_STATUS_CN[d.was] || d.was || '?'})`,
221
+ `原因已记入期间日志:${reason}`,
222
+ '解锁后改动会改变这一期的报表,已报出的数字若与账面不一致,需要能解释。',
223
+ ].join('\n'));
224
+ });
225
+
166
226
  /* ================================================================
167
227
  * 科目
168
228
  * ================================================================ */
169
-
170
229
  tool('finance_list_accounts', {
171
230
  title: '查询可记账科目',
172
231
  description: '列出可记账的叶子科目(父级科目不能记账)。记账前用它确认科目编码,别凭记忆写编码。',
@@ -195,6 +254,26 @@ tool('finance_list_accounts', {
195
254
  ].join('\n'));
196
255
  });
197
256
 
257
+ tool('finance_create_account', {
258
+ title: '新增明细科目',
259
+ description: '⚠️ 会改账套的科目表。只在「已有科目粒度不够」时用,比如要在 2241 其他应付款下面'
260
+ + '单独核算某类款项。编码不填则按父级编码 + 两位序号自动生成。'
261
+ + '新增的科目默认是可记账的叶子科目;父级会自动变成非末级(不能再直接记账)。'
262
+ + '调用前把父级科目、新科目名称与编码告诉用户确认。',
263
+ inputSchema: {
264
+ parent_code: z.string().describe('上级科目编码,如 2241'),
265
+ name: z.string().describe('新科目名称,如「员工扣款」'),
266
+ code: z.string().optional().describe('完整科目编码(必须以父级编码开头且更长);不填自动生成'),
267
+ },
268
+ }, async ({ parent_code, name, code }) => {
269
+ const d = await request('/api/coa', { method: 'POST', body: { parentCode: parent_code, name, code } });
270
+ return ok([
271
+ `✅ 已新增科目 ${d.code || code || ''} ${name}`,
272
+ `挂在 ${parent_code} 下面。现在可以在记账时直接用它。`,
273
+ '如果这是工资凭证要用的科目,记得在「工资 → 工资凭证科目」里把它选上。',
274
+ ].join('\n'));
275
+ });
276
+
198
277
  /* ================================================================
199
278
  * 报表
200
279
  * ================================================================ */
@@ -464,6 +543,38 @@ tool('finance_reverse_voucher', {
464
543
  ].join('\n'));
465
544
  });
466
545
 
546
+ tool('finance_delete_voucher', {
547
+ title: '作废凭证',
548
+ description: '⚠️ 会改账。软删除:凭证从账上消失,但编号、分录、原始单据都保留,可以随时恢复。'
549
+ + '适用「这张凭证根本不该存在」(重复录入、录错月份)。若只是记错科目或金额、'
550
+ + '或者期间已结账/已申报,应该用 finance_reverse_voucher 红冲。调用前必须跟用户确认凭证号。',
551
+ inputSchema: {
552
+ voucher_id: z.number().int().positive().describe('要作废的凭证 id'),
553
+ reason: z.string().optional().describe('作废原因,会留在凭证变更日志里'),
554
+ },
555
+ }, async ({ voucher_id, reason }) => {
556
+ const d = await request(`/api/voucher/${voucher_id}`, { method: 'DELETE', body: { reason } });
557
+ if (d.already) return ok(`凭证 id=${voucher_id} 已经是作废状态。`);
558
+ return ok([
559
+ `✅ 已作废:${d.period} 第 ${d.voucher_no} 号(id=${voucher_id})`,
560
+ '分录与原始单据都保留,需要时可以用 finance_restore_voucher 恢复。',
561
+ d.note || '',
562
+ ].filter(Boolean).join('\n'));
563
+ });
564
+
565
+ tool('finance_restore_voucher', {
566
+ title: '恢复已作废凭证',
567
+ description: '把作废的凭证恢复回账上。凭证所在期间必须未结账,否则后端会拒绝。',
568
+ inputSchema: {
569
+ voucher_id: z.number().int().positive().describe('要恢复的凭证 id'),
570
+ reason: z.string().optional().describe('恢复原因'),
571
+ },
572
+ }, async ({ voucher_id, reason }) => {
573
+ const d = await request(`/api/voucher/${voucher_id}/restore`, { method: 'POST', body: { reason } });
574
+ if (d.already) return ok(`凭证 id=${voucher_id} 本来就没被作废。`);
575
+ return ok(`✅ 已恢复:${d.period} 的凭证 id=${d.id}。报表数字已随之变化。`);
576
+ });
577
+
467
578
  /* ================================================================
468
579
  * 票据
469
580
  * ================================================================ */
@@ -724,6 +835,59 @@ tool('finance_list_employees', {
724
835
  ].join('\n'));
725
836
  });
726
837
 
838
+ tool('finance_save_employee', {
839
+ title: '新增或修改员工档案',
840
+ description: '⚠️ 会改工资计算的基础数据。不传 employee_id 则新增,传了则改那一条(只改传了的字段)。'
841
+ + '社保基数、公积金基数、六项专项附加扣除直接影响个税:改错一个月,汇算要补税。'
842
+ + '离职用 status="left" + left_at,不要删 —— 删了历史工资表就对不上人了。',
843
+ inputSchema: {
844
+ employee_id: z.number().int().positive().optional().describe('改谁;不填表示新增'),
845
+ name: z.string().optional().describe('姓名(新增时必填)'),
846
+ id_no: z.string().optional().describe('身份证号'),
847
+ phone: z.string().optional().describe('手机号'),
848
+ hired_at: z.string().optional().describe('入职日期 YYYY-MM-DD'),
849
+ left_at: z.string().optional().describe('离职日期 YYYY-MM-DD'),
850
+ si_base: z.number().optional().describe('社保缴费基数;不填按当月应发工资'),
851
+ hf_base: z.number().optional().describe('公积金缴费基数'),
852
+ deduct_child: z.number().optional().describe('子女教育(每月)'),
853
+ deduct_edu: z.number().optional().describe('继续教育(每月)'),
854
+ deduct_housing: z.number().optional().describe('住房贷款利息(每月)'),
855
+ deduct_rent: z.number().optional().describe('住房租金(每月)'),
856
+ deduct_elder: z.number().optional().describe('赡养老人(每月)'),
857
+ deduct_medical: z.number().optional().describe('大病医疗(每月,通常年度汇算时用)'),
858
+ bank_account: z.string().optional().describe('工资卡号'),
859
+ status: z.enum(['active', 'left']).optional().describe('active 在职 / left 离职'),
860
+ note: z.string().optional().describe('备注'),
861
+ },
862
+ }, async (a) => {
863
+ if (!a.employee_id && !a.name) {
864
+ return ok('没有提交:新增员工必须给 name。');
865
+ }
866
+ if (a.employee_id) {
867
+ const { employee_id, ...patch } = a;
868
+ const body = {};
869
+ for (const [k, v] of Object.entries(patch)) if (v !== undefined) body[k] = v;
870
+ if (!Object.keys(body).length) return ok('没有要改的字段。');
871
+ await request(`/api/employees/${employee_id}`, { method: 'PUT', body });
872
+ return ok(`✅ 已更新员工 id=${employee_id} 的 ${Object.keys(body).length} 个字段:${Object.keys(body).join('、')}`);
873
+ }
874
+ /* 新增接口收的是 camelCase(前端表单直传),修改接口收 snake_case(逐字段映射)。
875
+ * 两个接口的入参风格不一致是历史原因,MCP 这层统一成 snake_case,这里做一次转换。 */
876
+ const body = {
877
+ name: a.name, idNo: a.id_no, phone: a.phone, hiredAt: a.hired_at,
878
+ siBase: a.si_base, hfBase: a.hf_base,
879
+ deductChild: a.deduct_child, deductEdu: a.deduct_edu, deductHousing: a.deduct_housing,
880
+ deductRent: a.deduct_rent, deductElder: a.deduct_elder, deductMedical: a.deduct_medical,
881
+ bankAccount: a.bank_account, note: a.note,
882
+ };
883
+ for (const k of Object.keys(body)) if (body[k] === undefined) delete body[k];
884
+ const d = await request('/api/employees', { method: 'POST', body });
885
+ return ok([
886
+ `✅ 已新增员工 ${a.name}(id=${d.id})`,
887
+ '下一步:在「工资」页把这个人本月的收入填上,再算个税。',
888
+ ].join('\n'));
889
+ });
890
+
727
891
  tool('finance_calc_payroll', {
728
892
  title: '算一期工资(预览,不落库)',
729
893
  description: '按累计预扣预缴法算某月每个人的社保公积金、个税与实发。'
@@ -920,6 +1084,144 @@ tool('finance_save_tax_params', {
920
1084
  ].join('\n'));
921
1085
  });
922
1086
 
1087
+ /* ================================================================
1088
+ * 发票 / 流水 / 起草凭证(第 2 期)
1089
+ *
1090
+ * 这一组工具刻意停在「建议」那一步:AI 可以读票、读流水、算出借贷,
1091
+ * 但过账必须走 finance_create_voucher,且要用户确认。
1092
+ * 读接口与写接口分开,是为了让「先看清再动手」成为默认姿势。
1093
+ * ================================================================ */
1094
+
1095
+ const INVOICE_STATUS_CN = { new: '待处理', posted: '已入账', ignored: '已忽略' };
1096
+
1097
+ tool('finance_list_invoices', {
1098
+ title: '查询发票',
1099
+ description: '按期间 / 方向 / 状态查发票,返回销项进项的价税合计与税额小计。'
1100
+ + '核对「账上收入 vs 开票额」、回答「这个季度开了多少票」都用它。',
1101
+ inputSchema: {
1102
+ from: z.string().optional().describe('起始期间 YYYY-MM'),
1103
+ to: z.string().optional().describe('结束期间 YYYY-MM'),
1104
+ direction: z.enum(['销项', '进项']).optional().describe('销项=我开出去的,进项=我收到的'),
1105
+ status: z.enum(['new', 'posted', 'ignored']).optional().describe('核对状态'),
1106
+ q: z.string().optional().describe('发票号 / 对方 / 品名关键字'),
1107
+ limit: z.number().int().min(1).max(200).optional().describe('最多返回多少张,默认 50'),
1108
+ },
1109
+ }, async (a) => {
1110
+ const d = await request('/api/invoices' + qs({ ...a, limit: a.limit || 50 }));
1111
+ if (!d.rows.length) return ok('没有符合条件的发票。');
1112
+ const s = d.summary;
1113
+ return ok([
1114
+ `# 发票(共 ${d.total} 张,显示 ${d.rows.length} 张)`,
1115
+ `销项价税合计 ${fmt(s.outIncl)}(税额 ${fmt(s.outTax)})|进项价税合计 ${fmt(s.inIncl)}(税额 ${fmt(s.inTax)})|待处理 ${s.pending} 张`,
1116
+ '',
1117
+ '| id | 开票日期 | 发票号 | 方向 | 对方 | 不含税 | 税额 | 价税合计 | 状态 |',
1118
+ '| --- | --- | --- | --- | --- | --- | --- | --- | --- |',
1119
+ ...d.rows.map(r => `| ${r.id} | ${String(r.invoice_date || '').slice(0, 10)} | ${r.invoice_no} | ${r.direction} | `
1120
+ + `${String((r.direction === '销项' ? r.buyer : r.seller) || '').replace(/\|/g, '/').slice(0, 24)} | `
1121
+ + `${fmt(r.amount_excl_tax)} | ${fmt(r.tax)} | ${fmt(r.amount_incl_tax)} | ${INVOICE_STATUS_CN[r.status] || r.status} |`),
1122
+ '',
1123
+ '要把它变成凭证,用 finance_suggest_voucher 传 invoice_id 拿建议分录,确认后再 finance_create_voucher。',
1124
+ ].join('\n'));
1125
+ });
1126
+
1127
+ tool('finance_list_bank_flows', {
1128
+ title: '查询银行流水',
1129
+ description: '按日期区间 / 账户 / 状态查流水,返回流入流出小计。'
1130
+ + '对「这个月钱都去哪了」「哪几笔还没入账」这类问题,先看它再决定记哪几笔。',
1131
+ inputSchema: {
1132
+ from: z.string().optional().describe('起始日期 YYYY-MM-DD'),
1133
+ to: z.string().optional().describe('结束日期 YYYY-MM-DD'),
1134
+ account: z.string().optional().describe('账户名,如「交通银行2668」'),
1135
+ status: z.enum(['new', 'posted', 'ignored']).optional().describe('核对状态'),
1136
+ q: z.string().optional().describe('对方或摘要关键字'),
1137
+ limit: z.number().int().min(1).max(200).optional().describe('最多返回多少条,默认 50'),
1138
+ },
1139
+ }, async (a) => {
1140
+ const d = await request('/api/bank-flows' + qs({ ...a, limit: a.limit || 50 }));
1141
+ if (!d.rows.length) return ok('没有符合条件的流水。');
1142
+ const s = d.summary;
1143
+ return ok([
1144
+ `# 流水(共 ${d.total} 条,显示 ${d.rows.length} 条)`,
1145
+ `流入 ${fmt(s.inflow)}|流出 ${fmt(s.outflow)}|净额 ${fmt(s.net)}|待处理 ${s.pending} 条`,
1146
+ '',
1147
+ '| id | 日期 | 账户 | 方向 | 金额 | 对方 | 摘要 | 状态 |',
1148
+ '| --- | --- | --- | --- | --- | --- | --- | --- |',
1149
+ ...d.rows.map(r => `| ${r.id} | ${String(r.txn_date || '').slice(0, 10)} | ${r.account} | ${r.direction} | `
1150
+ + `${fmt(r.amount)} | ${String(r.counterparty || '').replace(/\|/g, '/').slice(0, 20)} | `
1151
+ + `${String(r.memo || '').replace(/\|/g, '/').slice(0, 24)} | ${INVOICE_STATUS_CN[r.status] || r.status} |`),
1152
+ '',
1153
+ '下一步:finance_suggest_voucher 传 flow_ids 拿建议分录。',
1154
+ ].join('\n'));
1155
+ });
1156
+
1157
+ tool('finance_import_bank_flow', {
1158
+ title: '导入银行流水',
1159
+ description: '把一批流水写进核对清单。⚠️ 会写库(但不是记账)。'
1160
+ + '同一账户+日期+方向+金额+对方方视为同一条,重复导入会被跳过而不是翻倍。'
1161
+ + '导入后还要用 finance_suggest_voucher 起草、finance_create_voucher 过账。',
1162
+ inputSchema: {
1163
+ rows: z.array(z.object({
1164
+ account: z.string().describe('账户名,如「交通银行2668」'),
1165
+ txn_date: z.string().describe('交易日期 YYYY-MM-DD'),
1166
+ direction: z.enum(['收', '支']).optional().describe('收支方向;不填按金额正负推断'),
1167
+ amount: z.number().describe('金额(正数;填负数会按方向推断)'),
1168
+ counterparty: z.string().optional().describe('对方户名'),
1169
+ memo: z.string().optional().describe('摘要 / 商品说明'),
1170
+ balance: z.number().optional().describe('账户余额'),
1171
+ })).min(1).max(500).describe('流水明细'),
1172
+ },
1173
+ }, async ({ rows }) => {
1174
+ const d = await request('/api/bank-flows', { method: 'POST', body: { rows } });
1175
+ const bad = d.errors || [];
1176
+ return ok([
1177
+ `✅ 导入完成:新增 ${d.inserted || 0} 条,跳过重复 ${d.skipped || 0} 条`
1178
+ + (bad.length ? `,${bad.length} 条没通过校验` : '') + '。',
1179
+ ...bad.slice(0, 5).map(e => ` · 第 ${e.row} 行:${e.msg}`),
1180
+ '',
1181
+ '接着可以对这批流水调 finance_suggest_voucher 看建议分录。',
1182
+ ].join('\n'));
1183
+ });
1184
+
1185
+ tool('finance_suggest_voucher', {
1186
+ title: '起草凭证建议(不记账)',
1187
+ description: '给发票或流水算出建议分录,**只返回建议,不写账**。'
1188
+ + '依据三档:同一对手方的历史记法 > 摘要关键词对应科目 > 收入/费用兜底。'
1189
+ + '拿到结果后把分录念给用户确认,再用 finance_create_voucher 保存。',
1190
+ inputSchema: {
1191
+ flow_ids: z.array(z.number().int().positive()).max(20).optional()
1192
+ .describe('要起草的流水 id(来自 finance_list_bank_flows)'),
1193
+ invoice_id: z.number().int().positive().optional().describe('要起草的发票 id'),
1194
+ },
1195
+ }, async ({ flow_ids, invoice_id }) => {
1196
+ if (!flow_ids?.length && !invoice_id) return ok('请给 flow_ids 或 invoice_id。');
1197
+
1198
+ let body;
1199
+ if (invoice_id) {
1200
+ const inv = await request('/api/invoice/' + invoice_id).catch(() => null);
1201
+ if (!inv || !inv.id) return ok(`没找到 id=${invoice_id} 的发票,先用 finance_list_invoices 确认。`);
1202
+ body = { rows: [{ type: 'invoice', direction: inv.direction, amount_incl_tax: inv.amount_incl_tax,
1203
+ tax: inv.tax, is_red: inv.is_red, memo: inv.item_name, counterparty: inv.buyer }] };
1204
+ } else {
1205
+ body = { flow_ids };
1206
+ }
1207
+
1208
+ const d = await request('/api/voucher/suggest', { method: 'POST', body });
1209
+ const list = d.suggestions || [];
1210
+ if (!list.length) return ok('没算出来。检查科目表是否为空。');
1211
+
1212
+ const out = ['# 建议分录(尚未入账)'];
1213
+ list.forEach((s, i) => {
1214
+ out.push('', `## 第 ${i + 1} 条${s.key ? `(来源 id=${s.key})` : ''}`, `依据:${s.reason}`);
1215
+ if (!s.entries.length) { out.push('没有可用科目,无法建议。'); return; }
1216
+ out.push('', '| 科目 | 名称 | 借方 | 贷方 |', '| --- | --- | --- | --- |',
1217
+ ...s.entries.map(e => `| ${e.account_code} | ${e.account_name || ''} | `
1218
+ + `${e.debit ? fmt(e.debit) : ''} | ${e.credit ? fmt(e.credit) : ''} |`));
1219
+ out.push(`合计:借 ${fmt(s.debit)}|贷 ${fmt(s.credit)}${s.balanced ? '(平)' : '(不平,需手工调整)'}`);
1220
+ });
1221
+ out.push('', '把上面每一行念给用户确认后,再用 finance_create_voucher 提交;不要跳过确认。');
1222
+ return ok(out.join('\n'));
1223
+ });
1224
+
923
1225
  /* ================================================================
924
1226
  * 启动
925
1227
  * ================================================================ */