npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dify-mcp-server

v0.0.1

Published

MCP Server for Dify workflow integration

Downloads

17

Readme

Dify MCP Server

一个将 Dify 工作流转换为 MCP (Model Context Protocol) 服务的 Node.js 包,支持在 Cursor 等 AI 开发环境中使用。

功能特性

  • ✅ 将 Dify 工作流转换为 MCP 工具
  • ✅ 支持阻塞和流式两种执行模式
  • ✅ 完整的配置管理和环境变量支持
  • ✅ TypeScript 开发,提供完整的类型定义
  • ✅ 支持 MCP 协议的工具调用和资源访问
  • ✅ 详细的错误处理和日志记录
  • ✅ 可打包为 npm 包发布

快速开始

1. 安装依赖

npm install

2. 配置环境变量

复制 .env.example 文件为 .env 并配置你的 Dify 信息:

cp .env.example .env

编辑 .env 文件:

# Dify 配置
DIFY_API_KEY=your_dify_api_key_here
DIFY_BASE_URL=https://api.dify.ai/v1
DIFY_APP_ID=your_dify_app_id_here

# MCP 服务器配置
MCP_SERVER_PORT=3000
MCP_SERVER_HOST=localhost

# 日志配置
LOG_LEVEL=info

# 工作流配置
WORKFLOW_TIMEOUT=30000
MAX_RETRY_ATTEMPTS=3

3. 构建项目

npm run build

4. 启动服务器

npm start

或者开发模式:

npm run dev

项目结构

src/
├── config/           # 配置管理
│   └── index.ts      # 环境变量和配置类
├── types/            # TypeScript 类型定义
│   └── index.ts      # Dify API 和 MCP 相关类型
├── services/         # 业务逻辑服务
│   └── difyService.ts # Dify API 服务
├── mcp/              # MCP 协议实现
│   └── server.ts     # MCP 服务器类
└── index.ts          # 应用程序入口

MCP 工具

可用工具

  1. execute_workflow - 执行 Dify 工作流

    • 输入参数: inputs (对象), mode (blocking|streaming), user (可选)
    • 支持阻塞和流式两种执行模式
  2. get_workflow_status - 获取工作流任务状态

    • 输入参数: taskId (字符串)
  3. validate_inputs - 验证输入参数

    • 输入参数: inputs (对象)
  4. get_service_status - 获取服务状态

    • 输入参数: 无

可用资源

  1. dify://config - Dify 配置信息
  2. dify://tools - 可用工具列表

在 Cursor 中使用

1. 安装 MCP 服务器

npm install -g dify-mcp-server

2. 配置 Cursor

在 Cursor 的 MCP 配置中添加:

{
  "mcpServers": {
    "dify": {
      "command": "dify-mcp-server",
      "env": {
        "DIFY_API_KEY": "your_dify_api_key",
        "DIFY_APP_ID": "your_dify_app_id"
      }
    }
  }
}

3. 使用示例

在 Cursor 中,你可以直接调用 Dify 工作流:

// 执行工作流
const result = await execute_workflow({
  inputs: {
    question: "什么是人工智能?",
    temperature: 0.7
  },
  mode: "blocking"
});

// 获取工作流状态
const status = await get_workflow_status({
  taskId: "task_123"
});

API 文档

DifyService

主要服务类,负责与 Dify API 交互:

const service = new DifyService();

// 连接测试
await service.connect();

// 执行工作流(阻塞模式)
const result = await service.executeWorkflowBlocking(inputs, user);

// 执行工作流(流式模式)
await service.executeWorkflowStreaming(inputs, user, onData, onError, onComplete);

// 获取工作流状态
const status = await service.getWorkflowStatus(taskId);

Config

配置管理类:

import config from './config';

// 获取配置实例
const config = Config.getInstance();

// 验证配置
if (config.validate()) {
  console.log('配置有效');
}

// 获取配置摘要
console.log(config.getSummary());

开发指南

添加新的 MCP 工具

  1. src/mcp/server.tsdefineTools() 方法中添加工具定义
  2. setupHandlers() 方法中添加工具处理逻辑
  3. 在相应的服务类中实现业务逻辑

自定义工作流输入验证

DifyService.validateInputs() 方法中添加自定义验证规则:

validateInputs(inputs: Record<string, any>): { isValid: boolean; errors: string[] } {
  const errors: string[] = [];

  // 示例:验证必需字段
  if (!inputs.question) {
    errors.push('question 字段是必需的');
  }

  // 示例:验证数据类型
  if (inputs.temperature && typeof inputs.temperature !== 'number') {
    errors.push('temperature 必须是数字');
  }

  return { isValid: errors.length === 0, errors };
}

部署和发布

构建发布包

npm run build
npm pack

发布到 npm

npm login
npm publish

Docker 部署

创建 Dockerfile

FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install --only=production

COPY dist/ ./dist/
COPY .env.example ./

EXPOSE 3000

CMD ["node", "dist/index.js"]

构建和运行:

docker build -t dify-mcp-server .
docker run -p 3000:3000 --env-file .env dify-mcp-server

故障排除

常见问题

  1. 连接 Dify 失败

    • 检查 DIFY_API_KEYDIFY_APP_ID 是否正确
    • 验证网络连接和防火墙设置
  2. 工作流执行失败

    • 检查输入参数格式
    • 查看 Dify 控制台的错误日志
  3. MCP 协议错误

    • 确保 MCP 客户端版本兼容
    • 检查服务器日志获取详细错误信息

调试模式

设置环境变量启用详细日志:

LOG_LEVEL=debug

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

更新日志

v1.0.0

  • 初始版本发布
  • 支持基本的 Dify 工作流执行
  • 完整的 MCP 协议实现
  • TypeScript 支持和类型定义