DIFY原生mcp支持SSE和Stream HTTP

首先我们可以看SSE和Stream HTTP的优缺点

image

使用dify原生mcp添加mcp服务:

image

发现dify报错:

image

这是我springai的配置文件

image

在chatbox可以正常调用

image

发现是因为endpoint设置不规范:

如果你是sse服务,那endpoint设置sse结尾,如果是Stream HTTP那要设置mcp结尾

查看mcp源码可以看出:

image

def _initialize(
        self,
    ):
        """Initialize the client with fallback to SSE if streamable connection fails"""
        connection_methods: dict[str, Callable[..., AbstractContextManager[Any]]] = {
            "mcp": streamablehttp_client,
            "sse": sse_client,
        }

        parsed_url = urlparse(self.server_url)
        path = parsed_url.path or ""
        method_name = path.rstrip("/").split("/")[-1] if path else ""
        if method_name in connection_methods:
            client_factory = connection_methods[method_name]
            self.connect_server(client_factory, method_name)
        else:
            try:
                logger.debug("Not supported method %s found in URL path, trying default 'mcp' method.", method_name)
                self.connect_server(sse_client, "sse")
            except MCPConnectionError:
                logger.debug("MCP connection failed with 'sse', falling back to 'mcp' method.")
                self.connect_server(streamablehttp_client, "mcp") 

修改endpoint

image

服务调用成功

image
image

posted @ 2025-11-05 14:34  748573200000  阅读(100)  评论(0)    收藏  举报