-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
482 lines (416 loc) · 13.1 KB
/
main.go
File metadata and controls
482 lines (416 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package main
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
var (
version = "dev"
// Update this URL to point to the latest release of github-mcp-server
releaseAPIURL = "https://api.github.com/repos/github/github-mcp-server/releases/latest"
)
func main() {
// Show version if requested
if len(os.Args) > 1 && (os.Args[1] == "-v" || os.Args[1] == "--version") {
fmt.Printf("gh-github-mcp-server v%s\n", version)
return
}
// Check if command is "stdio"
if len(os.Args) > 1 && os.Args[1] == "stdio" {
// Get GitHub token from CLI
token, err := getGitHubToken()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting GitHub token: %s\n", err)
os.Exit(1)
}
// Set the token as an environment variable
os.Setenv("GITHUB_PERSONAL_ACCESS_TOKEN", token)
// Find or download the server binary
serverPath, err := ensureServerBinary()
if err != nil {
fmt.Fprintf(os.Stderr, "Error with server binary: %s\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "Using server binary: %s\n", serverPath)
// Execute the actual MCP server with stdio mode
cmd := exec.Command(serverPath, "stdio")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Forward all arguments after "stdio"
if len(os.Args) > 2 {
cmd.Args = append(cmd.Args, os.Args[2:]...)
}
// Run the server
if err := cmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Error running MCP server: %s\n", err)
os.Exit(1)
}
} else {
// Show usage
fmt.Println("GitHub MCP Server CLI Extension")
fmt.Println()
fmt.Println("USAGE:")
fmt.Println(" gh github-mcp-server stdio [flags] - Start the MCP server in stdio mode")
fmt.Println()
fmt.Println("FLAGS:")
fmt.Println(" --read-only Restrict the server to read-only operations")
fmt.Println(" --log-file string Path to log file")
fmt.Println(" --gh-host string Specify the GitHub hostname (for GitHub Enterprise)")
fmt.Println()
fmt.Println("This extension uses your GitHub CLI authentication")
fmt.Println("to securely communicate with GitHub APIs.")
}
}
// getGitHubToken gets the GitHub token from the GitHub CLI
func getGitHubToken() (string, error) {
// Execute the GitHub CLI to get the token directly
cmd := exec.Command("gh", "auth", "token")
output, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
return "", fmt.Errorf("failed to get GitHub token: %s", string(exitErr.Stderr))
}
return "", fmt.Errorf("failed to execute gh auth token: %w", err)
}
// Trim any whitespace/newlines
token := strings.TrimSpace(string(output))
if token == "" {
return "", fmt.Errorf("received empty token from GitHub CLI")
}
return token, nil
}
// ensureServerBinary finds or downloads the server binary
func ensureServerBinary() (string, error) {
// Try to find the binary first
binPath := findServerBinary()
if binPath != "" {
return binPath, nil
}
fmt.Fprintf(os.Stderr, "GitHub MCP Server binary not found, downloading...\n")
// Binary not found, download it
binPath, err := downloadServerBinary()
if err != nil {
return "", fmt.Errorf("failed to download server binary: %w", err)
}
return binPath, nil
}
// findServerBinary locates the github-mcp-server binary
func findServerBinary() string {
// Check if a specific path is provided via environment variable
customPath := os.Getenv("GITHUB_MCP_SERVER_PATH")
if customPath != "" && fileExists(customPath) {
return customPath
}
// Check if workspace directory is specified
workspaceDir := os.Getenv("GITHUB_MCP_SERVER_DIR")
if workspaceDir != "" {
binPath := filepath.Join(workspaceDir, "bin", binaryName())
if fileExists(binPath) {
return binPath
}
}
// Check in the extension's data directory
dataDir := getExtensionDataDir()
binPath := filepath.Join(dataDir, "bin", binaryName())
if fileExists(binPath) {
return binPath
}
// Check standard locations
execPath, err := os.Executable()
if err == nil {
dir := filepath.Dir(execPath)
// Check for the binary in the 'bin' subdirectory
binPath := filepath.Join(dir, "bin", binaryName())
if fileExists(binPath) {
return binPath
}
// Check next to the extension binary
binPath = filepath.Join(dir, binaryName())
if fileExists(binPath) {
return binPath
}
}
// Finally check if it's available in PATH
path, lookErr := exec.LookPath("github-mcp-server")
if lookErr == nil {
return path
}
return ""
}
// downloadServerBinary downloads the latest release of the server binary
func downloadServerBinary() (string, error) {
// Get information about the latest release
assetURL, err := getLatestReleaseAssetURL()
if err != nil {
return "", err
}
// Create the data directory if it doesn't exist
dataDir := getExtensionDataDir()
binDir := filepath.Join(dataDir, "bin")
err = os.MkdirAll(binDir, 0755)
if err != nil {
return "", fmt.Errorf("failed to create binary directory: %w", err)
}
// Download the asset
fmt.Fprintf(os.Stderr, "Downloading from: %s\n", assetURL)
resp, err := http.Get(assetURL)
if err != nil {
return "", fmt.Errorf("failed to download binary: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to download binary: HTTP %d", resp.StatusCode)
}
// Create a temporary file
tmpFile, err := os.CreateTemp("", "github-mcp-server-*")
if err != nil {
return "", fmt.Errorf("failed to create temporary file: %w", err)
}
defer os.Remove(tmpFile.Name())
// Save the downloaded file
_, err = io.Copy(tmpFile, resp.Body)
tmpFile.Close()
if err != nil {
return "", fmt.Errorf("failed to save downloaded file: %w", err)
}
// Extract the binary
targetPath := filepath.Join(binDir, binaryName())
if strings.HasSuffix(assetURL, ".zip") {
err = extractFromZip(tmpFile.Name(), targetPath)
} else if strings.HasSuffix(assetURL, ".tar.gz") {
err = extractFromTarGz(tmpFile.Name(), targetPath)
} else {
// Direct binary download
err = os.Rename(tmpFile.Name(), targetPath)
}
if err != nil {
return "", fmt.Errorf("failed to extract binary: %w", err)
}
// Make the binary executable
err = os.Chmod(targetPath, 0755)
if err != nil {
return "", fmt.Errorf("failed to make binary executable: %w", err)
}
return targetPath, nil
}
// getLatestReleaseAssetURL gets the URL for the appropriate asset from the latest release
func getLatestReleaseAssetURL() (string, error) {
// Add a custom user agent
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest("GET", releaseAPIURL, nil)
if err != nil {
return "", err
}
req.Header.Set("User-Agent", "gh-github-mcp-server/"+version)
// Use token if available for higher rate limits
token, _ := getGitHubToken()
if token != "" {
req.Header.Set("Authorization", "token "+token)
}
// Make the request
fmt.Fprintf(os.Stderr, "Requesting latest release info from: %s\n", releaseAPIURL)
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("failed to get latest release info: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return "", fmt.Errorf("failed to get latest release info: HTTP %d - %s",
resp.StatusCode, string(body))
}
// Parse the response
var release struct {
Assets []struct {
Name string `json:"name"`
BrowserDownloadURL string `json:"browser_download_url"`
} `json:"assets"`
TagName string `json:"tag_name"`
}
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", fmt.Errorf("failed to parse release info: %w", err)
}
// Print all assets for debugging
fmt.Fprintf(os.Stderr, "Found release with %d assets:\n", len(release.Assets))
for _, asset := range release.Assets {
fmt.Fprintf(os.Stderr, " - %s\n", asset.Name)
}
// Find the appropriate asset for the current platform
assetPattern := fmt.Sprintf("github-mcp-server.*%s.*%s", runtime.GOOS, runtime.GOARCH)
fmt.Fprintf(os.Stderr, "Looking for pattern: %s\n", assetPattern)
// Try standard naming convention
for _, asset := range release.Assets {
lowerName := strings.ToLower(asset.Name)
if strings.Contains(lowerName, strings.ToLower(runtime.GOOS)) &&
strings.Contains(lowerName, strings.ToLower(runtime.GOARCH)) {
fmt.Fprintf(os.Stderr, "Found matching asset: %s\n", asset.Name)
return asset.BrowserDownloadURL, nil
}
}
// Try macOS alternatives (mac, macos, osx)
if runtime.GOOS == "darwin" {
fmt.Fprintf(os.Stderr, "Trying macOS alternatives: mac, macos, osx\n")
macVariants := []string{"mac", "macos", "osx"}
for _, variant := range macVariants {
for _, asset := range release.Assets {
lowerName := strings.ToLower(asset.Name)
if strings.Contains(lowerName, variant) &&
(strings.Contains(lowerName, runtime.GOARCH) ||
strings.Contains(lowerName, "x86_64") ||
strings.Contains(lowerName, "amd64")) {
fmt.Fprintf(os.Stderr, "Found matching asset with macOS alternative: %s\n", asset.Name)
return asset.BrowserDownloadURL, nil
}
}
}
}
// Try architecture alternatives
if runtime.GOARCH == "amd64" {
fmt.Fprintf(os.Stderr, "Trying architecture alternatives: x86_64\n")
for _, asset := range release.Assets {
lowerName := strings.ToLower(asset.Name)
if strings.Contains(lowerName, strings.ToLower(runtime.GOOS)) &&
strings.Contains(lowerName, "x86_64") {
fmt.Fprintf(os.Stderr, "Found matching asset with architecture alternative: %s\n", asset.Name)
return asset.BrowserDownloadURL, nil
}
}
}
// As a last resort, look for a potentially universal binary for the OS
fmt.Fprintf(os.Stderr, "Looking for any binary for %s\n", runtime.GOOS)
for _, asset := range release.Assets {
lowerName := strings.ToLower(asset.Name)
if strings.Contains(lowerName, strings.ToLower(runtime.GOOS)) &&
!strings.Contains(lowerName, ".sha") &&
!strings.Contains(lowerName, ".md5") &&
!strings.Contains(lowerName, "src") &&
!strings.Contains(lowerName, "source") {
fmt.Fprintf(os.Stderr, "Found OS-matching asset: %s\n", asset.Name)
return asset.BrowserDownloadURL, nil
}
}
// If this is macOS/amd64, maybe suggest building from source
if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" {
return "", fmt.Errorf("no suitable binary found for %s/%s. Consider building from source: "+
"go build -o bin/github-mcp-server ./cmd/github-mcp-server", runtime.GOOS, runtime.GOARCH)
}
return "", fmt.Errorf("no suitable binary found for %s/%s", runtime.GOOS, runtime.GOARCH)
}
// extractFromZip extracts a binary from a zip file
func extractFromZip(zipPath, targetPath string) error {
reader, err := zip.OpenReader(zipPath)
if err != nil {
return err
}
defer reader.Close()
// Find the binary file in the zip
var binaryFile *zip.File
for _, file := range reader.File {
if strings.Contains(file.Name, "github-mcp-server") &&
!strings.HasSuffix(file.Name, "/") {
binaryFile = file
break
}
}
if binaryFile == nil {
return fmt.Errorf("binary not found in zip")
}
// Extract the file
src, err := binaryFile.Open()
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(targetPath)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
return err
}
// extractFromTarGz extracts a binary from a tar.gz file
func extractFromTarGz(tarPath, targetPath string) error {
file, err := os.Open(tarPath)
if err != nil {
return err
}
defer file.Close()
gzr, err := gzip.NewReader(file)
if err != nil {
return err
}
defer gzr.Close()
tr := tar.NewReader(gzr)
// Find and extract the binary
for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if strings.Contains(header.Name, "github-mcp-server") &&
header.Typeflag == tar.TypeReg {
dst, err := os.Create(targetPath)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, tr)
return err
}
}
return fmt.Errorf("binary not found in tar.gz")
}
// getExtensionDataDir returns the data directory for this extension
func getExtensionDataDir() string {
var dataDir string
if xdgDataHome := os.Getenv("XDG_DATA_HOME"); xdgDataHome != "" {
dataDir = filepath.Join(xdgDataHome, "gh-github-mcp-server")
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
// Fallback to current directory
return "."
}
if runtime.GOOS == "windows" {
dataDir = filepath.Join(homeDir, "AppData", "Local", "gh-github-mcp-server")
} else if runtime.GOOS == "darwin" {
dataDir = filepath.Join(homeDir, "Library", "Application Support", "gh-github-mcp-server")
} else {
dataDir = filepath.Join(homeDir, ".local", "share", "gh-github-mcp-server")
}
}
// Create the directory if it doesn't exist
_ = os.MkdirAll(dataDir, 0755)
return dataDir
}
// binaryName returns the name of the binary with platform-specific extension
func binaryName() string {
baseName := "github-mcp-server"
if runtime.GOOS == "windows" {
return baseName + ".exe"
}
return baseName
}
// fileExists checks if a file exists and is not a directory
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if err != nil {
return false
}
return !info.IsDir()
}