fix: isolate startup tools and preview dialogs

This commit is contained in:
Xin Wang
2026-08-02 00:07:35 +08:00
parent 0331f8cd07
commit 479a516546
10 changed files with 202 additions and 49 deletions

View File

@@ -121,19 +121,10 @@ async def _validate_startup_actions(
actions = body.startup.actions
if not actions:
return
bound_tool_ids = set(body.tool_ids)
for action in actions:
if action.tool_id not in bound_tool_ids:
raise HTTPException(400, f"启动 Action 必须先绑定工具:{action.tool_id}")
tool = await session.get(Tool, action.tool_id)
if not tool or tool.status != "active":
raise HTTPException(400, f"启动 Action 引用了无效工具:{action.tool_id}")
if action.phase == "preflight" and tool.type not in {"http", "mcp"}:
raise HTTPException(400, "preflight Action 仅支持 HTTP 或 MCP 工具")
if action.phase == "opening" and tool.type not in {"http", "mcp", "client"}:
raise HTTPException(400, "opening Action 不支持该工具类型")
if tool.type != "client":
continue
runtime_tool = RuntimeTool(
id=tool.id,
name=tool.name,
@@ -141,10 +132,20 @@ async def _validate_startup_actions(
type=tool.type,
definition=tool.definition or {},
)
if action.phase == "preflight" and runtime_tool.type not in {"http", "mcp"}:
raise HTTPException(400, "preflight Action 仅支持 HTTP 或 MCP 工具")
if action.phase == "opening" and runtime_tool.type not in {
"http",
"mcp",
"client",
}:
raise HTTPException(400, "opening Action 不支持该工具类型")
if runtime_tool.type != "client":
continue
policy = policy_for_tool(runtime_tool)
if action.required and not policy.wait_for_response:
raise HTTPException(400, "必需的 Client 启动 Action 必须等待客户端响应")
if tool.function_name != "show_message":
if runtime_tool.function_name != "show_message":
continue
if policy.response_wait_mode != "session":
raise HTTPException(400, "show_message 启动 Action 必须使用会话内等待")