|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/extism/go-pdk" |
| 7 | +) |
| 8 | + |
| 9 | +var ( |
| 10 | + CreateGistTool = ToolDescription{ |
| 11 | + Name: "gh-create-gist", |
| 12 | + Description: "Create a GitHub Gist", |
| 13 | + InputSchema: schema{ |
| 14 | + "type": "object", |
| 15 | + "properties": props{ |
| 16 | + "description": prop("string", "Description of the gist"), |
| 17 | + "files": SchemaProperty{ |
| 18 | + Type: "object", |
| 19 | + Description: "Files contained in the gist.", |
| 20 | + AdditionalProperties: &schema{ |
| 21 | + "type": "object", |
| 22 | + "properties": schema{ |
| 23 | + "content": schema{ |
| 24 | + "type": "string", |
| 25 | + "description": "Content of the file", |
| 26 | + }, |
| 27 | + }, |
| 28 | + "required": []string{"content"}, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + "required": []string{"files"}, |
| 33 | + }, |
| 34 | + } |
| 35 | + GetGistTool = ToolDescription{ |
| 36 | + Name: "gh-get-gist", |
| 37 | + Description: "Gets a specified gist.", |
| 38 | + InputSchema: schema{ |
| 39 | + "type": "object", |
| 40 | + "properties": props{ |
| 41 | + "gist_id": prop("string", "The unique identifier of the gist."), |
| 42 | + }, |
| 43 | + "required": []string{"gist_id"}, |
| 44 | + }, |
| 45 | + } |
| 46 | + UpdateGistTool = ToolDescription{ |
| 47 | + Name: "gh-update-gist", |
| 48 | + Description: "Lists pull requests in a specified repository. Supports different response formats via accept parameter.", |
| 49 | + InputSchema: schema{ |
| 50 | + "type": "object", |
| 51 | + "properties": props{ |
| 52 | + "gist_id": prop("string", "The unique identifier of the gist."), |
| 53 | + "description": prop("string", "Description of the gist"), |
| 54 | + "files": SchemaProperty{ |
| 55 | + Type: "object", |
| 56 | + Description: "Files contained in the gist.", |
| 57 | + AdditionalProperties: &schema{ |
| 58 | + "type": "object", |
| 59 | + "properties": schema{ |
| 60 | + "content": schema{ |
| 61 | + "type": "string", |
| 62 | + "description": "Content of the file", |
| 63 | + }, |
| 64 | + }, |
| 65 | + "required": []string{"content"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + "required": []string{"gist_id"}, |
| 70 | + }, |
| 71 | + } |
| 72 | + DeleteGistTool = ToolDescription{ |
| 73 | + Name: "gh-delete-gist", |
| 74 | + Description: "Delete a specified gist.", |
| 75 | + InputSchema: schema{ |
| 76 | + "type": "object", |
| 77 | + "properties": props{ |
| 78 | + "gist_id": prop("string", "The unique identifier of the gist."), |
| 79 | + }, |
| 80 | + "required": []string{"gist_id"}, |
| 81 | + }, |
| 82 | + } |
| 83 | +) |
| 84 | + |
| 85 | +var GistTools = []ToolDescription{ |
| 86 | + CreateGistTool, |
| 87 | + GetGistTool, |
| 88 | + UpdateGistTool, |
| 89 | + DeleteGistTool, |
| 90 | +} |
| 91 | + |
| 92 | +func gistCreate(apiKey, description string, files map[string]any) CallToolResult { |
| 93 | + url := "https://api.github.com/gists" |
| 94 | + req := pdk.NewHTTPRequest(pdk.MethodPost, url) |
| 95 | + req.SetHeader("Authorization", fmt.Sprintf("token %s", apiKey)) |
| 96 | + req.SetHeader("Content-Type", "application/json") |
| 97 | + req.SetHeader("Accept", "application/vnd.github+json") |
| 98 | + req.SetHeader("User-Agent", "github-mcpx-servlet") |
| 99 | + |
| 100 | + data := map[string]any{ |
| 101 | + "description": description, |
| 102 | + "files": files, |
| 103 | + } |
| 104 | + res, err := json.Marshal(data) |
| 105 | + if err != nil { |
| 106 | + return CallToolResult{ |
| 107 | + IsError: some(true), |
| 108 | + Content: []Content{{ |
| 109 | + Type: ContentTypeText, |
| 110 | + Text: some(fmt.Sprintf("Failed to marshal branch data: %s", err)), |
| 111 | + }}, |
| 112 | + } |
| 113 | + } |
| 114 | + req.SetBody(res) |
| 115 | + resp := req.Send() |
| 116 | + if resp.Status() != 201 { |
| 117 | + return CallToolResult{ |
| 118 | + IsError: some(true), |
| 119 | + Content: []Content{{ |
| 120 | + Type: ContentTypeText, |
| 121 | + Text: some(fmt.Sprintf("Failed to create branch: %d %s", resp.Status(), string(resp.Body()))), |
| 122 | + }}, |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return CallToolResult{ |
| 127 | + Content: []Content{{ |
| 128 | + Type: ContentTypeText, |
| 129 | + Text: some(string(resp.Body())), |
| 130 | + }}, |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func gistUpdate(apiKey, gistId, description string, files map[string]any) CallToolResult { |
| 135 | + url := fmt.Sprintf("https://api.github.com/gists/%s", gistId) |
| 136 | + req := pdk.NewHTTPRequest(pdk.MethodPatch, url) |
| 137 | + req.SetHeader("Authorization", fmt.Sprintf("token %s", apiKey)) |
| 138 | + req.SetHeader("Content-Type", "application/json") |
| 139 | + req.SetHeader("Accept", "application/vnd.github+json") |
| 140 | + req.SetHeader("User-Agent", "github-mcpx-servlet") |
| 141 | + |
| 142 | + data := map[string]any{ |
| 143 | + "description": description, |
| 144 | + "files": files, |
| 145 | + } |
| 146 | + res, err := json.Marshal(data) |
| 147 | + if err != nil { |
| 148 | + return CallToolResult{ |
| 149 | + IsError: some(true), |
| 150 | + Content: []Content{{ |
| 151 | + Type: ContentTypeText, |
| 152 | + Text: some(fmt.Sprintf("Failed to marshal branch data: %s", err)), |
| 153 | + }}, |
| 154 | + } |
| 155 | + } |
| 156 | + req.SetBody(res) |
| 157 | + resp := req.Send() |
| 158 | + if resp.Status() != 201 { |
| 159 | + return CallToolResult{ |
| 160 | + IsError: some(true), |
| 161 | + Content: []Content{{ |
| 162 | + Type: ContentTypeText, |
| 163 | + Text: some(fmt.Sprintf("Failed to create branch: %d %s", resp.Status(), string(resp.Body()))), |
| 164 | + }}, |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + return CallToolResult{ |
| 169 | + Content: []Content{{ |
| 170 | + Type: ContentTypeText, |
| 171 | + Text: some(string(resp.Body())), |
| 172 | + }}, |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func gistGet(apiKey, gistId string) CallToolResult { |
| 177 | + url := fmt.Sprintf("https://api.github.com/gists/%s", gistId) |
| 178 | + req := pdk.NewHTTPRequest(pdk.MethodGet, url) |
| 179 | + req.SetHeader("Authorization", fmt.Sprintf("token %s", apiKey)) |
| 180 | + req.SetHeader("Content-Type", "application/json") |
| 181 | + req.SetHeader("Accept", "application/vnd.github+json") |
| 182 | + req.SetHeader("User-Agent", "github-mcpx-servlet") |
| 183 | + |
| 184 | + resp := req.Send() |
| 185 | + if resp.Status() != 201 { |
| 186 | + return CallToolResult{ |
| 187 | + IsError: some(true), |
| 188 | + Content: []Content{{ |
| 189 | + Type: ContentTypeText, |
| 190 | + Text: some(fmt.Sprintf("Failed to create branch: %d %s", resp.Status(), string(resp.Body()))), |
| 191 | + }}, |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return CallToolResult{ |
| 196 | + Content: []Content{{ |
| 197 | + Type: ContentTypeText, |
| 198 | + Text: some(string(resp.Body())), |
| 199 | + }}, |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +func gistDelete(apiKey, gistId string) CallToolResult { |
| 204 | + url := fmt.Sprintf("https://api.github.com/gists/%s", gistId) |
| 205 | + req := pdk.NewHTTPRequest(pdk.MethodDelete, url) |
| 206 | + req.SetHeader("Authorization", fmt.Sprintf("token %s", apiKey)) |
| 207 | + req.SetHeader("Content-Type", "application/json") |
| 208 | + req.SetHeader("Accept", "application/vnd.github+json") |
| 209 | + req.SetHeader("User-Agent", "github-mcpx-servlet") |
| 210 | + |
| 211 | + resp := req.Send() |
| 212 | + if resp.Status() != 201 { |
| 213 | + return CallToolResult{ |
| 214 | + IsError: some(true), |
| 215 | + Content: []Content{{ |
| 216 | + Type: ContentTypeText, |
| 217 | + Text: some(fmt.Sprintf("Failed to create branch: %d %s", resp.Status(), string(resp.Body()))), |
| 218 | + }}, |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + return CallToolResult{ |
| 223 | + Content: []Content{{ |
| 224 | + Type: ContentTypeText, |
| 225 | + Text: some(string(resp.Body())), |
| 226 | + }}, |
| 227 | + } |
| 228 | +} |
0 commit comments