-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
111 lines (88 loc) · 2.92 KB
/
main.go
File metadata and controls
111 lines (88 loc) · 2.92 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
package main
import (
"context"
"flag"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/common-creation/mcp-gateway/internal/config"
"github.com/common-creation/mcp-gateway/internal/handler"
"github.com/common-creation/mcp-gateway/internal/proxy"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func main() {
addr := flag.String("addr", ":8080", "Server address")
configPath := flag.String("config", "mcp.json", "Path to mcp.json config file")
flag.Parse()
absConfigPath, err := filepath.Abs(*configPath)
if err != nil {
log.Fatalf("Failed to get absolute config path: %v", err)
}
configMgr := config.NewConfigManager(absConfigPath)
if err := configMgr.Load(); err != nil {
log.Fatalf("Failed to load config: %v", err)
}
manager := proxy.NewManager(configMgr)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if err := manager.StartAll(ctx); err != nil {
log.Printf("Warning: Some servers failed to start: %v", err)
}
e := echo.New()
e.HideBanner = true
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodOptions},
AllowHeaders: []string{
echo.HeaderOrigin,
echo.HeaderContentType,
echo.HeaderAccept,
"Mcp-Session-Id",
"Mcp-Protocol-Version",
"Last-Event-ID",
},
ExposeHeaders: []string{
"Mcp-Session-Id",
},
}))
proxyHandler := handler.NewProxyHandler(manager)
streamableHandler := handler.NewStreamableProxyHandler(manager)
configHandler := handler.NewConfigHandler(configMgr, manager)
api := e.Group("/api")
api.GET("/config", configHandler.GetConfig)
api.PUT("/config", configHandler.SaveConfig)
api.POST("/config/reload", configHandler.ReloadConfig)
api.GET("/servers", configHandler.GetServers)
api.POST("/servers/:name/start", configHandler.StartServer)
api.POST("/servers/:name/stop", configHandler.StopServer)
// Legacy JSON-RPC proxy endpoint (POST only)
e.POST("/v1/proxy/:name", proxyHandler.HandleMCP)
// Streamable HTTP transport endpoint (GET, POST, DELETE)
// This is the recommended endpoint for MCP clients using Streamable HTTP
e.Any("/mcp/:name", streamableHandler.HandleMCP)
e.Static("/", "frontend/dist")
go func() {
if err := e.Start(*addr); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
}
}()
log.Printf("MCP Gateway started on %s", *addr)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down server...")
manager.StopAll()
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownCancel()
if err := e.Shutdown(shutdownCtx); err != nil {
log.Fatalf("Server shutdown error: %v", err)
}
log.Println("Server stopped")
}