harvesthealth commited on
Commit
99f4cdb
·
verified ·
1 Parent(s): 1a7256b

Fix: Corrected import path in main.go

Browse files

Updated import path from github.com/mark3labs/mcp-go to github.com/mark3labs/mcp-go/mcp to fix build issue

Files changed (1) hide show
  1. cmd/github-mcp-server/main.go +104 -104
cmd/github-mcp-server/main.go CHANGED
@@ -1,114 +1,114 @@
1
  package main
2
 
3
  import (
4
- main.go "bytes"
5
- main.go "context"
6
- main.go "fmt"
7
- main.go "io"
8
- main.go "log"
9
- main.go "net/http"
10
- main.go "os"
11
- main.go "os/exec"
12
- main.go "time"
13
-
14
- main.go "github.com/mark3labs/mcp-go/mcp" // Fixed import path
15
- main.go "github.com/mark3labs/mcp-go/mcp/protocol"
16
- main.go "github.com/spf13/viper"
17
  )
18
 
19
  const (
20
- main.go defaultPort = "8080"
21
- main.go defaultTimeoutSeconds = 60
22
  )
23
 
24
  func main() {
25
- main.go // Initialize configuration
26
- main.go viper.SetDefault("port", defaultPort)
27
- main.go viper.SetDefault("timeout_seconds", defaultTimeoutSeconds)
28
- main.go viper.AutomaticEnv()
29
-
30
- main.go port := viper.GetString("port")
31
- main.go timeoutSeconds := viper.GetInt("timeout_seconds")
32
-
33
- main.go // Create HTTP server
34
- main.go httpServer := &http.Server{
35
- main.go main.go Addr: ":" + port,
36
- main.go main.go Handler: nil, // Will be set later
37
- main.go main.go ReadTimeout: 5 * time.Second,
38
- main.go main.go WriteTimeout: 10 * time.Second,
39
- main.go }
40
-
41
- main.go // Create a context with timeout
42
- main.go ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds)*time.Second)
43
- main.go defer cancel()
44
-
45
- main.go // Start the server
46
- main.go log.Printf("Starting server on port %s with timeout %d seconds", port, timeoutSeconds)
47
-
48
- main.go // Handle JSON-RPC requests
49
- main.go http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
50
- main.go main.go // Set CORS headers
51
- main.go main.go w.Header().Set("Access-Control-Allow-Origin", "*")
52
- main.go main.go w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
53
- main.go main.go w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
54
-
55
- main.go main.go // Handle preflight requests
56
- main.go main.go if r.Method == "OPTIONS" {
57
- main.go main.go main.go w.WriteHeader(http.StatusOK)
58
- main.go main.go main.go return
59
- main.go main.go }
60
-
61
- main.go main.go // Only handle POST requests
62
- main.go main.go if r.Method != "POST" {
63
- main.go main.go main.go http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
64
- main.go main.go main.go return
65
- main.go main.go }
66
-
67
- main.go main.go // Read the request body
68
- main.go main.go body, err := io.ReadAll(r.Body)
69
- main.go main.go if err != nil {
70
- main.go main.go main.go http.Error(w, "Error reading request body", http.StatusBadRequest)
71
- main.go main.go main.go return
72
- main.go main.go }
73
- main.go main.go defer r.Body.Close()
74
-
75
- main.go main.go // Log the incoming request
76
- main.go main.go log.Printf("Received request: %s", string(body))
77
-
78
- main.go main.go // Create a subprocess to handle the request
79
- main.go main.go cmd := exec.CommandContext(ctx, "/usr/local/bin/github-mcp-server")
80
- main.go main.go cmd.Stdin = bytes.NewReader(body)
81
- main.go main.go cmd.Stdout = w
82
- main.go main.go cmd.Stderr = os.Stderr
83
-
84
- main.go main.go // Start the subprocess
85
- main.go main.go log.Printf("Starting subprocess: /usr/local/bin/github-mcp-server")
86
- main.go main.go if err := cmd.Start(); err != nil {
87
- main.go main.go main.go log.Printf("Error starting subprocess: %v", err)
88
- main.go main.go main.go http.Error(w, "Error starting subprocess", http.StatusInternalServerError)
89
- main.go main.go main.go return
90
- main.go main.go }
91
-
92
- main.go main.go // Wait for the subprocess to complete
93
- main.go main.go log.Printf("Waiting for subprocess to complete")
94
- main.go main.go if err := cmd.Wait(); err != nil {
95
- main.go main.go main.go log.Printf("Subprocess error: %v", err)
96
- main.go main.go main.go // Check if it's a timeout error
97
- main.go main.go main.go if ctx.Err() == context.DeadlineExceeded {
98
- main.go main.go main.go main.go log.Printf("Subprocess timed out after %d seconds", timeoutSeconds)
99
- main.go main.go main.go main.go http.Error(w, "Subprocess timed out", http.StatusRequestTimeout)
100
- main.go main.go main.go main.go return
101
- main.go main.go main.go }
102
- main.go main.go main.go http.Error(w, "Subprocess error", http.StatusInternalServerError)
103
- main.go main.go main.go return
104
- main.go main.go }
105
-
106
- main.go main.go log.Printf("Subprocess completed successfully")
107
- main.go })
108
-
109
- main.go // Start the HTTP server
110
- main.go log.Printf("Server listening on port %s", port)
111
- main.go if err := httpServer.ListenAndServe(); err != nil {
112
- main.go main.go log.Fatalf("Failed to start server: %v", err)
113
- main.go }
114
  }
 
