Skip to content

我如何通过python代码来使用这个项目访问我的SSE协议的MCP接口 #120

@haohuisss

Description

@haohuisss

Please confirm before submission 在提交之前,请确认

Dify version Dify版本

1.9.1

Plugin version 插件版本

0.2.3

HTTP with SSE or Streamable HTTP

HTTP with SSE

Problem description 问题描述

由于目前dify不支持工具的流式输出,所以我决定使用这个项目,来通过python代码构建一个调用MCP的接口,从而实现流式的输出。
但是目前项目中的mcp_call_tool.py在执行mcp的调用过程中,任然是使用非流的方式调用的;
请我,我应该怎么来调用这个项目,从而实现MCP的流式输出。

代码:
tool_parameters = {
"tool_name": "server_name1",
"arguments" : "{"query": "推荐菜"}"
}
servers_config = {
"server_name1": {
"transport": "sse",
"url": "http://x.x.x.x:5002/e/g1it8pcp7f52ui1n/sse",
"headers": {},
"timeout": 500,
"sse_read_timeout": 500
}
}
tool_name = tool_parameters.get("tool_name", "")
if not tool_name:
raise ValueError("Please fill in the tool_name")
arguments_json = tool_parameters.get("arguments", "")
if not arguments_json:
raise ValueError("Please fill in the arguments")
try:
arguments = json.loads(arguments_json)
except json.JSONDecodeError as e:
raise ValueError(f"Arguments must be a valid JSON string: {e}")

resources_as_tools = False
prompts_as_tools = False
mcp_clients = McpClients(servers_config, resources_as_tools, prompts_as_tools)
content = mcp_clients.execute_tool(tool_name, arguments)
print(content)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions