beacon-mfg-mcp 1.11.8 → 1.11.10
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 +1 -1
- package/package.json +1 -1
- package/server.py +169 -23
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
| tool | 用途 | 关键参数 |
|
|
11
11
|
|------|------|----------|
|
|
12
12
|
| `search_vendors` | 按 关键词 / 城市 / 国标码 检索,返回精简档案 | `query` `city` `gb` `limit` `offset` |
|
|
13
|
-
| `get_vendor` |
|
|
13
|
+
| `get_vendor` | 取一家或多家的完整中文档案。**看多家务必用 `ids` 一次拿**:单条约 2.3 s(往返受限,数据仅 ~1.4 KB),串行 6 家要 15 s,批量约 4.4 s | `id` 或 `ids`(二选一,`ids` 最多 20 个) `gb`(可选,自动反查) |
|
|
14
14
|
| `get_capability_card` | 按 id 取能力卡(工艺/设备/产能/认证等) | `id`(必填) |
|
|
15
15
|
| `suggest_filters` | **把用户原话映射成候选国标小类码**,供改用 `gb=` | `query`(必填) `city` `limit` |
|
|
16
16
|
| `list_industries` | **国标小类货架**(只列有数据的类,附企业条数),供按语义自己挑码 | `keyword` `parent` `limit` `offset` |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "beacon-mfg-mcp",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.10",
|
|
4
4
|
"description": "Read-only MCP server exposing the BeaconMFG灯塔 directory of business entities across ALL GB/T 4754 industry divisions (manufacturing, services, retail, logistics, catering, IT, and more) to any MCP-compatible agent. No signup and no API key: an anonymous identity is generated on first run, phone binding is optional (only needed for L2 capability cards).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mcp",
|
package/server.py
CHANGED
|
@@ -121,6 +121,16 @@ except Exception: # 副本缺失 → 退化成「不改写 id
|
|
|
121
121
|
# 漏了 "id" 的表现是「检索正常,点开某家报 找不到 id=<token> 对应的国标码」。
|
|
122
122
|
_VID_IN_ARGS = ("id", "vid", "vendor_id", "supplier_id", "company_id", "card_id")
|
|
123
123
|
|
|
124
|
+
# 批量形态的参数名(1.11.10 补)。
|
|
125
|
+
#
|
|
126
|
+
# `get_vendors(ids=[...])` 是 1.11.9 新增的,但**漏进了上面那圈转换**:
|
|
127
|
+
# `_vid_in` 只处理 `isinstance(v, str)`,列表被原样透传。后果实测(本地模式):
|
|
128
|
+
# 单个 id 正常返回档案,而 `ids=[S7YFA2XF, 801SCQ7K, 123XRNG5]` 三条**全部**报
|
|
129
|
+
# 「找不到 id 对应的国标码」——因为 token 直接进了 `get_vendor`,而本地模式的
|
|
130
|
+
# 分片 / idmap / name-index 全是拿**内部 id** 建的键,必然查不到。
|
|
131
|
+
# 症状极具迷惑性:批量接口"能调通、格式正确、就是没数据",容易被当成数据没发布。
|
|
132
|
+
_VID_IN_LIST_ARGS = ("ids",)
|
|
133
|
+
|
|
124
134
|
|
|
125
135
|
def _vid_ready() -> bool:
|
|
126
136
|
return bool(_vid_has_key and _vid_has_key())
|
|
@@ -154,6 +164,12 @@ def _vid_in(args: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
154
164
|
v = out.get(k)
|
|
155
165
|
if isinstance(v, str) and v:
|
|
156
166
|
out[k] = _vid_decode_arg(v, sec)
|
|
167
|
+
# 列表形态(`ids=[...]`)逐个解码;非字符串元素原样保留,不猜类型。
|
|
168
|
+
for k in _VID_IN_LIST_ARGS:
|
|
169
|
+
v = out.get(k)
|
|
170
|
+
if isinstance(v, (list, tuple)):
|
|
171
|
+
out[k] = [_vid_decode_arg(x, sec) if isinstance(x, str) and x else x
|
|
172
|
+
for x in v]
|
|
157
173
|
return out
|
|
158
174
|
|
|
159
175
|
|
|
@@ -322,7 +338,7 @@ def _recall_for_sourcing(text: str, top_k: int = 200, pack_id: str | None = None
|
|
|
322
338
|
# 都对齐到这个值,`scripts/check_version.py` 在发布前校验它们没有漂移。
|
|
323
339
|
# 读不到 package.json 时的兜底值 —— 改版本时**必须**和 package.json 一起改,
|
|
324
340
|
# 否则 check_version.py 会报错(这就是它的用途)。
|
|
325
|
-
VERSION_FALLBACK = "1.11.
|
|
341
|
+
VERSION_FALLBACK = "1.11.10"
|
|
326
342
|
|
|
327
343
|
|
|
328
344
|
def _pkg_version() -> str:
|
|
@@ -4545,6 +4561,14 @@ _VENDOR_CACHE_MAX = int(os.environ.get("BEACON_VENDOR_CACHE_MAX", "1024") or "0"
|
|
|
4545
4561
|
# 等着看「点开的那一家」,通用预算下 26.7s 的构建会原样砸到交互上。
|
|
4546
4562
|
_VENDOR_FP_DEADLINE = float(os.environ.get("BEACON_VENDOR_FP_DEADLINE", "5") or "0")
|
|
4547
4563
|
|
|
4564
|
+
# 批量详情的上限与并发度(2026-10-30)。
|
|
4565
|
+
# 上限 20:一次调用就吃 20 个 /v1/rec,再多会变成"变相全量导出"的入口,
|
|
4566
|
+
# 与「防一次性整体拉走」的设计相冲突。
|
|
4567
|
+
# 并发 6:服务端 worker 同 colo 读 R2 很快,瓶颈是链路 RTT,6 路足够把
|
|
4568
|
+
# 5~20 家压进一个往返量级;再高对 Cloudflare Pages 免费额度不友好。
|
|
4569
|
+
_BATCH_VENDOR_MAX = int(os.environ.get("BEACON_BATCH_VENDOR_MAX", "20") or "20")
|
|
4570
|
+
_BATCH_VENDOR_WORKERS = int(os.environ.get("BEACON_BATCH_VENDOR_WORKERS", "6") or "6")
|
|
4571
|
+
|
|
4548
4572
|
|
|
4549
4573
|
def _vendor_cache_sig() -> float:
|
|
4550
4574
|
m = _wt_mtime("data/manifest.json")
|
|
@@ -4660,32 +4684,141 @@ def _rec_gb(rec: Dict[str, Any]) -> str:
|
|
|
4660
4684
|
return ""
|
|
4661
4685
|
|
|
4662
4686
|
|
|
4687
|
+
def _vendor_fast(vid: str, ck: tuple, sig: float) -> Optional[Dict[str, Any]]:
|
|
4688
|
+
"""详情快路径:缓存 → `/v1/rec` 按条取 → 回写缓存;取不到返回 None。
|
|
4689
|
+
|
|
4690
|
+
为什么抽出来(2026-10-30):批量详情要并发这一段。单条详情稳定在
|
|
4691
|
+
2.3s,而它是**往返受限**的 —— 每次只搬 1.4KB,链路 RTT 占总耗时九成。
|
|
4692
|
+
串行取 5 家就是 11.7s,用户会把「推荐几家」直接当成卡顿。
|
|
4693
|
+
|
|
4694
|
+
一次 HTTP 拿 1 条 ≈ 1.1KB,替代「定国标码 → 拉 2~3 个 zh 续片
|
|
4695
|
+
(5~7MB)→ 解析整片 → 线性扫」。走不通时返回 None,由 `get_vendor`
|
|
4696
|
+
静默回落到原路径 —— 这一步失败**不会**让详情不可用,只会慢回去。
|
|
4697
|
+
"""
|
|
4698
|
+
_hit = _vendor_cache_get(ck, sig)
|
|
4699
|
+
if _hit is not None:
|
|
4700
|
+
return _hit
|
|
4701
|
+
_fast = _v1_rec(vid)
|
|
4702
|
+
if _fast is None:
|
|
4703
|
+
return None
|
|
4704
|
+
_rgb = _rec_gb(_fast)
|
|
4705
|
+
if _rgb and not _fast.get("gb"):
|
|
4706
|
+
# zh 记录常常不写顶层 gb(只留 industry.code),但它确实归档在这个
|
|
4707
|
+
# 国标码下。不回填就会出现「search 能搜到、get_vendor 说 gb=null」。
|
|
4708
|
+
_fast["gb"] = _rgb
|
|
4709
|
+
_fast["gb_source"] = "record"
|
|
4710
|
+
_resp = {"vendor": normalize_vendor(_fast)}
|
|
4711
|
+
_vendor_cache_put(ck, sig, _resp)
|
|
4712
|
+
return _resp
|
|
4713
|
+
|
|
4714
|
+
|
|
4715
|
+
def get_vendors(ids: Any, gb: str = "") -> Dict[str, Any]:
|
|
4716
|
+
"""批量取详情:一次调用拿回 N 家。
|
|
4717
|
+
|
|
4718
|
+
两条泳道,这是关键设计:
|
|
4719
|
+
① **快路径**(缓存 / `/v1/rec`)并发跑。这是绝大多数情况,也是慢的
|
|
4720
|
+
那个 —— 5 家串行 11.7s → 并发约 2.5s。
|
|
4721
|
+
② 快路径没命中的,退回 `get_vendor` 的慢路径,**串行**跑。
|
|
4722
|
+
不能并发:慢路径会构建全量指纹索引(`_build_fp_index`),N 个线程
|
|
4723
|
+
同时撞上去等于把一次重建变成 N 次,内存和出网都会爆。
|
|
4724
|
+
|
|
4725
|
+
`gb` 对整批生效;不传则每条各自反查(更慢,但结果更准)。
|
|
4726
|
+
"""
|
|
4727
|
+
raw = ids
|
|
4728
|
+
if isinstance(raw, str):
|
|
4729
|
+
raw = [raw]
|
|
4730
|
+
if not isinstance(raw, (list, tuple)):
|
|
4731
|
+
return {"error": "参数 ids 需要是字符串数组"}
|
|
4732
|
+
|
|
4733
|
+
# 去重保序:调用方经常把 search 结果的 id 和别处来的 id 混在一起传。
|
|
4734
|
+
seen: set = set()
|
|
4735
|
+
uniq: List[str] = []
|
|
4736
|
+
for _x in raw:
|
|
4737
|
+
_v = str(_x or "").strip()
|
|
4738
|
+
if _v and _v not in seen:
|
|
4739
|
+
seen.add(_v)
|
|
4740
|
+
uniq.append(_v)
|
|
4741
|
+
if not uniq:
|
|
4742
|
+
return {"error": "缺少必填参数 ids(或 id)"}
|
|
4743
|
+
|
|
4744
|
+
dropped = 0
|
|
4745
|
+
if len(uniq) > _BATCH_VENDOR_MAX:
|
|
4746
|
+
dropped = len(uniq) - _BATCH_VENDOR_MAX
|
|
4747
|
+
uniq = uniq[:_BATCH_VENDOR_MAX]
|
|
4748
|
+
|
|
4749
|
+
gb = (gb or "").strip()
|
|
4750
|
+
sig = _vendor_cache_sig()
|
|
4751
|
+
got: Dict[str, Dict[str, Any]] = {}
|
|
4752
|
+
|
|
4753
|
+
if len(uniq) > 1:
|
|
4754
|
+
# 每个任务一份 context 副本:同一 Context 不能并发进入,且
|
|
4755
|
+
# X-Beacon-Op 是靠 ContextVar 传的(缺了服务端按 401 处理)。
|
|
4756
|
+
import contextvars
|
|
4757
|
+
_base = contextvars.copy_context()
|
|
4758
|
+
_n = max(1, min(_BATCH_VENDOR_WORKERS, len(uniq)))
|
|
4759
|
+
with concurrent.futures.ThreadPoolExecutor(max_workers=_n) as ex:
|
|
4760
|
+
futs = {ex.submit(_base.copy().run, _vendor_fast, vid, (vid, gb), sig): vid
|
|
4761
|
+
for vid in uniq}
|
|
4762
|
+
for fut in concurrent.futures.as_completed(futs):
|
|
4763
|
+
_vid = futs[fut]
|
|
4764
|
+
try:
|
|
4765
|
+
_r = fut.result()
|
|
4766
|
+
except Exception:
|
|
4767
|
+
_r = None # 单条炸掉不能拖垮整批
|
|
4768
|
+
if _r is not None:
|
|
4769
|
+
got[_vid] = _r
|
|
4770
|
+
else:
|
|
4771
|
+
_r = _vendor_fast(uniq[0], (uniq[0], gb), sig)
|
|
4772
|
+
if _r is not None:
|
|
4773
|
+
got[uniq[0]] = _r
|
|
4774
|
+
|
|
4775
|
+
vendors: List[Dict[str, Any]] = []
|
|
4776
|
+
failed: List[Dict[str, Any]] = []
|
|
4777
|
+
for vid in uniq:
|
|
4778
|
+
if vid in got:
|
|
4779
|
+
vendors.append(got[vid]["vendor"])
|
|
4780
|
+
continue
|
|
4781
|
+
# 慢路径:串行。注意这里可能很慢(要拉分片),所以只在快路径没命中时走。
|
|
4782
|
+
try:
|
|
4783
|
+
r = get_vendor(vid, gb)
|
|
4784
|
+
except Exception as e: # noqa: BLE001
|
|
4785
|
+
r = {"error": "get_vendor 异常: %s" % e}
|
|
4786
|
+
if isinstance(r, dict) and r.get("vendor"):
|
|
4787
|
+
vendors.append(r["vendor"])
|
|
4788
|
+
else:
|
|
4789
|
+
item: Dict[str, Any] = {"id": vid,
|
|
4790
|
+
"error": (r or {}).get("error") if isinstance(r, dict) else "unknown"}
|
|
4791
|
+
if isinstance(r, dict) and r.get("message"):
|
|
4792
|
+
item["message"] = r["message"]
|
|
4793
|
+
failed.append(item)
|
|
4794
|
+
|
|
4795
|
+
out: Dict[str, Any] = {
|
|
4796
|
+
"vendors": vendors,
|
|
4797
|
+
"count": len(vendors),
|
|
4798
|
+
"requested": len(uniq),
|
|
4799
|
+
}
|
|
4800
|
+
if failed:
|
|
4801
|
+
out["failed"] = failed
|
|
4802
|
+
if dropped:
|
|
4803
|
+
out["truncated"] = dropped
|
|
4804
|
+
out["note"] = ("一次最多取 %d 家,本次多出的 %d 个已忽略;"
|
|
4805
|
+
"请分批调用或先用 search_vendors 缩小范围。"
|
|
4806
|
+
% (_BATCH_VENDOR_MAX, dropped))
|
|
4807
|
+
return out
|
|
4808
|
+
|
|
4809
|
+
|
|
4663
4810
|
def get_vendor(vid: str, gb: str = "") -> Dict[str, Any]:
|
|
4664
4811
|
vid = (vid or "").strip()
|
|
4665
4812
|
if not vid:
|
|
4666
4813
|
return {"error": "缺少必填参数 id"}
|
|
4667
4814
|
gb = (gb or "").strip()
|
|
4668
4815
|
# 命中缓存就直接回,省掉 id 反查 + 分片解析(三次实测均为 970ms 量级)。
|
|
4816
|
+
# ⓿ 缓存 + 按条取(2026-10-30):详情的主路径。逻辑抽到 `_vendor_fast`,
|
|
4817
|
+
# 批量详情(get_vendors)要并发复用同一段;这里失败会返回 None 继续往下走慢路径。
|
|
4669
4818
|
_ck = (vid, gb)
|
|
4670
4819
|
_sig = _vendor_cache_sig()
|
|
4671
|
-
|
|
4672
|
-
if
|
|
4673
|
-
return _hit
|
|
4674
|
-
# ⓿ 按条取(2026-10-30):详情的主路径。
|
|
4675
|
-
#
|
|
4676
|
-
# 一次 HTTP 拿 1 条 ≈ 1.1KB,替代下面「定国标码 → 拉 2~3 个 zh 续片
|
|
4677
|
-
# (5~7MB)→ 解析整片 → 线性扫」。走不通就静默回落到原路径,
|
|
4678
|
-
# 因此这一步失败**不会**让详情不可用,只会慢回去。
|
|
4679
|
-
_fast = _v1_rec(vid)
|
|
4680
|
-
if _fast is not None:
|
|
4681
|
-
_rgb = _rec_gb(_fast)
|
|
4682
|
-
if _rgb and not _fast.get("gb"):
|
|
4683
|
-
# zh 记录常常不写顶层 gb(只留 industry.code),但它确实归档在这个
|
|
4684
|
-
# 国标码下。不回填就会出现「search 能搜到、get_vendor 说 gb=null」。
|
|
4685
|
-
_fast["gb"] = _rgb
|
|
4686
|
-
_fast["gb_source"] = "record"
|
|
4687
|
-
_resp = {"vendor": normalize_vendor(_fast)}
|
|
4688
|
-
_vendor_cache_put(_ck, _sig, _resp)
|
|
4820
|
+
_resp = _vendor_fast(vid, _ck, _sig)
|
|
4821
|
+
if _resp is not None:
|
|
4689
4822
|
return _resp
|
|
4690
4823
|
# 没给国标码就先解析 id -> gb。
|
|
4691
4824
|
#
|
|
@@ -4964,14 +5097,22 @@ TOOLS = [
|
|
|
4964
5097
|
},
|
|
4965
5098
|
{
|
|
4966
5099
|
"name": "get_vendor",
|
|
4967
|
-
"description": "按主体 id
|
|
5100
|
+
"description": "按主体 id 取完整中文档案:名称/地址/经纬度/电话/行业路径/认证/关键词等。"
|
|
5101
|
+
"**取多家请用 ids 一次批量拿**:单条固定约 2.3 秒(往返受限,"
|
|
5102
|
+
"数据本身只有 ~1.4KB),串行取 5 家要 11~12 秒,批量并发后约 2~3 秒。"
|
|
5103
|
+
"典型用法:search_vendors 拿到一批 id 后,把要展示的几家一起传给 ids。",
|
|
4968
5104
|
"inputSchema": {
|
|
4969
5105
|
"type": "object",
|
|
4970
5106
|
"properties": {
|
|
4971
|
-
"id": {"type": "string",
|
|
4972
|
-
|
|
5107
|
+
"id": {"type": "string",
|
|
5108
|
+
"description": "单个主体 id,如 CN-MFG-0000163(与 ids 二选一,只传它时回执是 {\"vendor\": …})"},
|
|
5109
|
+
"ids": {"type": "array", "items": {"type": "string"}, "maxItems": 20,
|
|
5110
|
+
"description": "批量主体 id,最多 20 个;自动去重、按传入顺序返回。"
|
|
5111
|
+
"回执是 {\"vendors\": [...], \"count\": n, \"failed\": [...]},"
|
|
5112
|
+
"单条取不到只进 failed,不影响其余"},
|
|
5113
|
+
"gb": {"type": "string",
|
|
5114
|
+
"description": "国标码(可选);批量时对全部 id 生效。不填会自动反查,稍慢"},
|
|
4973
5115
|
},
|
|
4974
|
-
"required": ["id"],
|
|
4975
5116
|
},
|
|
4976
5117
|
},
|
|
4977
5118
|
{
|
|
@@ -5340,6 +5481,11 @@ def _dispatch_inner(name: str, args: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
5340
5481
|
radius_km=args.get("radius_km"),
|
|
5341
5482
|
)
|
|
5342
5483
|
if name == "get_vendor":
|
|
5484
|
+
# 批量优先:传了 ids 就整批取(并发后总耗时≈一个往返),
|
|
5485
|
+
# 只传 id 仍走单条,回执结构保持 {"vendor": …} 不变(向后兼容)。
|
|
5486
|
+
_ids = args.get("ids")
|
|
5487
|
+
if _ids:
|
|
5488
|
+
return get_vendors(_ids, args.get("gb", ""))
|
|
5343
5489
|
return get_vendor(args.get("id", ""), args.get("gb", ""))
|
|
5344
5490
|
if name == "get_capability_card":
|
|
5345
5491
|
return get_capability_card(args.get("id", ""))
|