DIFY原生mcp支持SSE和Stream HTTP
首先我们可以看SSE和Stream HTTP的优缺点

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

发现dify报错:

这是我springai的配置文件

在chatbox可以正常调用

发现是因为endpoint设置不规范:
如果你是sse服务,那endpoint设置sse结尾,如果是Stream HTTP那要设置mcp结尾
查看mcp源码可以看出:

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

服务调用成功



浙公网安备 33010602011771号