Skip to content

Commit 19e9662

Browse files
committed
improved error handling
1 parent 2ab9943 commit 19e9662

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

cmd/suseconnect-mcp/suseconnect_mcp.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,44 @@ type ToolInput struct{}
1414

1515
type JSONOutput struct {
1616
Response string `json:"response" jsonschema:"the response from the tool"`
17+
Error string `json:"error,omitempty" jsonschema:"the error message if the tool failed"`
1718
}
1819

19-
func RegistrationStatus(ctx context.Context, req *mcp.CallToolRequest, input ToolInput ) (
20+
func RegistrationStatus(ctx context.Context, req *mcp.CallToolRequest, input ToolInput) (
2021
*mcp.CallToolResult, JSONOutput, error) {
2122
slog.Info("RegistrationStatus tool called")
23+
2224
opts, err := connect.ReadFromConfiguration(connect.DefaultConfigPath)
23-
if err != nil {
24-
return nil, JSONOutput{}, err
25+
if err != nil {
26+
return nil, JSONOutput{Error: "Failed to read SUSEConnect configuration"}, err
2527
}
28+
2629
statuses, err := connect.GetProductStatuses(opts, connect.StatusJSON)
2730
if err != nil {
28-
return nil, JSONOutput{}, err
31+
return nil, JSONOutput{Error: "Failed to retrieve registration status"}, err
2932
}
33+
3034
return nil, JSONOutput{Response: statuses}, nil
3135
}
3236

3337
func ListExtensions(ctx context.Context, req *mcp.CallToolRequest, input ToolInput) (
3438
*mcp.CallToolResult, JSONOutput, error) {
3539
slog.Info("ListExtensions tool called")
40+
3641
opts, err := connect.ReadFromConfiguration(connect.DefaultConfigPath)
3742
if err != nil {
38-
return nil, JSONOutput{}, err
43+
return nil, JSONOutput{Error: "Failed to read SUSEConnect configuration"}, err
3944
}
40-
api := connect.NewWrappedAPI(opts)
45+
46+
api := connect.NewWrappedAPI(opts)
4147
tree, err := connect.RenderExtensionTree(api, true)
4248
if err != nil {
43-
return nil, JSONOutput{}, err
49+
if errors.Is(err, connect.ErrListExtensionsUnregistered) {
50+
return nil, JSONOutput{}, fmt.Errorf("System is not registered; Extension listing requires the system to register first.")
51+
}
52+
return nil, JSONOutput{Response: ""}, fmt.Errorf("Failed to list extensions: %w", err)
4453
}
54+
4555
return nil, JSONOutput{Response: tree}, nil
4656
}
4757

@@ -50,7 +60,7 @@ func main() {
5060
flag.Parse()
5161

5262
server := mcp.NewServer(&mcp.Implementation{Name: "suseconnect", Version: "v0.0.1"}, nil)
53-
63+
5464
mcp.AddTool(server, &mcp.Tool{
5565
Name: "RegistrationStatus",
5666
Description: "Tool to output the registration status of the system and activated/non-activated installed products",
@@ -77,4 +87,4 @@ func main() {
7787
slog.Error("Server failed", "error", err)
7888
}
7989
}
80-
}
90+
}

0 commit comments

Comments
 (0)