-
Notifications
You must be signed in to change notification settings - Fork 285
improve error handling for tools and server methods #311
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,16 @@ package mcp | |
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "reflect" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/google/jsonschema-go/jsonschema" | ||
| "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2" | ||
| ) | ||
|
|
||
| // testToolHandler is used for type inference in TestNewServerTool. | ||
|
|
@@ -132,3 +136,107 @@ func TestUnmarshalSchema(t *testing.T) { | |
|
|
||
| } | ||
| } | ||
|
|
||
| func TestToolErrorHandling(t *testing.T) { | ||
| // Test that structured JSON-RPC errors are returned directly | ||
| t.Run("structured_error", func(t *testing.T) { | ||
jba marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| server := NewServer(testImpl, nil) | ||
|
|
||
| // Create a tool that returns a structured error | ||
| structuredErrorHandler := func(ctx context.Context, req *ServerRequest[*CallToolParamsFor[map[string]any]]) (*CallToolResultFor[any], error) { | ||
| return nil, &jsonrpc2.WireError{ | ||
| Code: -32603, // ErrInternal | ||
|
||
| Message: "internal server error", | ||
| } | ||
| } | ||
|
|
||
| AddTool(server, &Tool{Name: "error_tool", Description: "returns structured error"}, structuredErrorHandler) | ||
|
|
||
| // Connect and test | ||
| ct, st := NewInMemoryTransports() | ||
| _, err := server.Connect(context.Background(), st, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| client := NewClient(testImpl, nil) | ||
| cs, err := client.Connect(context.Background(), ct, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer cs.Close() | ||
|
|
||
| // Call the tool | ||
| _, err = cs.CallTool(context.Background(), &CallToolParams{ | ||
| Name: "error_tool", | ||
| Arguments: map[string]any{}, | ||
| }) | ||
|
|
||
| // Should get the structured error directly | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
|
|
||
| var wireErr *jsonrpc2.WireError | ||
| if !errors.As(err, &wireErr) { | ||
| t.Fatalf("expected WireError, got %T: %v", err, err) | ||
|
||
| } | ||
|
|
||
| if wireErr.Code != -32603 { | ||
| t.Errorf("expected error code -32603, got %d", wireErr.Code) | ||
| } | ||
| }) | ||
|
|
||
| // Test that regular errors are embedded in tool results | ||
| t.Run("regular_error", func(t *testing.T) { | ||
| server := NewServer(testImpl, nil) | ||
|
|
||
| // Create a tool that returns a regular error | ||
| regularErrorHandler := func(ctx context.Context, req *ServerRequest[*CallToolParamsFor[map[string]any]]) (*CallToolResultFor[any], error) { | ||
| return nil, fmt.Errorf("tool execution failed") | ||
| } | ||
|
|
||
| AddTool(server, &Tool{Name: "regular_error_tool", Description: "returns regular error"}, regularErrorHandler) | ||
|
|
||
| // Connect and test | ||
| ct, st := NewInMemoryTransports() | ||
| _, err := server.Connect(context.Background(), st, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| client := NewClient(testImpl, nil) | ||
| cs, err := client.Connect(context.Background(), ct, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer cs.Close() | ||
|
|
||
| // Call the tool | ||
| result, err := cs.CallTool(context.Background(), &CallToolParams{ | ||
| Name: "regular_error_tool", | ||
| Arguments: map[string]any{}, | ||
| }) | ||
|
|
||
| // Should not get an error at the protocol level | ||
| if err != nil { | ||
| t.Fatalf("unexpected protocol error: %v", err) | ||
| } | ||
|
|
||
| // Should get a result with IsError=true | ||
| if !result.IsError { | ||
| t.Error("expected IsError=true, got false") | ||
| } | ||
|
|
||
| // Should have error message in content | ||
| if len(result.Content) == 0 { | ||
| t.Error("expected error content, got empty") | ||
| } | ||
|
|
||
| if textContent, ok := result.Content[0].(*TextContent); !ok { | ||
| t.Error("expected TextContent") | ||
| } else if !strings.Contains(textContent.Text, "tool execution failed") { | ||
| t.Errorf("expected error message in content, got: %s", textContent.Text) | ||
| } | ||
| }) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.