用 dify零代码开发你的第一个 MCP 服务|全落地指南

大模型向量数据库云通信

Dify 从 1.6.0 版本开始,支持将 AI 应用(例如Agent,Workflow 等)反向发布为 MCP 服务。

这意味着你的应用不仅可以在 Dify 内部使用,还能被 Claude、Cursor 等其他 MCP 客户端直接调用,每个应用都成为可复用的标准化 AI 服务。

picture.image

0-环境情况说明

1-Dify 版本号:最新的 1.7.1,通过 Docker 部署安装。

2-MCP 客户端:Cursor。

1-工作流说明

这里,我以一个对联生成器为案例,做个MCP 发布和 Cursor 配置说明:

这个是配置在 Dify 里的Workflow 工作流。工作流的功能是一句话生成对联,返回对联的代码文件。

输入:主题、上下联字数、选择是否开启一字总结。

picture.image

picture.image

我们先测试一下结果:

picture.image

这个是在 Dify 工作流返回的渲染后的结果:

picture.image

其实,内核就是一堆代码,点击复制,就可以看到如下的内容:

  
<div style="width:600px100%;margin:0pxauto;padding:0px;font-family:'隶书';font-size:2rem;border-radius:12px;text-align:center;">    <divstyle="background-color:red;text-align:center;background-size:95px;background-repeat:repeat-x;margin:0pxauto;padding:0px;display:inline-block;height:45px;">        <spanstyle="margin-left:15px;margin-right:15px;">临福晓春</span>    </div>    <divstyle="position:absolute;top:125px;left:50%;margin:10pxauto;transform:translate(-50%,-50%)rotate(45deg);width:45px;height:45px;background-color:red;background-size:95px;">        <spanstyle="position:absolute;top:0px;left:5px;transform:rotate(135deg);">禧</span>    </div>    <divstyle="min-width:200px;max-width:500px;width:35%;margin:0pxauto;padding:0px;">        <divstyle="background-color:red;background-size:95px;text-align:center;float:left;width:45px;">            福到家门万户欢        </div>        <divstyle="background-color:red;background-size:95px;text-align:center;float:right;width:45px;">            春来晓色千门喜        </div>    </div></div>
2-工作流发布 MCP配置说明

以 Workflow 为例,在发布之前,你需要完成两个简单的配置:

  • 服务描述:清晰说明该 Workflow 的功能和用途,帮助其他 MCP 客户端的 LLM 准确理解并调用你的服务。

  • 参数说明:为 Workflow 开始节点的各项参数添加详细描述,确保 LLM 能够正确传递所需参数。

在工作流编辑区,点击左侧工作工作流配置区域:

picture.image

点击开启 MCP 服务,然后,点击编辑描述。

picture.image

对Workflow 的功能和用途、参数进行明确的说明。

picture.image

然后,返回 MCP 服务区,你就能看到服务端点 URL,点击“复制”。

picture.image

是一串这样的 URL 地址:

http://localhost/mcp/server/8Ps7YZhNaJjT7RJF/mcp

注意,我的 dify 是本地部署的,所以我的 URL 里显示的是 localhost。

3- MCP客户端配置说明

嗯,这一步就是配置使用 MCP 服务了。召唤我的老朋友,Cursor.

打开 Cursor Setting 里的 Tools&Integrations,点击“New MCP Server”

picture.image

然后,你会进入到这样的配置页面:

picture.image

把 MCP 服务器的配置信息拷贝到进来。

  
"duilian\_generate":{      "url":"http://localhost/mcp/server/8Ps7YZhNaJjT7RJF/mcp"    }

特地说明一下,这里的 duilian_generate 就是我们在 Dify 配置的 Workflow 名称。

url 是我们发布 MCP 服务后拿到的 URL 地址。

4-测试 MCP 服务

在 Cursor 对话框输入:

  
调用 duilian\_generate的 mcp 服务,帮我生成一副对联,主题是 春晓,7 个字

picture.image

看到 Cursor 自动识别工具,解析需要填入的三个参数,就知道这把稳了。

picture.image

我们看一下返回的结果:

picture.image

把代码拷贝出来。

  
<div style=\"width:600px100%; margin:0pxauto; padding:0px; font-family: '隶书'; font-size:2rem; border-radius:12px; text-align:center;\">\n    <divstyle=\"background-color:red; text-align:center; background-size:95px; background-repeat:repeat-x; margin:0pxauto; padding:0px; display:inline-block; height:45px;\">\n        <spanstyle=\"margin-left:15px; margin-right:15px;\">间人满春</span>\n    </div>\n    <divstyle=\"position:absolute; top:125px; left:50%; margin:10pxauto; transform:translate(-50%, -50%) rotate(45deg); width:45px; height:45px; background-color:red; background-size:95px;\">\n        <spanstyle=\"position:absolute; top:0px; left:5px; transform:rotate(135deg);\">禧</span>\n    </div>\n    <divstyle=\"min-width:200px; max-width:500px; width:35%; margin:0pxauto; padding:0px;\">\n        <divstyle=\"background-color:red; background-size:95px; text-align:center; float:left; width:45px;\">\n            紫燕衔泥福满门\n        </div>\n        <divstyle=\"background-color:red; background-size:95px; text-align:center; float:right; width:45px;\">\n            东风拂柳春方晓\n        </div>\n    </div>\n</div>

放到随便一个 html 工具预览一下:

picture.image

好的,搞定了,手工。

Dify 集成了 MCP 服务发布能力后,极大的降低了普通开发者开发 AIAgent 的门槛。这对于我们这些普通开发者而言,绝对是时代红利。

您还想了解 Dify 关于 MCP 服务的哪些能力?欢迎评论区留言。

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
火山引擎大规模机器学习平台架构设计与应用实践
围绕数据加速、模型分布式训练框架建设、大规模异构集群调度、模型开发过程标准化等AI工程化实践,全面分享如何以开发者的极致体验为核心,进行机器学习平台的设计与实现。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论