fix: return handler errors and panics as tool results (#230)

Fixes https://gitea.com/gitea/gitea-mcp/issues/229

https://gitea.com/gitea/gitea-mcp/pulls/227 left an unexpected handler error and a recovered panic as JSON-RPC errors, which tell the client the request itself failed and kill the session, as in https://gitea.com/gitea/gitea-mcp/issues/229. Both now return tool results.
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/230
Reviewed-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
silverwind
2026-08-09 10:09:14 +00:00
committed by bircni
parent 75f1adf979
commit e81df2d9a0
3 changed files with 43 additions and 18 deletions
+21 -10
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/modelcontextprotocol/go-sdk/jsonrpc"
@@ -72,12 +73,11 @@ func TestMCPHandlerErrorClassification(t *testing.T) {
for _, test := range []struct {
name string
handler Handler
wantCode int64
wantCode int64 // zero when the failure belongs in a tool result rather than a protocol error
}{
{
name: "server error",
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) { return nil, errors.New("failed") },
wantCode: jsonrpc.CodeInternalError,
name: "server error",
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) { return nil, errors.New("failed") },
},
{
name: "protocol error",
@@ -87,17 +87,28 @@ func TestMCPHandlerErrorClassification(t *testing.T) {
wantCode: jsonrpc.CodeInvalidParams,
},
{
name: "panic",
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) { panic("failed") },
wantCode: jsonrpc.CodeInternalError,
name: "panic",
handler: func(context.Context, map[string]any) (*mcp.CallToolResult, error) { panic("failed") },
},
} {
t.Run(test.name, func(t *testing.T) {
result, err := callTool(test.handler, nil)
if result != nil {
t.Errorf("result = %#v, want nil", result)
if test.wantCode != 0 {
if result != nil {
t.Errorf("result = %#v, want nil", result)
}
assertProtocolErrorCode(t, err, test.wantCode)
return
}
if err != nil {
t.Fatalf("MCPHandler() error = %v, want nil", err)
}
if !result.IsError {
t.Error("IsError = false, want true")
}
if content, ok := result.Content[0].(*mcp.TextContent); !ok || !strings.Contains(content.Text, "failed") {
t.Errorf("Content[0] = %#v, want text naming the failure", result.Content[0])
}
assertProtocolErrorCode(t, err, test.wantCode)
})
}
}