@teamdream/sems-mcp 1.1.3 → 1.3.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 CHANGED
@@ -1,101 +1,115 @@
1
- # @teamdream/sems-mcp
2
-
3
- 海竣信息系统(SEMS)的 MCP 连接器。以 **stdio** 方式运行在用户电脑上,把销售单、采购单、报价、合同、库存、
4
- 客户供应商、工作日志与经营统计等能力暴露成 28 个 `sems_*` 工具,供支持 MCP 的客户端(如 WorkBuddy)调用。
5
-
6
- - **不直连 SEMS**:所有请求经 `sems-proxy` 转发,出站前用 HMAC-SHA256 签名。
7
- - **不持有账号密码**:绑定只下发一对 `key_id` + `secret`,密码只提交给 SEMS 登录接口。
8
- - **写操作两阶段**:起草(不写库)→ 用户确认 → 凭 `draftId + digest` 提交,摘要不一致会被拒绝。
9
-
10
- ## 环境要求
11
-
12
- | 项 | 要求 |
13
- |---|---|
14
- | Node.js | >= 22.18 |
15
- | 网络 | 能访问 sems-proxy(建议 HTTPS) |
16
- | 系统 | Windows / macOS / Linux |
17
-
18
- ## 安装与配置
19
-
20
- 通常不需要手动安装:在 WorkBuddy 连接器市场添加「海竣信息系统助手」,客户端会用 `npx` 自动拉起本包。
21
-
22
- 手工配置 MCP 客户端时,在 `mcp.json` 中加:
23
-
24
- ```json
25
- {
26
- "mcpServers": {
27
- "sems": {
28
- "type": "stdio",
29
- "command": "npx",
30
- "args": ["-y", "@teamdream/sems-mcp@1.0.1"],
31
- "env": {
32
- "WB_PROXY_BASE_URL": "https://<你的 sems-proxy 地址>",
33
- "WB_BIND_TOKEN": "<绑定码>"
34
- }
35
- }
36
- }
37
- }
38
- ```
39
-
40
- 也可以全局安装后直接用命令启动:
41
-
42
- ```bash
43
- npm install -g @teamdream/sems-mcp
44
- sems-mcp
45
- ```
46
-
47
- ## 绑定流程
48
-
49
- 1. 浏览器打开 `<WB_PROXY_BASE_URL>/bind`,用 SEMS 账号登录并勾选要绑定的公司;
50
- 2. 页面给出一次性绑定码,填进 MCP 客户端的表单(或 `WB_BIND_TOKEN`);
51
- 3. 下次启动时自动用绑定码换密钥,写入 `~/.sems-mcp/binding.json`(权限 0600),之后不再需要绑定码。
52
-
53
- 命令行绑定(等价流程,适合排障):
54
-
55
- ```bash
56
- sems-mcp-bind --proxy https://<你的 sems-proxy 地址>
57
- ```
58
-
59
- 一家公司绑定后可以在会话里用 `sems_switch_tenant` 切换,不需要重新绑定。
60
-
61
- ## 环境变量
62
-
63
- | 变量 | 必填 | 说明 |
64
- |---|---|---|
65
- | `WB_PROXY_BASE_URL` | 二选一 | sems-proxy 地址,如 `https://ai-agent.example.com` |
66
- | `WB_BIND_TOKEN` | 否 | 连接器表单带回的绑定码;有值就尝试兑换密钥 |
67
- | `WB_KEY_ID` / `WB_SECRET` | 否 | 手工指定密钥,优先级高于绑定文件 |
68
- | `WB_TENANT_ID` | 否 | 指定公司;留空用绑定时选中的默认公司 |
69
- | `WB_BINDING_FILE` | 否 | 覆盖密钥文件路径,默认 `~/.sems-mcp/binding.json` |
70
- | `WB_TIMEOUT_MS` | 否 | 单次请求超时,默认 `30000` |
71
- | `WB_LOG_LEVEL` | 否 | `debug` / `info` / `warn` / `error`,默认 `info`,日志只写 stderr |
72
-
73
- ## 安全说明
74
-
75
- - 日志走 stderr 并脱敏,stdout 只输出 JSON-RPC 帧;
76
- - 密钥只落 `~/.sems-mcp/binding.json`;Windows 无法把文件权限收到 0600,请确认该目录不被他人读取;
77
- - 权限预检是 fail-closed:结构异常时按「无权限」处理;
78
- - 成本、利润、采购价等字段由服务端按账号权限删除,无权限时字段不存在而不是空值。
79
-
80
- ## 可用工具
81
-
82
- 工具清单真源在服务端的 `configs/tools.yaml`。不确定能力边界时先调 `sems_capabilities`。
83
-
84
- 不提供删单、改单、审批、导出 Excel/PDF、发消息、跨公司汇总。
85
-
86
- ## 开发
87
-
88
- ```bash
89
- npm ci
90
- npm run build # 产出 dist(含 shebang)
91
- npm run typecheck
92
- npm test
93
- npm run dev # 需要 .env,参考 .env.example
94
- ```
95
-
96
- 发布:`npm publish --access public --registry=https://registry.npmjs.org/ --userconfig=<专用 npmrc 路径>`
97
- (该 npmrc 里放一枚勾了 Bypass 2FA、具备 `@teamdream` 写权限的 granular token)。
98
- `prepublishOnly` 会先跑 typecheck 与测试,`prepack` 会重新构建 `dist`,包内只包含 `dist/**/*.js`、README 与 LICENSE。
99
-
100
- 🔴 **不要加 `--ignore-scripts`**:它会同时跳过 typecheck、测试和 `prepack` 的构建,把当前 `dist`(可能是旧的)原样发出去;
101
- npm 不允许覆盖已发布版本,发错只能再发一个版本号补救。
1
+ # @teamdream/sems-mcp
2
+
3
+ 海竣信息系统(SEMS)的 MCP 连接器。以 **stdio** 方式运行在用户电脑上,把销售单、采购单、报价、合同、库存、
4
+ 客户供应商、工作日志与经营统计等能力暴露给支持 MCP 的客户端(如 WorkBuddy)。
5
+
6
+ - **只有 5 个工具**:`sems_help` / `sems_call` / `sems_prepare` / `sems_commit` / `sems_switch_tenant`。
7
+ 具体业务能力(查单据、建销售单…)由 sems-proxy 按当前账号权限下发,**新增能力不需要发 npm 包**。
8
+ - **不直连 SEMS**:所有请求经 `sems-proxy` 转发,出站前用 HMAC-SHA256 签名。
9
+ - **不持有账号密码**:绑定只下发一对 `key_id` + `secret`,密码只提交给 SEMS 登录接口。
10
+ - **写操作两阶段**:起草(不写库)→ 用户确认 → 凭 `draftId + digest` 提交,摘要不一致会被拒绝。
11
+
12
+ ## 环境要求
13
+
14
+ | 项 | 要求 |
15
+ |---|---|
16
+ | Node.js | >= 22.18 |
17
+ | 网络 | 能访问 sems-proxy(建议 HTTPS) |
18
+ | 系统 | Windows / macOS / Linux |
19
+
20
+ ## 安装与配置
21
+
22
+ 通常不需要手动安装:在 WorkBuddy 连接器市场添加「海竣信息系统助手」,客户端会用 `npx` 自动拉起本包。
23
+
24
+ 手工配置 MCP 客户端时,在 `mcp.json` 中加:
25
+
26
+ ```json
27
+ {
28
+ "mcpServers": {
29
+ "sems": {
30
+ "type": "stdio",
31
+ "command": "npx",
32
+ "args": ["-y", "@teamdream/sems-mcp@1.2.0"],
33
+ "env": {
34
+ "WB_PROXY_BASE_URL": "https://<你的 sems-proxy 地址>",
35
+ "WB_BIND_TOKEN": "<绑定码>"
36
+ }
37
+ }
38
+ }
39
+ }
40
+ ```
41
+
42
+ 也可以全局安装后直接用命令启动:
43
+
44
+ ```bash
45
+ npm install -g @teamdream/sems-mcp
46
+ sems-mcp
47
+ ```
48
+
49
+ ## 绑定流程
50
+
51
+ 1. 浏览器打开 `<WB_PROXY_BASE_URL>/bind`,用 SEMS 账号登录并勾选要绑定的公司;
52
+ 2. 页面给出一次性绑定码,填进 MCP 客户端的表单(或 `WB_BIND_TOKEN`);
53
+ 3. 下次启动时自动用绑定码换密钥,写入 `~/.sems-mcp/binding.json`(权限 0600),之后不再需要绑定码。
54
+
55
+ 命令行绑定(等价流程,适合排障):
56
+
57
+ ```bash
58
+ sems-mcp-bind --proxy https://<你的 sems-proxy 地址>
59
+ ```
60
+
61
+ 一家公司绑定后可以在会话里用 `sems_switch_tenant` 切换,不需要重新绑定。
62
+
63
+ ## 环境变量
64
+
65
+ | 变量 | 必填 | 说明 |
66
+ |---|---|---|
67
+ | `WB_PROXY_BASE_URL` | 二选一 | sems-proxy 地址,如 `https://ai-agent.example.com` |
68
+ | `WB_BIND_TOKEN` | 否 | 连接器表单带回的绑定码;有值就尝试兑换密钥 |
69
+ | `WB_KEY_ID` / `WB_SECRET` | 否 | 手工指定密钥,优先级高于绑定文件 |
70
+ | `WB_TENANT_ID` | 否 | 指定公司;留空用绑定时选中的默认公司 |
71
+ | `WB_BINDING_FILE` | 否 | 覆盖密钥文件路径,默认 `~/.sems-mcp/binding.json` |
72
+ | `WB_TIMEOUT_MS` | 否 | 单次请求超时,默认 `30000` |
73
+ | `WB_LOG_LEVEL` | 否 | `debug` / `info` / `warn` / `error`,默认 `info`,日志只写 stderr |
74
+
75
+ ## 安全说明
76
+
77
+ - 日志走 stderr 并脱敏,stdout 只输出 JSON-RPC 帧;
78
+ - 密钥只落 `~/.sems-mcp/binding.json`;Windows 无法把文件权限收到 0600,请确认该目录不被他人读取;
79
+ - 权限判定在服务端:能力清单按账号权限过滤(看不到就是没权限),`/invoke` 再判一次,
80
+ 两侧共用同一份权限码,**MCP 进程内不做权限预检**;
81
+ - 权限取不到时 fail-closed(按空权限处理),宁可少下发能力;
82
+ - 成本、利润、采购价等字段由服务端按账号权限删除,无权限时字段不存在而不是空值。
83
+
84
+ ## 可用工具
85
+
86
+ | 工具 | 用途 |
87
+ |---|---|
88
+ | `sems_help` | 能力清单(含每条能力的参数契约)+ 已绑定公司 + 当前公司 |
89
+ | `sems_call` | 执行只读能力:`name` + `args` |
90
+ | `sems_prepare` | 起草写操作,返回 `draftId` / `digest` / `summary` / `warnings`,不写库 |
91
+ | `sems_commit` | 提交草稿,只认 `draftId` + `digest` |
92
+ | `sems_switch_tenant` | 切换当前公司(改本机 `binding.json`,是唯一非转发工具) |
93
+
94
+ 工具清单真源在服务端的 `configs/tools.yaml`;**业务能力的真源在 proxy 的 `wb_allowlist`**
95
+ (`capability` 非空的行),后台 `/_proxy/admin/ui` 可改,改完立即生效。
96
+ 不确定能力边界时先调 `sems_help`。
97
+
98
+ 不提供删单、改单、审批、导出 Excel/PDF、发消息、跨公司汇总。
99
+
100
+ ## 开发
101
+
102
+ ```bash
103
+ npm ci
104
+ npm run build # 产出 dist(含 shebang)
105
+ npm run typecheck
106
+ npm test
107
+ npm run dev # 需要 .env,参考 .env.example
108
+ ```
109
+
110
+ 发布:`npm publish --access public --registry=https://registry.npmjs.org/ --userconfig=<专用 npmrc 路径>`
111
+ (该 npmrc 里放一枚勾了 Bypass 2FA、具备 `@teamdream` 写权限的 granular token)。
112
+ `prepublishOnly` 会先跑 typecheck 与测试,`prepack` 会重新构建 `dist`,包内只包含 `dist/**/*.js`、README 与 LICENSE。
113
+
114
+ 🔴 **不要加 `--ignore-scripts`**:它会同时跳过 typecheck、测试和 `prepack` 的构建,把当前 `dist`(可能是旧的)原样发出去;
115
+ npm 不允许覆盖已发布版本,发错只能再发一个版本号补救。
@@ -137,7 +137,7 @@ export class RuleBook {
137
137
  this.saveCache();
138
138
  }
