dify-mcp-client
by: 3dify-project
MCP Client as an Agent Strategy Plugin. Dify is not MCP Server but MCP Host.
📌Overview
Purpose: The MCP Client serves as an agent strategy plugin that enables communication between Dify and MCP servers, facilitating effective tool utilization and resource management.
Overview: Dify's MCP Client transforms the agent's interaction with machine learning models and external resources by leveraging the ReAct framework. It allows for seamless connections to multiple MCP servers, enabling the execution of commands based on the agent's reasoning and observation processes.
Key Features:
-
Multi-server Support: Connects to multiple MCP servers simultaneously, enhancing flexibility and efficiency in executing tasks across different systems.
-
Dify Tools Integration: Converts Tool, Resource, and Prompt lists into accessible Dify Tools for the LLM, enabling an intuitive interface and direct tool invocation during the agent’s operations.
-
Customizable Configuration: Utilizes a JSON-based configuration for server connections, making deployment and updates straightforward.
-
Error Handling & Flexibility: Offers a streamlined installation process and error mitigation strategies, ensuring reliable plugin usage within the Dify ecosystem.
dify-mcp-client
MCP Client
as Agent Strategy Plugin.
Important:
Dify is notMCP Server
butMCP Host
.
How it works
Each MCP client
(ReAct Agent) node can connect to MCP servers
.
Tool
,Resource
, andPrompt
lists are converted into Dify Tools.- Your selected LLM can see their
name
,description
, andargument type
. - The LLM calls Tools based on the ReAct loop (Reason → Act → Observe).
Note:
Most of the code in this repository contains files similar to Dify Official Plugins / Agent Strategies found here:
https://github.com/langgenius/dify-official-plugins/tree/main/agent-strategies/cot_agent
What I did
- Copied
ReAct.py
and renamed the file tomcpReAct.py
. - Added
config_json
GUI input field by editingmcpReAct.yaml
andclass mcpReActParams()
.
Changes in mcpReAct.py
- Added 12 new functions for MCP.
- Implemented
__init__()
to initializeAsyncExitStack
andevent loop
. - Modified
_handle_invoke_action()
with MCP-specific code. - Added MCP setup and cleanup in
_invoke()
.
Important:
The ReAct while loop remains unchanged.
Update history
- Add SSE MCP client (v0.0.2)
- Support multi SSE servers (v0.0.3)
- Update python module and simplify dependencies (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 only connect to reliable MCP servers.
To reduce risk, decreasemax iterations
(default: 3) to 1 and use this Agent node repeatedly in Chatflow.
Note agent memory resets at the end of the Workflow. Use Conversation Variables to save history and pass it to QUERY.
Also, include instructions such as "ask for user's permission when calling tools" in the INSTRUCTION.
How to use this plugin
Install the plugin from GitHub
- Enter the GitHub repository name:
https://github.com/3dify-project/dify-mcp-client/
- In Dify: PLUGINS > + Install plugin > INSTALL FROM > GitHub
Install the plugin from .difypkg
file
- Go to Releases: https://github.com/3dify-project/dify-mcp-client/releases
- Select a suitable version of
.difypkg
. - In Dify: PLUGINS > + Install plugin > INSTALL FROM > Local Package File
Handling plugin installation errors
Issue: If you encounter the error:
plugin verification has been enabled, and the plugin you want to install has a bad signature
Solution:
- Open
/docker/.env
and change:
FORCE_VERIFYING_SIGNATURE=false
- Restart Dify services:
cd docker
docker compose down
docker compose up -d
This disables signature verification, allowing installation of unverified plugins.
Tip:
Marketplace approval may be required. If stars⭐ reach 100, author may consider making a PR for marketplace inclusion.
Where does this plugin show up?
- Takes a few minutes to install.
- Once installed, use it in any workflow as an Agent node.
- Select the "mcpReAct" strategy (otherwise, no MCP functionality).
Configuration
MCP Agent Plugin node requires a config_json
to specify commands or URL(s) 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 unique (e.g., 8080, 8008, etc.).
- To use a stdio MCP server, consider:
- Converting it to an SSE MCP server
- Deploying with source code (not via
.difypkg
or GitHub repository install)- Pre-installing Node.js inside the dify-plugin docker container (see issue #10 in repository)
Chatflow example
- The Tools field should not be blank. Select Dify tools like "current time".
- A sample ChatFlow
.yml
for testing is available in the repository's test/chatflow folder. - After downloading the DSL(yml) file, import it in Dify and test MCP using "Everything MCP server".
- See: https://github.com/modelcontextprotocol/servers/tree/main/src/everything
How to convert stdio
MCP server into SSE MCP server
Option 1: Edit MCP server's code
If using fastMCP server, change:
if __name__ == "__main__":
- mcp.run(transport="stdio")
+ mcp.run(transport="sse")
Option 2: via mcp-proxy
\mcp-proxy> uv venv -p 3.12
.venv\Scripts\activate
uv tool install mcp-proxy
Check Node.js and npx
(.cmd) path:
(Mac/Linux)
which npx
(Windows)
where npx
Example results:
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:
{
"mcpServers": {
"SERVER_NAME": {
"command": CMD_NAME_OR_PATH,
"args": {VALUE1, VALUE2}
}
}
}
Wake up stdio MCP server by:
mcp-proxy --sse-port=8080 --pass-environment -- CMD_NAME_OR_PATH --arg1 VALUE1 --arg2 VALUE2 ...
(On Windows, use npx.cmd
instead of npx
.)
Example 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
Another example using 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
Example 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 development 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, download dify-plugin-windows-amd64.exe
(v0.0.7) from:
https://github.com/langgenius/dify-plugin-daemon/releases
Steps:
- Rename it to
dify.exe
for convenience. - Create folder
C\User\user\.local\bin
(Windows) and register it in system PATH. - Copy
dify.exe
to underdify-mcp-client/
.
Tip:
Helpful guide: 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 code of this repo using:
dify plugin init
Initial settings will be prepared automatically.
Change directory
cd dify-mcp-client
Install Python modules
Python 3.12+ is compatible. Using virtual environments (venv
) and uv
tool is recommended but not necessary.
uv venv -p 3.12
.venv\Scripts\activate
Install python modules required for plugin development:
uv pip install -r requirements.txt
Duplicate env.example
and rename to .env
I changed REMOTE_INSTALL_HOST
from debug.dify.ai
to localhost
for Docker Compose environment.
Activate Dify plugin
python -m main
Use Ctrl+C
to stop the plugin.
Tip:
REMOTE_INSTALL_KEY
in.env
often changes. If you get errors likehandshake failed, invalid key
, renew it.
Package into .difypkg
Default root is ./dify-mcp-client
:
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 pluginrequirements.txt
automatically installs python modules, including the latestmcp
package, so no need to download MCP SDK separately.