Skip to content

Commit 67dabdf

Browse files
committed
send errors back to LLM
1 parent 82b50fb commit 67dabdf

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

internal/agent/agent.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package agent
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
"github.com/cloudwego/eino/components/model"
89
"github.com/cloudwego/eino/components/tool"
910
"github.com/cloudwego/eino/schema"
11+
"github.com/mark3labs/mcp-go/mcp"
1012
"github.com/mark3labs/mcphost/internal/config"
1113
"github.com/mark3labs/mcphost/internal/models"
1214
"github.com/mark3labs/mcphost/internal/tools"
@@ -154,11 +156,20 @@ func (a *Agent) GenerateWithLoop(ctx context.Context, messages []*schema.Message
154156
onToolResult(toolCall.Function.Name, toolCall.Function.Arguments, errorMsg, true)
155157
}
156158
} else {
159+
// Check if this is an MCP tool response with an error
160+
isError := false
161+
if output != "" {
162+
var mcpResult mcp.CallToolResult
163+
if err := json.Unmarshal([]byte(output), &mcpResult); err == nil && mcpResult.IsError {
164+
isError = true
165+
}
166+
}
167+
157168
toolMessage := schema.ToolMessage(output, toolCall.ID)
158169
workingMessages = append(workingMessages, toolMessage)
159170

160171
if onToolResult != nil {
161-
onToolResult(toolCall.Function.Name, toolCall.Function.Arguments, output, false)
172+
onToolResult(toolCall.Function.Name, toolCall.Function.Arguments, output, isError)
162173
}
163174
}
164175
} else {

internal/tools/mcp.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,11 @@ func (t *mcpToolImpl) InvokableRun(ctx context.Context, argumentsInJSON string,
154154
if err != nil {
155155
return "", fmt.Errorf("failed to marshal mcp tool result: %w", err)
156156
}
157-
if result.IsError {
158-
return "", fmt.Errorf("failed to call mcp tool, mcp server return error: %s", marshaledResult)
159-
}
157+
158+
// If the MCP server returned an error, we still return the error content as the response
159+
// to the LLM so it can see what went wrong. The error will be shown to the user via
160+
// the UI callbacks, but the LLM needs to see the actual error details to continue
161+
// the conversation appropriately.
160162
return marshaledResult, nil
161163
}
162164

0 commit comments

Comments
 (0)