Difyclient
    Difyclient

    Difyclient

    MCP Client as an Agent Strategy Plugin. Dify is not MCP Server but MCP Host.

    4.3

    GitHub Stats

    Stars

    139

    Forks

    30

    Release Date

    4/11/2025

    about 2 months ago

    Detailed Description

    dify-mcp-client

    mcp client as agent strategy plugin.

    [!important] dify is not mcp server but mcp host.

    showcase1

    how it works

    each mcp client (react agent) node can connect mcp servers.

    1. tool, resource, prompt lists are converted into dify tools.
    2. your selected llm can see their name, description, argument type
    3. the llm calls tools based on the react loop (reason → act → observe).

    [!note] most of the code in this repository contains the following files.

    dify official plugins / agent strategies

    https://github.com/langgenius/dify-official-plugins/tree/main/agent-strategies/cot_agent

    ✅ what i did

    • copied react.py and renamed file as mcpreact.py
    • added config_json gui input field by editing mcpreact.yaml and class mcpreactparams()

    in mcpreact.py, i added

    • new 12 functions for mcp
    • __init__() for initializing asyncexitstack and event loop
    • some codes in _handle_invoke_action() for mcp
    • mcp setup and cleanup in _invoke()

    [!important] react while loop is as they are

    🔄 update history

    • add sse mcp client (v0.0.2)
    • support multi sse servers (v0.0.3)
    • update python module and simplify its dependency (v0.0.4)
      • mcp(v1.1.2→v1.6.0+)
      • dify_plugin(0.0.1b72→v0.1.0)

    ⚠️ caution and limitation

    [!caution] this plugin does not implement a human-in-the-loop mechanism by default, so connect reliable mcp server only. to avoid it, decrease max itereations(default:3) to 1, and use this agent node repeatedly in chatflow. however, agent memory is reset by the end of workflow. use conversaton variable to save history and pass it to query.
    don't forget to add a phrase such as "ask for user's permission when calling tools" in instruction.

    how to use this plugin

    🛜install the plugin from github

    • enter the following github repository name
    https://github.com/3dify-project/dify-mcp-client/
    
    • dify > plugins > + install plugin > install from > github difyui1

    ⬇️install the plugin from .difypkg file

    • go to releases https://github.com/3dify-project/dify-mcp-client/releases
    • select suitable version of .difypkg
    • dify > plugins > + install plugin > install from > local package file difyui2

    how to handle errors when installing plugins?

    issue: if you encounter the error message: plugin verification has been enabled, and the plugin you want to install has a bad signature, how to handle the issue? solution: open /docker/.env and change from true to false:

    force_verifying_signature=false
    

    run the following commands to restart the dify service:

    cd docker
    docker compose down
    docker compose up -d
    

    once this field is added, the dify platform will allow the installation of all plugins that are not listed (and thus not verified) in the dify marketplace.

    [!tip] marketplace need approval. if stars⭐ reach 100, i'll consider to make pr for them.

    where does this plugin show up?

    • it takes few minutes to install
    • once installed, you can use it any workflows as agent node
    • select "mcpreact" strategy (otherwise no mcp) asagentstrategiesnode

    config

    mcp agent plugin node require config_json like this to command or url to connect mcp servers

    {
        "mcpservers":{
            "name_of_server1":{
                "url": "http://host.docker.internal:8080/sse"
            },
            "name_of_server2":{
                "url": "http://host.docker.internal:8008/sse"
            }
        }
    }
    

    [!warning]

    • each server's port number should be different, like 8080, 8008, ...
    • if you want to use stdio mcp server, there are 3 ways.
      1. convert it to sse mcp server https://github.com/3dify-project/dify-mcp-client/edit/main/readme.md#how-to-convert-stdio-mcp-server-into-sse-mcp-server
      2. deploy with source code (not by .difypkg or github reposity name install) https://github.com/3dify-project/dify-mcp-client/edit/main/readme.md#-how-to-develop-and-deploy-plugin
      3. pre-install node.js inside dify-plugin docker (issue:https://github.com/3dify-project/dify-mcp-client/issues/10) guide: https://github.com/tangyoha/tangyoha-bili/tree/master/dify/mcp/map_mcp

    chatflow example

    showcase2

    [!warning]

    • the tools field should not be left blank. so select dify tools like "current time".

    i provide this dify chatflow .yml for testing this plugin.

    https://github.com/3dify-project/dify-mcp-client/tree/main/test/chatflow

    after download dsl(yml) file, import it in dify and you can test mcp using "everything mcp server"

    https://github.com/modelcontextprotocol/servers/tree/main/src/everything

    how to convert stdio mcp server into sse mcp server

    option1️⃣: edit mcp server's code

    if fastmcp server, change like this

    if __name__ == "__main__":
    -    mcp.run(transport="stdio")
    +    mcp.run(transport="sse")
    

    option2️⃣: via mcp-proxy

    \mcp-proxy>uv venv -p 3.12
    .venv\scripts\activate
    uv tool install mcp-proxy
    

    check node.js has installed and npx(.cmd) path

    (mac/linux)

    which npx
    

    (windows)

    where npx
    

    result

    c:\program files\nodejs\npx
    c:\program files\nodejs\npx.cmd
    c:\users\user_name\appdata\roaming\npm\npx
    c:\users\user_name\appdata\roaming\npm\npx.cmd
    

    if claude_desktop_config.json is following schema,

    {
      "mcpservers": {
        "server_name": {
           "command": cmd_name_or_path 
           "args": {value1, value2}
        }
      }
    }
    

    wake up stdio mcp server by this command

    mcp-proxy --sse-port=8080 --pass-environment -- cmd_name_or_path --arg1 value1 --arg2 value2 ...
    

    if your os is windows, use npx.cmd instead of npx. following is example command to convert stdio "everything mcp server" to sse via mcp-proxy.

    mcp-proxy --sse-port=8080 --pass-environment -- c:\program files\nodejs\npx.cmd --arg1 -y --arg2 @modelcontextprotocol/server-everything
    

    similarly, on another command line (if you use sample chatflow for v0.0.3)

    pip install mcp-simple-arxiv
    mcp-proxy --sse-port=8008 --pass-environment -- c:\users\user_name\appdata\local\programs\python\python310\python.exe -m -mcp_simple_arxiv
    

    following is a mcp-proxy setup log.

    (mcp_proxy) c:\user\user_name\mcp-proxy>mcp-proxy --sse-port=8080 --pass-environment -- c:\program files\nodejs\npx.cmd --arg1 -y --arg2 @modelcontextprotocol/server-everything
    debug:root:starting stdio client and sse server
    debug:asyncio:using proactor: iocpproactor
    debug:mcp.server.lowlevel.server:initializing server 'example-servers/everything'
    debug:mcp.server.sse:sseservertransport initialized with endpoint: /messages/
    info:     started server process [53104]
    info:     waiting for application startup.
    info:     application startup complete.
    info:     uvicorn running on http://127.0.0.1:8080 (press ctrl+c to quit)
    

    🔨 how to develop and deploy plugin

    official plugin dev guide

    https://github.com/3dify-project/dify-mcp-client/blob/main/guide.md

    dify plugin sdk daemon

    if your os is windows and cpu is intel or amd, you need to download dify-plugin-windows-amd64.exe (v0.0.7) choose your os-compatible verson here: https://github.com/langgenius/dify-plugin-daemon/releases

    1. rename it as dify.exe for convinence
    2. mkdir "c\user\user\.local\bin" (windows) and register it as system path.
    3. copy dify.exe to under dify-mcp-client/

    [!tip] following guide is helpful. https://docs.dify.ai/plugins/quick-start/develop-plugins/initialize-development-tools

    reference

    https://docs.dify.ai/plugins/quick-start/develop-plugins/initialize-development-tools

    [!note] you can skip this stage if you pull or download codes of this repo

    dify plugin init
    

    initial settings are as follow initialdifypluginsetting

    change directory

    cd dify-mcp-client
    

    install python module

    python3.12+ is compatible. the venv and uv are not necessary, but recommended.

    uv venv -p 3.12
    .venv\scripts\activate
    

    install python modules for plugin development

    uv pip install -r requirements.txt
    

    duplicate env.example and rename one to .env

    i changed remote_install_host from debug.dify.ai to localhost (docker compose environment) click 🪲bug icon button to see these information

    activate dify plugin

    python -m main
    

    (ctrl+c to stop)

    [!tip] remote_install_key of .env often changes. if you encounter error messages like handshake failed, invalid key, renew it.

    package into .difypkg

    ./dify-mcp-client is my default root name

    dify plugin package ./root_of_your_project
    

    useful github repositories for developers

    dify plugin sdks

    https://github.com/langgenius/dify-plugin-sdks

    mcp python sdk

    https://github.com/modelcontextprotocol/python-sdk

    [!tip] mcp client example https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py

    [!note] dify plugin has requirements.txt which automatically installs python modules. i include latest mcp in it, so you don't need to download the mcp sdk separately.

    Star History

    Star History

    Mar 12Mar 21Mar 30Apr 8Apr 15Apr 22Apr 30May 15May 28Jun 2403570105140
    Powered by MSeeP Analytics

    About the Project

    This app has not been claimed by its owner yet.

    Claim Ownership

    Receive Updates

    Security Updates

    Get notified about trust rating changes

    to receive email notifications.