From 6a6daf0553182dee690581b70f092c47be8f495d Mon Sep 17 00:00:00 2001 From: Shibin Nambiar Date: Tue, 17 Mar 2026 12:00:15 -0700 Subject: [PATCH] fix(cortex_agent): fix SSE response parsing for Cortex Agent API Two bugs prevented the cortex_agent tool from returning agent responses: 1. `stream: False` in the request payload caused Snowflake to return a plain JSON response instead of SSE. The `parse_agent_response()` parser expects SSE format with `event: response` markers, so it found no events and returned "No final response found." Removing `stream: False` lets the API default to streaming (SSE), which the parser handles correctly. 2. The SSE parser assumed the text response was the last item in the `content` array (`content[-1]`). In practice, the array contains multiple content types (thinking, tool_use, tool_result, text, suggested_queries) and the last item is `suggested_queries`, not `text`. The fix iterates the content array and finds the item with `type == "text"` explicitly. Co-Authored-By: Claude Opus 4.6 (1M context) --- mcp_server_snowflake/cortex_services/tools.py | 1 - mcp_server_snowflake/utils.py | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/mcp_server_snowflake/cortex_services/tools.py b/mcp_server_snowflake/cortex_services/tools.py index bdbaa8b..c9d98a9 100644 --- a/mcp_server_snowflake/cortex_services/tools.py +++ b/mcp_server_snowflake/cortex_services/tools.py @@ -76,7 +76,6 @@ async def query_cortex_agent( payload = { "messages": [{"role": "user", "content": [{"type": "text", "text": query}]}], "tool_choice": {"type": "auto"}, - "stream": False, # Ignored by Agent API } try: response = requests.post( diff --git a/mcp_server_snowflake/utils.py b/mcp_server_snowflake/utils.py index 297a0ee..94d7a53 100644 --- a/mcp_server_snowflake/utils.py +++ b/mcp_server_snowflake/utils.py @@ -309,11 +309,11 @@ def parse_agent_response(self, response_stream: requests.Response) -> str: # If the next line is 'data:', it contains the JSON payload we need json_data = line[len("data: ") :] try: - final_text = ( - json.loads(json_data) - .get("content", [{}])[-1] - .get("text", "No final response found.") - ) + content = json.loads(json_data).get("content", []) + final_text = "No final response found." + for item in content: + if item.get("type") == "text" and "text" in item: + final_text = item["text"] # Return formatted AgentResponse for consistency with other parsers response = AgentResponse(results=final_text) return response.model_dump_json()