页面加载中...
从插件结构到调试发布,完整走一遍 Dify 插件开发流程——以「汇率查询」插件为例,实操 Dify Plugin SDK。
Dify 插件是 Dify 平台的扩展机制,类似于 VS Code 扩展。每个插件可以为 Dify 的 Agent 和工作流增加新的工具能力。和 MCP Server 不同,Dify 插件运行在 Dify 平台内部,拥有更好的集成体验和权限管理。
# 安装 Dify Plugin CLI pip install dify-plugin-cli # 创建插件项目 dify-plugin init exchange-rate-plugin cd exchange-rate-plugin
项目结构:
exchange-rate-plugin/
├── manifest.yaml # 插件元数据
├── provider/ # 插件供应商定义
│ └── exchange_rate.py
├── tools/ # 工具定义
│ └── get_rate.py
└── _assets/ # 图标等资源
└── icon.svg
# tools/get_rate.yaml identity: name: get_exchange_rate label: en_US: Get Exchange Rate zh_Hans: 获取汇率 description: en_US: Get real-time exchange rate between two currencies zh_Hans: 获取两种货币之间的实时汇率 parameters: - name: from_currency type: string required: true label: en_US: From Currency zh_Hans: 源货币 - name: to_currency type: string required: true label: en_US: To Currency zh_Hans: 目标货币
# tools/get_rate.py import httpx from dify_plugin.entities.tool import ToolInvokeMessage from dify_plugin import Tool class GetExchangeRateTool(Tool): def _invoke(self, parameters: dict) -> ToolInvokeMessage: from_curr = parameters["from_currency"] to_curr = parameters["to_currency"] resp = httpx.get( f"https://api.exchangerate.host/latest", params={"from": from_curr, "to": to_curr} ) data = resp.json() rate = data["rates"][to_curr] return self.create_json_message({ "from": from_curr, "to": to_curr, "rate": rate })
# 在 Dify 本地开发环境运行 dify-plugin run # 在 Dify 控制台测试 # 创建 Agent → 添加工具 → 选择「获取汇率」→ 输入测试参数
# 打包 dify-plugin package # 发布(需要 Dify 开发者账号) dify-plugin publish
identity.description 是 Agent 选择工具的依据,务必清晰准确string、number、boolean、select(枚举)Agent 站点首选部署平台,零配置部署 Next.js,全球 CDN
立即体验 →