defineToolPlugin 會建置一個只新增可由代理呼叫工具的外掛:不含
頻道、模型供應商、掛鉤、服務或設定後端。它會產生 OpenClaw 所需的
資訊清單中繼資料,以便在不載入外掛執行階段程式碼的情況下探索工具。
若要建置供應商、頻道、掛鉤、服務或混合功能外掛,請改從
建置外掛、頻道外掛
或供應商外掛開始。
需求
- Node 22.22.3+、Node 24.15+ 或 Node 25.9+。
- TypeScript ESM 套件輸出。
typebox位於dependencies(而不只是devDependencies——產生的 外掛會在執行階段匯入它)。openclaw >=2026.5.17,即第一個匯出openclaw/plugin-sdk/tool-plugin的版本。- 套件根目錄須發布
dist/、openclaw.plugin.json和package.json。
快速開始
plugins init 會建立以下骨架:
npm run plugin:build 會執行 npm run build(tsc),然後執行
openclaw plugins build --entry ./dist/index.js。npm run plugin:validate
會重新建置並執行 openclaw plugins validate --entry ./dist/index.js。
驗證成功時會顯示:
openclaw plugins init <id> 選項:
撰寫工具
defineToolPlugin 接受外掛身分、選用的設定結構描述,以及
靜態工具清單。參數和設定型別會從
TypeBox 結構描述推斷。
選用工具與工廠工具
當使用者應先明確將工具加入允許清單,工具才會傳送給模型時,請設定optional: true。
openclaw plugins build 會寫入相符的
toolMetadata.<tool>.optional 資訊清單項目,讓 OpenClaw 無須載入外掛執行階段程式碼,
也能得知該工具為選用工具。
factory——例如針對特定執行選擇退出、檢查沙箱狀態,或繫結
執行階段輔助函式。即使具體工具是在
執行階段建置,中繼資料仍維持靜態。
definePluginEntry。
傳回值
defineToolPlugin 會將純值傳回結果包裝成 OpenClaw 工具結果
格式:
- 當模型應看到該段完全相同的文字時,傳回字串。
- 當你希望模型看到格式化的 JSON,且 OpenClaw 將原始值保留在
details中時, 傳回與 JSON 相容的值。
AgentToolResult,或想重複使用現有的
api.registerTool 實作時,請使用工廠工具。
輸出合約
當工具會傳回穩定且與 JSON 相容的資料時,請加入outputSchema。它描述的是
儲存在 AgentToolResult.details 中的原始值,而不是
content 中的格式化文字:
details 值,再透過橋接層傳回。
無效的結構描述無法執行工具;結果不符會使已完成的
呼叫失敗。請包含所有不會擲回例外的結果變體,包括結構化錯誤
變體;若結果不穩定,則省略結構描述。請勿在結構描述的說明中放入祕密
或敏感值,因為受信任的輸出中繼資料可能會
對模型可見。
若要取得完整且
精簡的輸出提示,請在物件層使用 { additionalProperties: false };開放或截斷的結構描述仍可透過
tools.describe(...) 使用,但不會被標示為完整的快速索引合約。
工廠工具會在其傳回的具體 AnyAgentTool 上宣告 outputSchema。
靜態 tool({ factory }) 宣告不接受獨立的
輸出結構描述,因為它可能與執行階段工具產生差異。
設定
configSchema 為選用。省略時,OpenClaw 會套用嚴格的空物件
結構描述;產生的資訊清單仍會包含 configSchema。
configSchema,第二個 execute 引數的型別會從中推斷:
已產生的中繼資料
OpenClaw 必須先讀取外掛資訊清單,才能匯入外掛執行階段程式碼。defineToolPlugin 會為此公開靜態中繼資料,而
openclaw plugins build 會將它寫入套件。變更外掛 ID、名稱、說明、設定結構描述、啟用方式或工具
名稱後,請重新執行產生器:
contracts.tools 是重要的探索合約:它會告訴 OpenClaw 每個工具由哪個
外掛擁有,而不必載入所有已安裝外掛的執行階段。過時的
資訊清單可能導致探索時遺漏工具,或將註冊
錯誤歸咎於錯誤的外掛。
套件中繼資料
openclaw plugins build 也會將 package.json 與選定的執行階段
進入點對齊:
./dist/index.js),而非 TypeScript 原始碼進入點。
原始碼進入點只適用於工作區本機開發。
在 CI 中驗證
當產生的中繼資料過時時,plugins build --check 會失敗,但不會重寫檔案:
@deprecated 註解,
編輯器會將其顯示為遷移警告。若要在 CI 中強制執行,請啟用
具型別感知能力的規則,例如
@typescript-eslint/no-deprecated。
Oxlint 不具型別感知能力,因此無法強制執行這些註解。因此,產生的
plugins init 骨架不會新增淘汰項目 lint 設定。
plugins validate 會檢查:
openclaw.plugin.json存在,且可通過一般資訊清單載入器。- 目前的進入點會匯出
defineToolPlugin中繼資料。 - 產生的資訊清單欄位與進入點中繼資料相符。
contracts.tools與宣告的工具名稱相符。package.json會將openclaw.extensions指向所選的執行階段進入點。
在本機安裝並檢查
從另一個 OpenClaw 簽出目錄或已安裝的命令列介面安裝套件路徑:發布
套件準備就緒後,透過 ClawHub 發布。clawhub package publish
接受一個來源:本機資料夾、GitHub 儲存庫(owner/repo[@ref])或
tarball URL。
疑難排解
plugin entry not found: ./dist/index.js
所選的進入點檔案不存在。請執行 npm run build,然後重新執行
openclaw plugins build --entry ./dist/index.js 或
openclaw plugins validate --entry ./dist/index.js。
plugin entry does not expose defineToolPlugin metadata
該進入點未匯出由 defineToolPlugin 建立的值。請確認
模組的預設匯出是 defineToolPlugin(...) 的結果,或透過
--entry 傳入正確的進入點。
openclaw.plugin.json generated metadata is stale
資訊清單已不再與進入點中繼資料相符。請執行:
openclaw.plugin.json 和 package.json 的變更。
package.json openclaw.extensions must include ./dist/index.js
套件中繼資料指向不同的執行階段進入點。請執行
openclaw plugins build --entry ./dist/index.js,讓產生器將
套件中繼資料與你打算發佈的進入點對齊。
Cannot find package 'typebox'
建置後的外掛會在執行階段匯入 typebox。請將它保留在 dependencies 中,
重新安裝、重新建置,並再次執行驗證。
安裝後未顯示工具
請依序檢查下列項目:openclaw plugins inspect <plugin-id> --runtimeopenclaw plugins validate --root <plugin-root> --entry ./dist/index.jsopenclaw.plugin.json具有contracts.tools,且其中包含預期的工具名稱。package.json具有openclaw.extensions: ["./dist/index.js"]。- 安裝外掛後,已重新啟動或重新載入閘道。