Skip to content

introduce NewToolResultErrorWithErr and update docs #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func main() {
result = x * y
case "divide":
if y == 0 {
return nil, errors.New("Cannot divide by zero")
return mcp.NewToolResultError("cannot divide by zero"), nil
}
result = x / y
}
Expand Down Expand Up @@ -325,7 +325,7 @@ s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest)
result = x * y
case "divide":
if y == 0 {
return nil, errors.New("Division by zero is not allowed")
return mcp.NewToolResultError("cannot divide by zero"), nil
}
result = x / y
}
Expand Down Expand Up @@ -370,20 +370,20 @@ s.AddTool(httpTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp
req, err = http.NewRequest(method, url, nil)
}
if err != nil {
return nil, fmt.Errorf("Failed to create request: %v", err)
return mcp.NewToolResultErrorFromErr("unable to create request", err), nil
}

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Request failed: %v", err)
return mcp.NewToolResultErrorFromErr("unable to execute request", err), nil
}
defer resp.Body.Close()

// Return response
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read response: %v", err)
return mcp.NewToolResultErrorFromErr("unable to read request response", err), nil
}

return mcp.NewToolResultText(fmt.Sprintf("Status: %d\nBody: %s", resp.StatusCode, string(respBody))), nil
Expand Down
18 changes: 18 additions & 0 deletions mcp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ func NewToolResultError(text string) *CallToolResult {
}
}

// NewToolResultErrorFromErr creates a new CallToolResult with an error message.
// If an error is provided, its details will be appended to the text message.
// Any errors that originate from the tool SHOULD be reported inside the result object.
func NewToolResultErrorFromErr(text string, err error) *CallToolResult {
if err != nil {
text = fmt.Sprintf("%s: %v", text, err)
}
return &CallToolResult{
Content: []Content{
TextContent{
Type: "text",
Text: text,
},
},
IsError: true,
}
}

// NewListResourcesResult creates a new ListResourcesResult
func NewListResourcesResult(
resources []Resource,
Expand Down