Skip to content

Commit 37ac814

Browse files
authored
introduce NewToolResultErrorWithErr and update docs (#140)
* introduce NewToolResultErrorWithErr and update docs * rename to NewToolResultErrorFromErr
1 parent 3fa49a8 commit 37ac814

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func main() {
163163
result = x * y
164164
case "divide":
165165
if y == 0 {
166-
return nil, errors.New("Cannot divide by zero")
166+
return mcp.NewToolResultError("cannot divide by zero"), nil
167167
}
168168
result = x / y
169169
}
@@ -325,7 +325,7 @@ s.AddTool(calculatorTool, func(ctx context.Context, request mcp.CallToolRequest)
325325
result = x * y
326326
case "divide":
327327
if y == 0 {
328-
return nil, errors.New("Division by zero is not allowed")
328+
return mcp.NewToolResultError("cannot divide by zero"), nil
329329
}
330330
result = x / y
331331
}
@@ -370,20 +370,20 @@ s.AddTool(httpTool, func(ctx context.Context, request mcp.CallToolRequest) (*mcp
370370
req, err = http.NewRequest(method, url, nil)
371371
}
372372
if err != nil {
373-
return nil, fmt.Errorf("Failed to create request: %v", err)
373+
return mcp.NewToolResultErrorFromErr("unable to create request", err), nil
374374
}
375375

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

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

389389
return mcp.NewToolResultText(fmt.Sprintf("Status: %d\nBody: %s", resp.StatusCode, string(respBody))), nil

mcp/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,24 @@ func NewToolResultError(text string) *CallToolResult {
266266
}
267267
}
268268

269+
// NewToolResultErrorFromErr creates a new CallToolResult with an error message.
270+
// If an error is provided, its details will be appended to the text message.
271+
// Any errors that originate from the tool SHOULD be reported inside the result object.
272+
func NewToolResultErrorFromErr(text string, err error) *CallToolResult {
273+
if err != nil {
274+
text = fmt.Sprintf("%s: %v", text, err)
275+
}
276+
return &CallToolResult{
277+
Content: []Content{
278+
TextContent{
279+
Type: "text",
280+
Text: text,
281+
},
282+
},
283+
IsError: true,
284+
}
285+
}
286+
269287
// NewListResourcesResult creates a new ListResourcesResult
270288
func NewListResourcesResult(
271289
resources []Resource,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy