feat(mcp): library-map M4 — kbdb_get_map 工具+instructions 注入全館地圖 #73
Reference in New Issue
Block a user
Delete Branch "feat/mcp-library-map-inject"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
對應 library-map SDD M4(
system-dev/docs/3-specs/library-map/design.md§4「注入機制」)。關聯 #39。與 #68(kbdb_graph_neighbors)同族薄殼。內容(design §4 兩件)
1. MCP tool
kbdb_get_map(mcp/src/tools/kbdb_map.ts)library參數=該庫詳圖:完整欄位;top_entities/relation_profile/bridges若為 JSON 字串形(live 曾觀測的 slot 直出形)容錯JSON.parse成物件,parse 失敗當空陣列不 crash;triplet_count字串轉數字POST /map/recompute?library=backfill(含過渡期source_prefix用法)kbdbFetch,與 kbdb_data.ts 同款)——kbdb base 本來就有GET /map/GET /map/:library(M2 已 merge),不需繞 cypher proxy。不碰 D1、不新增 binding、D17kbdb_*前綴2. server instructions 注入(
mcp/src/lib/library-map.ts+mcp/src/mcp-handler.ts)GET /map,渲染成每庫一行:{library}:{narrative}|核心:{top3}|{triplet_count} triplets,嵌入 instructions(design §6:session 啟動 push 零查詢)。總量控制:narrative 截 60 字、庫數上限 30 行(數百 token 內,R3)sessionIdGenerator: undefined),每個 HTTP request 都重建 McpServer——「每次現拉」實際上是每個 tool call 都多打一次 /map,不只 initialize;地圖只在 ingest recompute 後才變,5 分鐘 staleness 無害。isolate 回收即自然失效測試(誠實回報)
pnpm vitest run:76/76 過(新增mcp/tests/unit/tools/kbdb-map.test.ts17 測,假 KBDB binding 比照 kbdb-graph.test.ts 手法:全館/詳圖路徑與 auth header、JSON 字串 slot 容錯、404/空庫誠實訊息、instructions 失敗靜默、快取命中不重打)tsc --noEmit:乾淨https://arcrun-kbdb.leo21c.workers.dev/map(leo21c backfill 後)核實部署
merge 後需 gated redeploy arcrun-mcp(leo 閘)。純 code 變更,無 wrangler.toml/binding/migration 改動。
關聯 #39。
總管審過 merge。一項 D21「記錄不阻擋」留檔:instructions 注入的 module 快取為全域、/map 未帶 owner 過濾——MULTI_TENANT=true 部署時=跨租戶把全館地圖注入所有人 instructions。現行單租戶(D21 唯一目標)無虞;未來多租戶啟用前必須:快取按 namespace 分 key+/map 帶 owner 過濾。其餘:注入失敗路徑 null-safe 不擋連線 ✅、timeout 1.5s ✅、工具 owner_id 透傳 ✅、76/76 綠。