From 13488f7da1ed8eda47413df3420ab64026696798 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Thu, 15 Jan 2026 11:46:13 -0500 Subject: [PATCH] Export GetError and SetError methods (#753) Exported GetError and SetError. Fixes #64 Co-authored-by: Claude --- mcp/mcp_test.go | 4 ++-- mcp/protocol.go | 10 +++++----- mcp/server.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mcp/mcp_test.go b/mcp/mcp_test.go index e272cb4..ce352ae 100644 --- a/mcp/mcp_test.go +++ b/mcp/mcp_test.go @@ -2124,7 +2124,7 @@ func TestToolErrorMiddleware(t *testing.T) { res, err := h(ctx, method, req) if err == nil { if ctr, ok := res.(*CallToolResult); ok { - middleErr = ctr.getError() + middleErr = ctr.GetError() } } return res, err @@ -2161,7 +2161,7 @@ func TestToolErrorMiddleware(t *testing.T) { t.Fatal("want error, got none") } // Clients can't see the error, because it isn't marshaled. - if err := res.getError(); err != nil { + if err := res.GetError(); err != nil { t.Fatalf("got %v, want nil", err) } if middleErr != errTestFailure { diff --git a/mcp/protocol.go b/mcp/protocol.go index 26c8982..bea776f 100644 --- a/mcp/protocol.go +++ b/mcp/protocol.go @@ -116,17 +116,17 @@ type CallToolResult struct { err error } -// TODO(#64): consider exposing setError (and getError), by adding an error -// field on CallToolResult. -func (r *CallToolResult) setError(err error) { +// SetError sets the error for the tool result and populates the Content field +// with the error text. It also sets IsError to true. +func (r *CallToolResult) SetError(err error) { r.Content = []Content{&TextContent{Text: err.Error()}} r.IsError = true r.err = err } -// getError returns the error set with setError, or nil if none. +// GetError returns the error set with SetError, or nil if none. // This function always returns nil on clients. -func (r *CallToolResult) getError() error { +func (r *CallToolResult) GetError() error { return r.err } diff --git a/mcp/server.go b/mcp/server.go index f24a6a8..1485b88 100644 --- a/mcp/server.go +++ b/mcp/server.go @@ -339,7 +339,7 @@ func toolForErr[In, Out any](t *Tool, h ToolHandlerFor[In, Out]) (*Tool, ToolHan } // For regular errors, embed them in the tool result as per MCP spec var errRes CallToolResult - errRes.setError(err) + errRes.SetError(err) return &errRes, nil }