Skip to content

Commit 25b6812

Browse files
committed
add RegisterSystem + ActivateProduct tools
1 parent 19e9662 commit 25b6812

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

cmd/suseconnect-mcp/suseconnect_mcp.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@ package main
22

33
import (
44
"context"
5+
"errors"
56
"flag"
7+
"fmt"
68
"log/slog"
79
"net/http"
810

911
"github.com/SUSE/connect-ng/internal/connect"
12+
"github.com/SUSE/connect-ng/pkg/registration"
1013
"github.com/modelcontextprotocol/go-sdk/mcp"
1114
)
1215

1316
type ToolInput struct{}
1417

18+
type RegisterInput struct {
19+
Regcode string `json:"regcode" jsonschema:"The subscription registration code to register the system with"`
20+
Email string `json:"email,omitempty" jsonschema:"Email Address to associate the registration with"`
21+
}
22+
23+
type ActivateInput struct {
24+
Regcode string `json:"regcode,omitempty" jsonschema:"The subscription registration code to register the system with"`
25+
Product string `json:"product" jsonschema:"The product to activate on the system, e.g. 'sle-module-basesystem/15.5/x86_64'. The system needs to be registered first. Available extensions and modules to activate can be found via the ListExtensions tool."`
26+
Email string `json:"email,omitempty" jsonschema:"Email Address to associate the registration with"`
27+
}
28+
1529
type JSONOutput struct {
1630
Response string `json:"response" jsonschema:"the response from the tool"`
1731
Error string `json:"error,omitempty" jsonschema:"the error message if the tool failed"`
@@ -55,6 +69,51 @@ func ListExtensions(ctx context.Context, req *mcp.CallToolRequest, input ToolInp
5569
return nil, JSONOutput{Response: tree}, nil
5670
}
5771

72+
func RegisterSystem(ctx context.Context, req *mcp.CallToolRequest, input RegisterInput) (
73+
*mcp.CallToolResult, JSONOutput, error) {
74+
slog.Info("RegisterSystem tool called")
75+
76+
opts, err := connect.ReadFromConfiguration(connect.DefaultConfigPath)
77+
if err != nil {
78+
return nil, JSONOutput{Error: "Failed to read SUSEConnect configuration"}, err
79+
}
80+
opts.Token = input.Regcode
81+
opts.Email = input.Email
82+
83+
api := connect.NewWrappedAPI(opts)
84+
err = connect.Register(api, opts)
85+
if err != nil {
86+
return nil, JSONOutput{Error: "System registration failed"}, err
87+
}
88+
89+
return nil, JSONOutput{Response: "System successfully registered"}, nil
90+
}
91+
92+
func ActivateProduct(ctx context.Context, req *mcp.CallToolRequest, input ActivateInput) (
93+
*mcp.CallToolResult, JSONOutput, error) {
94+
slog.Info("ActivateProduct tool called")
95+
96+
opts, err := connect.ReadFromConfiguration(connect.DefaultConfigPath)
97+
if err != nil {
98+
return nil, JSONOutput{Error: "Failed to read SUSEConnect configuration"}, err
99+
}
100+
opts.Token = input.Regcode
101+
opts.Email = input.Email
102+
if p, err := registration.FromTriplet(input.Product); err != nil {
103+
return nil, JSONOutput{Error: "Please provide the product identifier in this format: <internal name>/<version>/<architecture>. You can find these values in the ListExtensions tool"}, err
104+
} else {
105+
opts.Product = p
106+
}
107+
108+
api := connect.NewWrappedAPI(opts)
109+
err = connect.Register(api, opts)
110+
if err != nil {
111+
return nil, JSONOutput{Error: "System registration failed"}, err
112+
}
113+
114+
return nil, JSONOutput{Response: "System successfully registered"}, nil
115+
}
116+
58117
func main() {
59118
listenAddr := flag.String("http", "", "address for http transport, defaults to stdio")
60119
flag.Parse()
@@ -69,6 +128,14 @@ func main() {
69128
Name: "ListExtensions",
70129
Description: "List available extension products for your SUSE system. Your system's base product must be activated first.",
71130
}, ListExtensions)
131+
mcp.AddTool(server, &mcp.Tool{
132+
Name: "RegisterSystem",
133+
Description: "Registers and activates your SUSE system. This will enable access to online repositories and additional extensions and modules.",
134+
}, RegisterSystem)
135+
mcp.AddTool(server, &mcp.Tool{
136+
Name: "ActivateProduct",
137+
Description: "Activates and additional extension product or module on your SUSE system. Available extensions can get queried with the ListExtensions tool.",
138+
}, ActivateProduct)
72139

73140
if *listenAddr == "" {
74141
// Run the server on the stdio transport.

0 commit comments

Comments
 (0)