Skip to content

Commit 1645a32

Browse files
feat: bing_search
1 parent 04c2824 commit 1645a32

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

backend/service/mcp_client.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: ai-business-hql qingli.hql@alibaba-inc.com
33
Date: 2025-06-16 16:50:17
44
LastEditors: ai-business-hql ai.bussiness.hql@gmail.com
5-
LastEditTime: 2025-10-11 16:32:59
5+
LastEditTime: 2025-10-20 17:28:25
66
FilePath: /comfyui_copilot/backend/service/mcp-client.py
77
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
88
'''
@@ -71,15 +71,31 @@ async def comfyui_agent_invoke(messages: List[Dict[str, Any]], images: List[Imag
7171
raise ValueError("No session_id found in request context")
7272
if not config:
7373
raise ValueError("No config found in request context")
74-
async with MCPServerSse(
74+
75+
# Create MCP server instances
76+
mcp_server = MCPServerSse(
7577
params= {
7678
"url": BACKEND_BASE_URL + "/mcp-server/mcp",
7779
"timeout": 300.0,
7880
"headers": {"X-Session-Id": session_id, "Authorization": f"Bearer {get_comfyui_copilot_api_key()}"}
7981
},
8082
cache_tools_list=True,
8183
client_session_timeout_seconds=300.0
82-
) as server:
84+
)
85+
86+
bing_server = MCPServerSse(
87+
params= {
88+
"url": "https://mcp.api-inference.modelscope.net/8c9fe550938e4f/sse",
89+
"timeout": 300.0,
90+
"headers": {"X-Session-Id": session_id, "Authorization": f"Bearer {get_comfyui_copilot_api_key()}"}
91+
},
92+
cache_tools_list=True,
93+
client_session_timeout_seconds=300.0
94+
)
95+
96+
server_list = [mcp_server, bing_server]
97+
98+
async with mcp_server, bing_server:
8399

84100
# 创建workflow_rewrite_agent实例 (session_id通过context获取)
85101
workflow_rewrite_agent_instance = create_workflow_rewrite_agent()
@@ -121,6 +137,8 @@ async def on_handoff(ctx: RunContextWrapper[None], input_data: HandoffRewriteDat
121137
- [Critical!] When the user's intent is to get workflows or generate images with specific requirements, you MUST ALWAYS call BOTH recall_workflow tool AND gen_workflow tool to provide comprehensive workflow options. Never call just one of these tools - both are required for complete workflow assistance. First call recall_workflow to find existing similar workflows, then call gen_workflow to generate new workflow options.
122138
- When the user's intent is to query, return the query result directly without attempting to assist the user in performing operations.
123139
- When the user's intent is to get prompts for image generation (like Stable Diffusion). Use specific descriptive language with proper weight modifiers (e.g., (word:1.2)), prefer English terms, and separate elements with commas. Include quality terms (high quality, detailed), style specifications (realistic, anime), lighting (cinematic, golden hour), and composition (wide shot, close up) as needed. When appropriate, include negative prompts to exclude unwanted elements. Return words divided by commas directly without any additional text.
140+
- If you cannot find the information needed to answer a query, consider using bing_search to obtain relevant information. For example, if search_node tool cannot find the node, you can use bing_search to obtain relevant information about those nodes or components.
141+
- If search_node tool cannot find the node, you MUST use bing_search to obtain relevant information about those nodes or components.
124142
125143
- **DEBUG Intent** - When the user's intent is to debug workflow execution problems, runtime errors, or troubleshoot failed workflows (keywords: "debug", "调试", "工作流报错", "执行失败", "workflow failed", "node error", "runtime error", "不能运行", "出错了"), respond with: "Please click the 🪲 button in the bottom right corner to trigger debug"
126144
@@ -138,7 +156,7 @@ async def on_handoff(ctx: RunContextWrapper[None], input_data: HandoffRewriteDat
138156
- Reinstalling dependencies
139157
- Alternative approaches if the extension is problematic
140158
""",
141-
mcp_servers=[server],
159+
mcp_servers=server_list,
142160
handoffs=[handoff_rewrite],
143161
config=config
144162
)
@@ -248,9 +266,17 @@ async def process_stream_events(stream_result):
248266

249267
if "text" in tool_output_data and tool_output_data.get('text'):
250268
parsed_output = json.loads(tool_output_data['text'])
251-
answer = parsed_output.get("answer")
252-
data = parsed_output.get("data")
253-
tool_ext = parsed_output.get("ext")
269+
270+
# Handle case where parsed_output might be a list instead of dict
271+
if isinstance(parsed_output, dict):
272+
answer = parsed_output.get("answer")
273+
data = parsed_output.get("data")
274+
tool_ext = parsed_output.get("ext")
275+
else:
276+
# If it's a list or other type, handle gracefully
277+
answer = None
278+
data = parsed_output if isinstance(parsed_output, list) else None
279+
tool_ext = None
254280

255281
# Store tool results similar to reference facade.py
256282
tool_results[tool_name] = {
@@ -264,7 +290,6 @@ async def process_stream_events(stream_result):
264290
# Track workflow tools that produced results
265291
if tool_name in ["recall_workflow", "gen_workflow"]:
266292
log.info(f"-- Workflow tool '{tool_name}' produced result with data: {len(data) if data else 0}")
267-
268293

269294

270295

0 commit comments

Comments
 (0)