Files
Arcrun/mcp/src/tools/arcrun_search_components.ts
T
Leo 1b687fedb0 fix(mcp): 解掉三個把人推去寫零件的誤導入口(leo 2026-07-21 拍板)
leo:「刪掉,技術者才會寫 component,在 Arcrun repo 寫一條如何 contribute
指向另一個 repo 就好。」

實測病灶:總管想寫「定期打 API 然後通知」的 workflow(Python 約 10 行),
問 foreach_control 怎麼用 → MCP 回傳 TinyGo 寫 WASM 零件教學(白名單/syscall/
contract schema)=完全另一件事,40 分鐘未完成。

三處修正:
1. search_components 搜不到時的話術——原本建議 publish_component(把「我找不到」
   翻譯成「你去造一個」,方向完全相反)。改為導向正確順序:語意搜尋知識庫→
   auth-recipe list/scaffold→acr parts(http_request 能打任意 API)→acr list,
   並明說 registry 可能是空的(已知問題),搜不到≠沒有這能力。
2. registry.ts 停用 publish_component / get_component_guide 兩個註冊
   (檔案保留,只是不對 AI 暴露)。
3. 新增 CONTRIBUTING-components.md:三層責任分工(平台通用能力/熱門預鋪 recipe/
   冷門誰用到誰開發)、什麼時候才真需要新零件、真要貢獻走 PR 的流程。

typecheck 通過。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 16:42:51 +08:00

81 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { toolName } from "../brand.js";
import { z } from "zod";
import { Env } from "../types.js";
/**
* arcrun_search_components — 語意搜尋零件庫
* 呼叫 Component Registry GET /components/search?q=...
*/
export function registerSearchComponents(server: McpServer, env: Env, orgNamespace: string) {
server.tool(
toolName("search_components"),
"用自然語言語意搜尋零件庫,找出符合需求的零件。例如:「查詢 Google Sheets 資料」、「發送 LINE 訊息」、「驗證 JSON 格式」。回傳零件清單含 canonical_id、描述、評分。",
{
query: z.string().describe("自然語言搜尋詞,如「查詢 Google Sheets 資料」"),
},
async ({ query }) => {
try {
if (!env.COMPONENT_REGISTRY) {
return {
content: [{ type: "text", text: "Error: COMPONENT_REGISTRY service binding is not configured." }],
isError: true,
};
}
const response = await env.COMPONENT_REGISTRY.fetch(
`http://component-registry/components/search?q=${encodeURIComponent(query)}`,
{ method: "GET", headers: { "Content-Type": "application/json" } },
);
if (!response.ok) {
const errorText = await response.text();
return {
content: [{ type: "text", text: `Search failed: ${errorText}` }],
isError: true,
};
}
const result = await response.json() as { data?: { results?: unknown[]; count?: number } };
const results = result.data?.results ?? [];
const count = result.data?.count ?? 0;
if (count === 0) {
return {
content: [{
type: "text",
text: [
`registry 裡找不到「${query}」——但**先別急著造零件**(零件走 PR、專業等級)。`,
``,
`⚠️ registry 目前可能是空的(已知問題:唯一寫入觸發點隨 .github/workflows 移除而蒸發),`,
` 「搜不到」不代表「沒有這個能力」。請改用下列順序找:`,
``,
`1. **語意搜尋知識庫**(最強,能找到你沒猜中的用詞):`,
` kbdb_search(q="我想達成什麼(用一句話描述)", mode="semantic")`,
`2. **看現成的服務整合**\`acr auth-recipe list\`26 個:GitHub/Notion/Gemini…)`,
` 要用哪個 → \`acr auth-recipe scaffold <服務>\` 直接吐 credentials 範本+workflow 範例`,
`3. **看零件全集**\`acr parts\`21 顆通用零件,含 http_request 可打任意 API`,
`4. **看有沒有現成 workflow 可直接接**\`acr list\``,
``,
`💡 多數需求的正解是「用 recipe 設定既有零件」或「接既有 workflow」,不是新造零件。`,
].join("\n"),
}],
};
}
return {
content: [{
type: "text",
text: `找到 ${count} 個零件:\n${JSON.stringify(results, null, 2)}`,
}],
};
} catch (error) {
return {
content: [{ type: "text", text: `Internal Error: ${error instanceof Error ? error.message : String(error)}` }],
isError: true,
};
}
}
);
}