|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + |
| 11 | + "github.com/mcpjungle/mcpjungle/internal/model" |
| 12 | + "github.com/mcpjungle/mcpjungle/pkg/types" |
| 13 | +) |
| 14 | + |
| 15 | +// ListPrompts retrieves all prompts or prompts filtered by server name |
| 16 | +func (c *Client) ListPrompts(serverName string) ([]model.Prompt, error) { |
| 17 | + u, err := c.constructAPIEndpoint("/prompts") |
| 18 | + if err != nil { |
| 19 | + return nil, fmt.Errorf("failed to construct API endpoint: %w", err) |
| 20 | + } |
| 21 | + |
| 22 | + // Add server filter if specified |
| 23 | + if serverName != "" { |
| 24 | + parsed, _ := url.Parse(u) |
| 25 | + q := parsed.Query() |
| 26 | + q.Set("server", serverName) |
| 27 | + parsed.RawQuery = q.Encode() |
| 28 | + u = parsed.String() |
| 29 | + } |
| 30 | + |
| 31 | + req, err := c.newRequest(http.MethodGet, u, nil) |
| 32 | + if err != nil { |
| 33 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 34 | + } |
| 35 | + |
| 36 | + resp, err := c.httpClient.Do(req) |
| 37 | + if err != nil { |
| 38 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 39 | + } |
| 40 | + defer resp.Body.Close() |
| 41 | + |
| 42 | + if resp.StatusCode != http.StatusOK { |
| 43 | + return nil, c.parseErrorResponse(resp) |
| 44 | + } |
| 45 | + |
| 46 | + var prompts []model.Prompt |
| 47 | + if err := json.NewDecoder(resp.Body).Decode(&prompts); err != nil { |
| 48 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + return prompts, nil |
| 52 | +} |
| 53 | + |
| 54 | +// GetPrompt retrieves a specific prompt by name |
| 55 | +func (c *Client) GetPrompt(name string) (*model.Prompt, error) { |
| 56 | + u, err := c.constructAPIEndpoint("/prompt") |
| 57 | + if err != nil { |
| 58 | + return nil, fmt.Errorf("failed to construct API endpoint: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Add name as query parameter |
| 62 | + parsed, _ := url.Parse(u) |
| 63 | + q := parsed.Query() |
| 64 | + q.Set("name", name) |
| 65 | + parsed.RawQuery = q.Encode() |
| 66 | + u = parsed.String() |
| 67 | + |
| 68 | + req, err := c.newRequest(http.MethodGet, u, nil) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + resp, err := c.httpClient.Do(req) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 76 | + } |
| 77 | + defer resp.Body.Close() |
| 78 | + |
| 79 | + if resp.StatusCode != http.StatusOK { |
| 80 | + return nil, c.parseErrorResponse(resp) |
| 81 | + } |
| 82 | + |
| 83 | + var prompt model.Prompt |
| 84 | + if err := json.NewDecoder(resp.Body).Decode(&prompt); err != nil { |
| 85 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + return &prompt, nil |
| 89 | +} |
| 90 | + |
| 91 | +// GetPromptWithArgs retrieves a prompt with arguments and returns the rendered template |
| 92 | +func (c *Client) GetPromptWithArgs(name string, arguments map[string]string) (*types.PromptResult, error) { |
| 93 | + u, err := c.constructAPIEndpoint("/prompts/render") |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("failed to construct API endpoint: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + request := types.PromptGetRequest{ |
| 99 | + Name: name, |
| 100 | + Arguments: arguments, |
| 101 | + } |
| 102 | + |
| 103 | + body, err := json.Marshal(request) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + req, err := c.newRequest(http.MethodPost, u, bytes.NewBuffer(body)) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 111 | + } |
| 112 | + req.Header.Set("Content-Type", "application/json") |
| 113 | + |
| 114 | + resp, err := c.httpClient.Do(req) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 117 | + } |
| 118 | + defer resp.Body.Close() |
| 119 | + |
| 120 | + if resp.StatusCode != http.StatusOK { |
| 121 | + return nil, c.parseErrorResponse(resp) |
| 122 | + } |
| 123 | + |
| 124 | + var result types.PromptResult |
| 125 | + if err := json.NewDecoder(resp.Body).Decode(&result); err != nil { |
| 126 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 127 | + } |
| 128 | + |
| 129 | + return &result, nil |
| 130 | +} |
| 131 | + |
| 132 | +// EnablePrompts enables one or more prompts |
| 133 | +func (c *Client) EnablePrompts(entity string) ([]string, error) { |
| 134 | + return c.setPromptsEnabled(entity, true) |
| 135 | +} |
| 136 | + |
| 137 | +// DisablePrompts disables one or more prompts |
| 138 | +func (c *Client) DisablePrompts(entity string) ([]string, error) { |
| 139 | + return c.setPromptsEnabled(entity, false) |
| 140 | +} |
| 141 | + |
| 142 | +// setPromptsEnabled is a helper function to enable or disable prompts |
| 143 | +func (c *Client) setPromptsEnabled(entity string, enabled bool) ([]string, error) { |
| 144 | + var endpoint string |
| 145 | + if enabled { |
| 146 | + endpoint = "/prompts/enable" |
| 147 | + } else { |
| 148 | + endpoint = "/prompts/disable" |
| 149 | + } |
| 150 | + |
| 151 | + u, err := c.constructAPIEndpoint(endpoint) |
| 152 | + if err != nil { |
| 153 | + return nil, fmt.Errorf("failed to construct API endpoint: %w", err) |
| 154 | + } |
| 155 | + |
| 156 | + // Add entity as query parameter |
| 157 | + parsed, _ := url.Parse(u) |
| 158 | + q := parsed.Query() |
| 159 | + q.Set("entity", entity) |
| 160 | + parsed.RawQuery = q.Encode() |
| 161 | + u = parsed.String() |
| 162 | + |
| 163 | + req, err := c.newRequest(http.MethodPost, u, nil) |
| 164 | + if err != nil { |
| 165 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + resp, err := c.httpClient.Do(req) |
| 169 | + if err != nil { |
| 170 | + return nil, fmt.Errorf("failed to send request: %w", err) |
| 171 | + } |
| 172 | + defer resp.Body.Close() |
| 173 | + |
| 174 | + if resp.StatusCode != http.StatusOK { |
| 175 | + body, _ := io.ReadAll(resp.Body) |
| 176 | + action := "enable" |
| 177 | + if !enabled { |
| 178 | + action = "disable" |
| 179 | + } |
| 180 | + return nil, fmt.Errorf("failed to %s prompts: status %d, message: %s", action, resp.StatusCode, body) |
| 181 | + } |
| 182 | + |
| 183 | + var promptNames []string |
| 184 | + if err := json.NewDecoder(resp.Body).Decode(&promptNames); err != nil { |
| 185 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 186 | + } |
| 187 | + |
| 188 | + return promptNames, nil |
| 189 | +} |
0 commit comments