agent: Check is_error flag in MCP tool responses (#47095)

Xiaobo Liu and Ben Brandt created

Previously, when an MCP server returned a tool response with
`is_error: true`, the error content was incorrectly treated as
a successful result. This could mislead the LLM into thinking
the tool call succeeded when it actually failed.

Now we check the `is_error` flag and propagate the error message
properly, allowing the agent to handle failures appropriately.

Release Notes:

- N/A

---------

Co-authored-by: Ben Brandt <benjamin.j.brandt@gmail.com>

Change summary

crates/agent/src/tools/context_server_registry.rs | 6 ++++++
1 file changed, 6 insertions(+)

Detailed changes

crates/agent/src/tools/context_server_registry.rs 🔗

@@ -380,6 +380,12 @@ impl AnyAgentTool for ContextServerTool {
                 }
             };
 
+            if response.is_error == Some(true) {
+                let error_message: String =
+                    response.content.iter().filter_map(|c| c.text()).collect();
+                anyhow::bail!(error_message);
+            }
+
             let mut result = String::new();
             for content in response.content {
                 match content {