139
139
  catch (err) {
140
- // 老 proxy 没有这个端点时会被 catch-all 判成 NOT_IN_ALLOWLIST,属预期降级。
140
+ // 未升级的 proxy(catch-all 仍在转发的版本)没有这个端点,会回 NOT_IN_ALLOWLIST,属预期降级。
141
141
  // 清空快照会触发一条「已撤销」,proxy 抖动一次就把在用的规则撤掉了。
142
142
  const reason = err instanceof Error ? err.message : String(err);
143
143
  log.warn("内置规则拉取失败,沿用上一次成功的规则", { reason });
@@ -41,10 +41,16 @@ const HINTS = {
41
41
  HMAC_TS_SKEW: "本机时间与服务器相差超过 60 秒,签名会被判定过期。请校准系统时间(开启自动同步)后重试。",
42
42
  HMAC_MISSING: "本机没有正确附加签名头,通常意味着 sems-mcp 版本过旧或配置被改写。",
43
43
  HMAC_INVALID: "密钥与服务器记录不一致(常见原因:重新绑定后仍在使用旧密钥)。请确认使用最新的绑定信息。",
44
- NOT_IN_ALLOWLIST: "该能力尚未对 WorkBuddy 开放。白名单在构建期打进 sems-proxy 镜像,"
45
- + "登记到 configs/whitelist.yaml 后必须重建 sems-proxy 才生效(只重启容器无效)。",
44
+ NOT_IN_ALLOWLIST: "该接口没有对 WorkBuddy 开放。白名单已改为运行时从 proxy 数据库读取,"
45
+ + "管理员在 /_proxy/admin/ui 的「接口白名单」里新增后立即生效、不需要重建;"
46
+ + "若确认已开通,请检查对应能力是否被停用。",
47
+ CAPABILITY_NOT_FOUND: "这个能力不存在,或当前账号没有权限。先调 sems_help 看可用能力清单,不要猜名称。",
48
+ PARAM_INVALID: "参数不符合该能力的契约:可能是参数名写错、类型不对,或缺少必填项。"
49
+ + "按 sems_help 里该能力的 params 逐项核对——服务端不会静默忽略多余或写错的参数。",
50
+ DRAFT_INVALID: "草稿校验失败:内容被改动过,或提交时传了与起草时不一致的摘要。请重新 prepare。",
51
+ DRAFT_NOT_FOUND: "草稿不存在或已过期(有效期 30 分钟)。请重新 prepare。",
46
52
  WRITE_DISABLED: "写操作当前是关闭的(ALLOW_WRITE=false),只能查询。",
47
- CONTRACT_MISMATCH: "本机的 sems-mcp 与服务器端的 sems-proxy 版本不匹配,请升级后重试。",
53
+ CONTRACT_MISMATCH: "本机连接器与服务器端版本不匹配,请立即升级到最新版本后重试。",
48
54
  RATE_LIMITED: "调用过于频繁,请稍等几秒再试。",
49
55
  UPSTREAM_TIMEOUT: "海竣信息系统响应超时,服务可能正忙。稍后重试通常可恢复。",
50
56
  };
@@ -1,37 +1,13 @@
1
1
  // 本文件由 scripts/gen_mcp_manifest.py 自动生成,请勿手工修改。
2
2
  // 真源:configs/tools.yaml(工具清单)+ configs/permissions.yaml(权限字典)
3
3
  // 重新生成:server/proxy/.venv/Scripts/python.exe scripts/gen_mcp_manifest.py
4
- export const CONTRACT_VERSION = 2;
4
+ export const CONTRACT_VERSION = 3;
5
5
  export const TOOLS = [
6
- { name: "sems_whoami", tier: "P0", perm: null, write: false, desc: "当前绑定的海竣信息系统用户与租户" },
7
- { name: "sems_capabilities", tier: "P0", perm: null, write: false, desc: "权限字典 + 缓存的 permissions,反查「我能做什么」" },
8
- { name: "sems_list_tenants", tier: "P0", perm: null, write: false, desc: "这把密钥绑定了哪几家公司、当前用的是哪家" },
6
+ { name: "sems_help", tier: "P0", perm: null, write: false, desc: "我能做什么:按当前账号权限下发能力清单、已绑定公司与当前公司" },
9
7
  { name: "sems_switch_tenant", tier: "P0", perm: null, write: false, desc: "切换当前使用的公司(default 回默认公司),不重绑、不需要密码" },
10
- { name: "sems_query_sales", tier: "P1", perm: ["invoicing_sale_view"], write: false, desc: "分页查销售单" },
11
- { name: "sems_query_purchases", tier: "P1", perm: ["order_orderform_view"], write: false, desc: "分页查采购单" },
12
- { name: "sems_query_contracts", tier: "P1", perm: ["invoicing_contract_view"], write: false, desc: "分页查合同" },
13
- { name: "sems_query_quotations", tier: "P1", perm: ["pre_sale_management"], write: false, desc: "分页查报价单" },
14
- { name: "sems_query_market_quotes", tier: "P1", perm: ["market_dynamics_management", "market_dynamics_view", "pre_sale_management"], write: false, desc: "本公司市场行情;报价单第一顺位产品来源" },
15
- { name: "sems_query_group_market_quotes", tier: "P1", perm: ["market_dynamics_management", "market_dynamics_view", "pre_sale_management"], write: false, desc: "集团共享市场动态(集团自建报价);报价单第二顺位,需本公司已加入集团" },
16
- { name: "sems_query_vendor_prices", tier: "P1", perm: ["market_dynamics_management", "pre_sale_management"], write: false, desc: "标准产品清单(固定 bizType=standard_product);报价单第四顺位来源" },
17
- { name: "sems_query_stock", tier: "P1", perm: null, write: false, desc: "库存流水;报价单第三顺位产品来源,可用量 amountRemain" },
18
- { name: "sems_query_products", tier: "P1", perm: null, write: false, desc: "商品档案;仅支持按 name 模糊,不供报价单取产品" },
19
- { name: "sems_query_partners", tier: "P1", perm: null, write: false, desc: "客户/供应商(supplier 同形)" },
20
- { name: "sems_query_daily_reports", tier: "P1", perm: null, write: false, desc: "工作日志(周报素材,受数据范围约束)" },
21
- { name: "sems_stat", tier: "P1", perm: null, write: false, desc: "统计入口,type 枚举 sales/purchase/inventory/contract/dashboard,非法值拒绝" },
22
- { name: "sems_weekly_report_data", tier: "P1", perm: null, write: false, desc: "聚合:工作日志 + 销售统计" },
23
- { name: "sems_prepare_sale", tier: "P2", perm: ["invoicing_sale_management"], write: false, desc: "起草销售单(不写库)" },
24
- { name: "sems_commit_sale", tier: "P2", perm: ["invoicing_sale_management"], write: true, desc: "提交销售单;须带 storageManageRecordId,否则库存不扣减" },
25
- { name: "sems_prepare_purchase", tier: "P2", perm: ["order_orderform_add"], write: false, desc: "起草采购单(不写库)" },
26
- { name: "sems_commit_purchase", tier: "P2", perm: ["order_orderform_add"], write: true, desc: "提交采购单" },
27
- { name: "sems_prepare_quotation", tier: "P2", perm: ["pre_sale_management"], write: false, desc: "起草报价单(不写库)" },
28
- { name: "sems_commit_quotation", tier: "P2", perm: ["pre_sale_management"], write: true, desc: "提交报价单" },
29
- { name: "sems_create_contract", tier: "P2", perm: ["invoicing_contract_add"], write: true, desc: "建合同记录" },
30
- { name: "sems_upload_file", tier: "P2", perm: null, write: true, desc: "上传附件(body 摘要用固定占位符)" },
31
- { name: "sems_relate_contract", tier: "P2", perm: ["invoicing_contract_edit"], write: true, desc: "合同关联单据" },
32
- { name: "sems_shortage_to_purchase", tier: "P3", perm: ["order_orderform_add"], write: true, desc: "销售草稿 → 库存比对 → 采购草稿(推荐供应商取 si_market_quotes)" },
33
- { name: "sems_document_to_sale", tier: "P3", perm: ["invoicing_sale_management"], write: true, desc: "结构化数据 → 销售草稿 + 合同草稿 + 附件上传" },
34
- { name: "sems_contract_from_template", tier: "P3", perm: ["invoicing_contract_add"], write: true, desc: "模板渲染 → 上传 → 建合同 → 关联销售单" },
8
+ { name: "sems_call", tier: "P1", perm: null, write: false, desc: "执行只读能力;name 与 args 来自 sems_help 的能力清单" },
9
+ { name: "sems_prepare", tier: "P2", perm: null, write: false, desc: "起草写操作:返回 draftId / digest / summary / warnings,不写库" },
10
+ { name: "sems_commit", tier: "P2", perm: null, write: true, desc: "提交草稿:只认 draftId + digest,不接受业务参数覆盖;重复提交返回上次结果" },
35
11
  ];
36
12
  export const PERMISSIONS = [
37
13
  { code: "invoicing_sale_view", name: "销售单查看", covers: "查询销售单、销售统计" },
@@ -108,9 +84,13 @@ export const ERROR_MAP = {
108
84
  "BIND_DISABLED": { code: "BIND_DISABLED", http: 503, retry: false, message: "绑定入口已暂停(管理员关闭了 BIND_ENABLED)" },
109
85
  "BIND_IN_PROGRESS": { code: "BIND_IN_PROGRESS", http: 409, retry: true, message: "这个绑定码正在提交中,请勿重复点击;稍等片刻再看结果" },
110
86
  "PERMISSION_DENIED": { code: "PERMISSION_DENIED", http: 403, retry: false, message: "当前账号缺少该操作所需的权限" },
87
+ "CAPABILITY_NOT_FOUND": { code: "CAPABILITY_NOT_FOUND", http: 404, retry: false, message: "没有这个能力,请先查看可用能力清单" },
88
+ "DRAFT_NOT_FOUND": { code: "DRAFT_NOT_FOUND", http: 404, retry: false, message: "草稿不存在或已过期,请重新起草" },
89
+ "DRAFT_INVALID": { code: "DRAFT_INVALID", http: 400, retry: false, message: "草稿校验失败:确认的内容与实际提交的不一致" },
90
+ "DRAFT_STATE_INVALID": { code: "DRAFT_STATE_INVALID", http: 409, retry: false, message: "草稿状态不允许该操作(可能已提交或正在提交)" },
111
91
  "UPSTREAM_ERROR": { code: "UPSTREAM_ERROR", http: 502, retry: true, message: "海竣信息系统服务异常" },
112
92
  "UPSTREAM_TIMEOUT": { code: "UPSTREAM_TIMEOUT", http: 504, retry: true, message: "海竣信息系统响应超时" },
113
- "CONTRACT_MISMATCH": { code: "CONTRACT_MISMATCH", http: 426, retry: false, message: "客户端与代理版本不匹配,请升级 WorkBuddy 侧组件" },
93
+ "CONTRACT_MISMATCH": { code: "CONTRACT_MISMATCH", http: 426, retry: false, message: "连接器版本过旧,请立即升级到最新版本" },
114
94
  "CONFIG_INVALID": { code: "CONFIG_INVALID", http: 500, retry: false, message: "配置不合法,服务已拒绝启动" },
115
95
  "ADMIN_DISABLED": { code: "ADMIN_DISABLED", http: 503, retry: false, message: "管理接口未启用:服务器未设置管理员密码" },
116
96
  "ADMIN_UNAUTHORIZED": { code: "ADMIN_UNAUTHORIZED", http: 401, retry: false, message: "管理员未登录或会话已过期" },
package/dist/main.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
- * sems-mcp 进程入口:stdio 传输层 + 工具注册。
3
+ * sems-mcp 进程入口:stdio 传输层 + 5 个通道工具注册。
4
4
  *
5
5
  * 🔴 三条硬约束,每条踩了都表现为「连接器不正常」而报错现场不在这里:
6
6
  *
@@ -10,106 +10,116 @@
10
10
  * WorkBuddy 认为连接器崩溃。
11
11
  * 3. **未绑定不能让进程崩**。起不来 → 连工具列表都没有 → 用户无从自助。
12
12
  * 正确做法是照常暴露工具,调用时统一返回「请先绑定」。
13
+ *
14
+ * 🔴 这个进程里**没有业务逻辑**:能力清单、参数校验、body 构造、草稿状态机、
15
+ * 权限判定、错误解释全在 sems-proxy。所以新增业务能力不需要发 npm 包——
16
+ * 改 proxy 的能力注册表即可,这里长期不动。
13
17
  */
14
18
  import { readFileSync } from "node:fs";
15
19
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
16
20
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
17
21
  import { RuleBook, withRuleDescriptions } from "./agent-rules.js";
18
22
  import { fetchHealth, ProxyClient } from "./client/proxy.js";
19
- import { loadConfig } from "./config.js";
20
- import { DraftStore } from "./drafts/store.js";
23
+ import { loadConfig, persistActiveTenant } from "./config.js";
21
24
  import { CONTRACT_VERSION, TOOLS } from "./generated/manifest.js";
22
25
  import { log, setLogLevel } from "./logger.js";
23
- import { PermissionGuard } from "./permission-guard.js";
24
26
  import { redeemIfNeeded } from "./redeem.js";
25
- import { registerComposite } from "./tools/composite.js";
26
- import { registerIdentity } from "./tools/identity.js";
27
- import { registerOrchestration } from "./tools/orchestrate.js";
28
- import { registerQueries } from "./tools/queries.js";
29
- import { registerWrites } from "./tools/writes.js";
30
- import { UserProfileCache } from "./user-profile.js";
27
+ import { registerChannel } from "./tools/channel.js";
31
28
  const SERVER_NAME = "sems";
32
29
  /** 自报版本取自包清单,换版本号时无需改这里。 */
33
30
  const SERVER_VERSION = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version;
34
31
  /**
35
32
  * 随 initialize 下发的服务说明。
36
33
  *
37
- * 用户机器上可能同时挂了多个连接器(运维、文档、网盘),模型是按工具名与描述选的,
38
- * 所以这里只写「怎么选路」和「写操作怎么收口」两件事——业务细节留在工具描述里,
39
- * 免得每次连接都往上下文塞一大段会过期的口径。
34
+ * 只写「怎么选路」和「写操作怎么收口」两件事:能力细节由 sems_help 按当前账号
35
+ * 实时下发,不塞进这里——写在这里的内容一发版就固定了,会很快过期。
40
36
  *
41
37
  * 管理员配置的按公司规则不写在这里:WorkBuddy 客户端不消费 initialize 的 instructions,
42
38
  * 规则随工具注册拼进 description(见 withRuleDescriptions)。
43
- *
44
- * 客户端不读这个字段也不影响功能(协议里是可选项),读了能少走弯路。
45
39
  */
46
40
  const SERVER_INSTRUCTIONS = [
47
41
  "这是海竣信息系统(SEMS,ERP)的连接器,工具全部以 sems_ 开头。",
48
42
  "涉及销售单、采购单、报价单、合同、库存、商品、客户供应商、工作日志、经营统计的请求,一律使用 sems_* 工具,"
49
- + "不要用其它连接器的同类工具替代;不确定能力边界时先调 sems_capabilities。",
50
- "读操作直接执行。销售单、采购单、报价单的写操作分两段:先 sems_prepare_* 出草稿并把 summary 与 warnings "
51
- + "完整呈现给用户,用户明确同意后再用 draftId 与 digest 调 sems_commit_*;用户要改任何一项就重新 prepare,不要改旧草稿。",
52
- "sems_create_contract / sems_relate_contract / sems_upload_file 与 sems_contract_from_template 是直接写库、没有草稿阶段,"
53
- + "调用前先向用户确认客户全称、合同类型、关联单据等关键字段。",
43
+ + "不要用其它连接器的同类工具替代。",
44
+ "不确定能做什么时,先调 sems_help:它按当前账号的权限下发能力清单,含每个能力的参数名与用法。",
45
+ "查数据用 sems_call(name, args)。写操作分两段:先 sems_prepare 出草稿,把 summary 与 warnings 完整呈现给用户,"
46
+ + "用户明确同意后再用 draftId 与 digest 调 sems_commit;用户要改任何一项就重新 prepare,不要改旧草稿。",
54
47
  "单据编号只能来自返回值,回查不到就说让用户到系统里查看,不要自己编。",
55
48
  "成本、利润、采购价等字段受账号权限控制,无权限时服务端已删除,不要当成数据缺失。",
56
49
  ].join("\n");
57
50
  /** 未绑定时占位工具用的空 schema:占位不是为了能用,而是为了让用户看见「有这个工具」。 */
58
51
  const EMPTY_SCHEMA = {};
59
52
  /**
60
- * 注册占位工具:全部工具名照常暴露,调用时统一提示去绑定。
53
+ * 注册占位工具:全部工具名照常暴露,调用时统一返回同一条说明。
61
54
  *
62
- * 为什么不干脆不注册:WorkBuddy 拿不到工具列表时只会显示一个空连接器,
63
- * 用户不知道是没配置、没绑定,还是服务坏了。给出「请先绑定 SEMS」才是可自助的失败。
55
+ * 占位不是为了能用,而是为了让用户看见「有这个工具」:WorkBuddy 拿不到工具列表时
56
+ * 只显示一个空连接器,用户不知道是没配置、没绑定,还是服务坏了。
57
+ * 目前两个场景用它:尚未绑定、服务器端版本过旧。
64
58
  */
65
- function registerUnbound(server, reason) {
66
- // 连接器把服务地址写死在 mcp.json 里,用户只需要一个绑定码,所以这里直接把完整网址给出来
67
- const base = (process.env["WB_PROXY_BASE_URL"] ?? "").replace(/\/+$/, "");
68
- const bindUrl = base ? `${base}/bind` : "「连接服务地址」+ /bind";
59
+ function registerPlaceholders(server, describe, text) {
69
60
  for (const spec of TOOLS) {
70
61
  server.registerTool(spec.name, {
71
62
  title: spec.desc,
72
- description: `⚠️ 尚未绑定海竣信息系统:${reason}`,
63
+ description: `⚠️ ${describe}`,
73
64
  inputSchema: EMPTY_SCHEMA,
74
65
  annotations: { readOnlyHint: !spec.write },
75
66
  }, async () => ({
76
67
  isError: true,
77
- content: [
78
- {
79
- type: "text",
80
- text: `无法调用 ${spec.name}:${reason}\n\n`
81
- + "解决方式:\n"
82
- + `1. 在浏览器打开 ${bindUrl},用系统账号登录、勾选公司,页面会给出一个绑定码;\n`
83
- + "2. 回到 WorkBuddy 重新连接这个连接器,把绑定码填进表单;\n"
84
- + "3. 手工配置 mcp.json 时,可直接补 WB_PROXY_BASE_URL / WB_KEY_ID / WB_SECRET。",
85
- },
86
- ],
68
+ content: [{ type: "text", text: text(spec.name) }],
87
69
  }));
88
70
  }
71
+ }
72
+ /** 没绑定成功:给出绑定页地址,让用户能自助。 */
73
+ function registerUnbound(server, reason) {
74
+ // 连接器把服务地址写死在 mcp.json 里,用户只需要一个绑定码,所以这里直接把完整网址给出来
75
+ const base = (process.env["WB_PROXY_BASE_URL"] ?? "").replace(/\/+$/, "");
76
+ const bindUrl = base ? `${base}/bind` : "「连接服务地址」+ /bind";
77
+ registerPlaceholders(server, `尚未绑定海竣信息系统:${reason}`, (name) => `无法调用 ${name}:${reason}\n\n`
78
+ + "解决方式:\n"
79
+ + `1. 在浏览器打开 ${bindUrl},用系统账号登录、勾选公司,页面会给出一个绑定码;\n`
80
+ + "2. 回到 WorkBuddy 重新连接这个连接器,把绑定码填进表单;\n"
81
+ + "3. 手工配置 mcp.json 时,可直接补 WB_PROXY_BASE_URL / WB_KEY_ID / WB_SECRET。");
89
82
  log.warn(`以「未绑定」模式启动,暴露 ${TOOLS.length} 个占位工具`);
90
83
  }
91
- /** 校验 proxy 契约版本。不匹配时不阻断启动,但要明确告知能力可能不可用。 */
84
+ /**
85
+ * 服务器端版本过旧:本机连接器已不支持它的接口,工具照常列出、调用了才说明原因。
86
+ *
87
+ * 不直接退出的理由同上:工具列表为空时用户只看到一个坏掉的连接器。
88
+ * 提示必须给出「找谁做什么」——终端用户不懂版本号,也够不着服务器。
89
+ */
90
+ function registerUpgradeRequired(server, detail) {
91
+ registerPlaceholders(server, `服务器端版本过旧,请立即升级:${detail}`, (name) => `无法调用 ${name}:${detail}\n\n`
92
+ + "解决办法:让运维升级服务器上的 sems-proxy 后重新连接这个连接器。");
93
+ log.warn(`以「需升级服务端」模式启动:${detail}`);
94
+ }
95
+ /**
96
+ * 校验 proxy 契约版本。返回 null 表示就绪;返回字符串表示不可用的原因(服务器端要升级)。
97
+ *
98
+ * 连不上不阻断:proxy 可能稍后启动,工具列表先给出来更有用,真调用时会拿到明确错误。
99
+ */
92
100
  async function checkContract(config) {
93
101
  try {
94
102
  const health = await fetchHealth(config);
95
103
  if (health.contractVersion !== CONTRACT_VERSION) {
96
- log.warn(`契约版本不匹配:本端 ${CONTRACT_VERSION},proxy ${health.contractVersion}。`
97
- + "请前后端一同升级,否则部分工具可能返回意料之外的结构。");
98
- return;
104
+ const detail = `服务器端契约 v${health.contractVersion},本机连接器需要 v${CONTRACT_VERSION}`;
105
+ log.warn(detail);
106
+ return detail;
99
107
  }
100
108
  // profile / upstream / allow_write 可能不返回:内部诊断由 deploy/proxy/deploy.sh status 查看。
101
- // 这里按需拼接,避免打出 undefined。
102
- const detail = [
109
+ const extra = [
103
110
  health.profile === undefined ? "" : `profile ${health.profile}`,
104
111
  health.upstream === undefined ? "" : `上游 ${health.upstream}`,
105
112
  health.allowWrite === undefined ? "" : `写入 ${health.allowWrite ? "开启" : "关闭"}`,
106
- ].filter(Boolean).join("|");
113
+ ]
114
+ .filter(Boolean)
115
+ .join("|");
107
116
  log.info(`proxy 已连通|版本 ${health.version}|契约 v${health.contractVersion}`
108
- + (detail ? `|${detail}` : ""));
117
+ + (extra ? `|${extra}` : ""));
118
+ return null;
109
119
  }
110
120
  catch (err) {
111
- // 连不上不阻断:proxy 可能稍后启动,而工具列表先给出来更有用
112
121
  log.warn(`proxy /health 探测失败:${err instanceof Error ? err.message : String(err)}`);
122
+ return null;
113
123
  }
114
124
  }
115
125
  async function main() {
@@ -143,21 +153,19 @@ async function main() {
143
153
  withRuleDescriptions(server, agentRules.snapshotText());
144
154
  const ctx = {
145
155
  client,
146
- // 权限预检与业务请求共用同一份签名密钥,没必要建两个 client
147
- guard: new PermissionGuard(client),
148
- userProfile: new UserProfileCache(client),
149
- // 草稿只活在本进程内存里:进程一退就失效,避免旧草稿被当成新单提交
150
- drafts: new DraftStore(),
151
156
  config,
152
157
  agentRules,
158
+ persistActiveTenant,
153
159
  };
154
- registerIdentity(server, ctx);
155
- registerQueries(server, ctx);
156
- registerComposite(server, ctx);
157
- registerWrites(server, ctx);
158
- registerOrchestration(server, ctx);
159
- log.info(`已注册工具 ${TOOLS.length} 个(P0-P3 全部)`);
160
- await checkContract(config);
160
+ // 契约不符时通道端点不存在,注册了只会让每个调用都失败(策略见 contracts/compat.yaml)。
161
+ const blocked = await checkContract(config);
162
+ if (blocked === null) {
163
+ registerChannel(server, ctx);
164
+ log.info(`已注册通道工具 ${TOOLS.length} 个(help / call / prepare / commit / switch_tenant)`);
165
+ }
166
+ else {
167
+ registerUpgradeRequired(server, blocked);
168
+ }
161
169
  }
162
170
  const transport = new StdioServerTransport();
163
171
  await server.connect(transport);