1
  package main
2
 
3
  import (
4
+ "bytes"
5
+ "context"
6
+ "fmt"
7
+ "io"
8
+ "log"
9
+ "net/http"
10
+ "os"
11
+ "os/exec"
12
+ "time"
13
+
14
+ "github.com/mark3labs/mcp-go/mcp" // Fixed import path
15
+ "github.com/mark3labs/mcp-go/mcp/protocol"
16
+ "github.com/spf13/viper"
17
  )
18
 
19
  const (
20
+ defaultPort = "8080"
21
+ defaultTimeoutSeconds = 60
22
  )
23
 
24
  func main() {
25
+ // Initialize configuration
26
+ viper.SetDefault("port", defaultPort)
27
+ viper.SetDefault("timeout_seconds", defaultTimeoutSeconds)
28
+ viper.AutomaticEnv()
29
+
30
+ port := viper.GetString("port")
31
+ timeoutSeconds := viper.GetInt("timeout_seconds")
32
+
33
+ // Create HTTP server
34
+ httpServer := &http.Server{
35
+ Addr: ":" + port,
36
+ Handler: nil, // Will be set later
37
+ ReadTimeout: 5 * time.Second,
38
+ WriteTimeout: 10 * time.Second,
39
+ }
40
+
41
+ // Create a context with timeout
42
+ ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds)*time.Second)
43
+ defer cancel()
44
+
45
+ // Start the server
46
+ log.Printf("Starting server on port %s with timeout %d seconds", port, timeoutSeconds)
47
+
48
+ // Handle JSON-RPC requests
49
+ http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
50
+ // Set CORS headers
51
+ w.Header().Set("Access-Control-Allow-Origin", "*")
52
+ w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
53
+ w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
54
+
55
+ // Handle preflight requests
56
+ if r.Method == "OPTIONS" {
57
+ w.WriteHeader(http.StatusOK)
58
+ return
59
+ }
60
+
61
+ // Only handle POST requests
62
+ if r.Method != "POST" {
63
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
64
+ return
65
+ }
66
+
67
+ // Read the request body
68
+ body, err := io.ReadAll(r.Body)
69
+ if err != nil {
70
+ http.Error(w, "Error reading request body", http.StatusBadRequest)
71
+ return
72
+ }
73
+ defer r.Body.Close()
74
+
75
+ // Log the incoming request
76
+ log.Printf("Received request: %s", string(body))
77
+
78
+ // Create a subprocess to handle the request
79
+ cmd := exec.CommandContext(ctx, "/usr/local/bin/github-mcp-server")
80
+ cmd.Stdin = bytes.NewReader(body)
81
+ cmd.Stdout = w
82
+ cmd.Stderr = os.Stderr
83
+
84
+ // Start the subprocess
85
+ log.Printf("Starting subprocess: /usr/local/bin/github-mcp-server")
86
+ if err := cmd.Start(); err != nil {
87
+ log.Printf("Error starting subprocess: %v", err)
88
+ http.Error(w, "Error starting subprocess", http.StatusInternalServerError)
89
+ return
90
+ }
91
+
92
+ // Wait for the subprocess to complete
93
+ log.Printf("Waiting for subprocess to complete")
94
+ if err := cmd.Wait(); err != nil {
95
+ log.Printf("Subprocess error: %v", err)
96
+ // Check if it's a timeout error
97
+ if ctx.Err() == context.DeadlineExceeded {
98
+ log.Printf("Subprocess timed out after %d seconds", timeoutSeconds)
99
+ http.Error(w, "Subprocess timed out", http.StatusRequestTimeout)
100
+ return
101
+ }
102
+ http.Error(w, "Subprocess error", http.StatusInternalServerError)
103
+ return
104
+ }
105
+
106
+ log.Printf("Subprocess completed successfully")
107
+ })
108
+
109
+ // Start the HTTP server
110
+ log.Printf("Server listening on port %s", port)
111
+ if err := httpServer.ListenAndServe(); err != nil {
112
+ log.Fatalf("Failed to start server: %v", err)
113
+ }
114
  }