|
| 1 | +package confluence |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/google/jsonschema-go/jsonschema" |
| 7 | + goconfluence "github.com/virtomize/confluence-go-api" |
| 8 | + "k8s.io/utils/ptr" |
| 9 | + |
| 10 | + "github.com/containers/kubernetes-mcp-server/pkg/api" |
| 11 | + "github.com/containers/kubernetes-mcp-server/pkg/config" |
| 12 | + "github.com/containers/kubernetes-mcp-server/pkg/kubernetes" |
| 13 | +) |
| 14 | + |
| 15 | +// NewToolset returns a new toolset for Confluence. |
| 16 | +func NewToolset(cfg *config.ConfluenceConfig) (api.Toolset, error) { |
| 17 | + if cfg == nil || cfg.URL == "" { |
| 18 | + return &disabledToolset{}, nil |
| 19 | + } |
| 20 | + |
| 21 | + confluenceAPI, err := goconfluence.NewAPI(cfg.URL, cfg.Username, cfg.Token) |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("failed to create confluence api: %w", err) |
| 24 | + } |
| 25 | + |
| 26 | + return &confluenceToolset{api: confluenceAPI}, nil |
| 27 | +} |
| 28 | + |
| 29 | +type confluenceToolset struct { |
| 30 | + api *goconfluence.API |
| 31 | +} |
| 32 | + |
| 33 | +func (t *confluenceToolset) GetName() string { |
| 34 | + return "confluence" |
| 35 | +} |
| 36 | + |
| 37 | +func (t *confluenceToolset) GetDescription() string { |
| 38 | + return "Tools for interacting with Confluence" |
| 39 | +} |
| 40 | + |
| 41 | +func (t *confluenceToolset) GetTools(_ kubernetes.Openshift) []api.ServerTool { |
| 42 | + if t.api == nil { |
| 43 | + return nil |
| 44 | + } |
| 45 | + return []api.ServerTool{ |
| 46 | + { |
| 47 | + Tool: api.Tool{ |
| 48 | + Name: "confluence.createPage", |
| 49 | + Description: "Create a new page in Confluence.", |
| 50 | + InputSchema: &jsonschema.Schema{ |
| 51 | + Type: "object", |
| 52 | + Properties: map[string]*jsonschema.Schema{ |
| 53 | + "space_key": { |
| 54 | + Type: "string", |
| 55 | + Description: "The key of the space to create the page in.", |
| 56 | + }, |
| 57 | + "title": { |
| 58 | + Type: "string", |
| 59 | + Description: "The title of the new page.", |
| 60 | + }, |
| 61 | + "content": { |
| 62 | + Type: "string", |
| 63 | + Description: "The content of the new page in Confluence Storage Format (XHTML).", |
| 64 | + }, |
| 65 | + "parent_id": { |
| 66 | + Type: "string", |
| 67 | + Description: "Optional ID of a parent page.", |
| 68 | + }, |
| 69 | + }, |
| 70 | + Required: []string{"space_key", "title", "content"}, |
| 71 | + }, |
| 72 | + Annotations: api.ToolAnnotations{ |
| 73 | + DestructiveHint: ptr.To(true), |
| 74 | + }, |
| 75 | + }, |
| 76 | + Handler: createPageHandler(t.api), |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func createPageHandler(confluenceAPI *goconfluence.API) api.ToolHandlerFunc { |
| 82 | + return func(params api.ToolHandlerParams) (*api.ToolCallResult, error) { |
| 83 | + spaceKey, _ := params.GetArguments()["space_key"].(string) |
| 84 | + title, _ := params.GetArguments()["title"].(string) |
| 85 | + content, _ := params.GetArguments()["content"].(string) |
| 86 | + parentID, _ := params.GetArguments()["parent_id"].(string) |
| 87 | + |
| 88 | + pageContent := &goconfluence.Content{ |
| 89 | + Type: "page", |
| 90 | + Title: title, |
| 91 | + Space: &goconfluence.Space{Key: spaceKey}, |
| 92 | + Body: goconfluence.Body{ |
| 93 | + Storage: goconfluence.Storage{ |
| 94 | + Value: content, |
| 95 | + Representation: "storage", |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + if parentID != "" { |
| 101 | + pageContent.Ancestors = []goconfluence.Ancestor{ |
| 102 | + {ID: parentID}, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + createdPage, err := confluenceAPI.CreateContent(pageContent) |
| 107 | + if err != nil { |
| 108 | + return api.NewToolCallResult("", fmt.Errorf("failed to create page: %w", err)), nil |
| 109 | + } |
| 110 | + |
| 111 | + return api.NewToolCallResult(fmt.Sprintf("Page created successfully with ID %s. View it at: %s", createdPage.ID, createdPage.Links.WebUI), nil), nil |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +type disabledToolset struct{} |
| 116 | + |
| 117 | +func (t *disabledToolset) GetName() string { |
| 118 | + return "confluence" |
| 119 | +} |
| 120 | + |
| 121 | +func (t *disabledToolset) GetDescription() string { |
| 122 | + return "Confluence toolset is disabled. Please configure it in the server settings." |
| 123 | +} |
| 124 | + |
| 125 | +func (t *disabledToolset) GetTools(_ kubernetes.Openshift) []api.ServerTool { |
| 126 | + return nil |
| 127 | +} |
0 commit comments