-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
112 lines (99 loc) · 3.06 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"github.com/1Panel-dev/mcp-1panel/operations/app"
"github.com/1Panel-dev/mcp-1panel/operations/database"
"github.com/1Panel-dev/mcp-1panel/operations/ssl"
"github.com/1Panel-dev/mcp-1panel/operations/system"
"github.com/1Panel-dev/mcp-1panel/operations/website"
"github.com/1Panel-dev/mcp-1panel/utils"
"log"
"os"
"path/filepath"
"github.com/mark3labs/mcp-go/server"
)
var (
Version = utils.Version
)
func setupLogger() (*os.File, error) {
logDir := "logs"
if err := os.MkdirAll(logDir, 0755); err != nil {
fmt.Printf("create log dir error: %v\n", err)
return nil, err
}
logFilePath := filepath.Join(logDir, "mcp-1panel.log")
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
fmt.Printf("open log file error: %v\n", err)
return nil, err
}
log.SetOutput(logFile)
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
return logFile, nil
}
func newMCPServer() *server.MCPServer {
return server.NewMCPServer(
"github.com/1Panel-dev/mcp-1panel",
Version,
server.WithToolCapabilities(true),
server.WithLogging(),
)
}
func addTools(s *server.MCPServer) {
s.AddTool(system.GetSystemInfoTool, system.GetSystemInfoHandle)
s.AddTool(system.GetDashboardInfoTool, system.GetDashboardInfoHandle)
s.AddTool(website.ListWebsitesTool, website.ListWebsiteHandle)
s.AddTool(website.CreateWebsiteTool, website.CreateWebsiteHandle)
s.AddTool(ssl.ListSSLsTool, ssl.ListSSLHandle)
s.AddTool(app.InstallMySQLTool, app.InstallMySQLHandle)
s.AddTool(app.InstallOpenRestyTool, app.InstallOpenRestyHandle)
s.AddTool(app.ListInstalledAppsTool, app.ListInstalledAppsHandle)
s.AddTool(ssl.CreateSSLTool, ssl.CreateSSLHandle)
s.AddTool(database.ListDatabasesTool, database.ListDatabasesHandle)
s.AddTool(database.CreateDatabaseTool, database.CreateDatabaseHandle)
}
func runServer(transport string, addr string) error {
mcpServer := newMCPServer()
addTools(mcpServer)
if transport == "sse" {
port, err := utils.GetPortFromAddr(addr)
if err != nil {
return err
}
log.Printf("SSE server listening on :%s", port)
sseServer := server.NewSSEServer(mcpServer, server.WithBaseURL(addr))
if err := sseServer.Start(fmt.Sprintf(":%s", port)); err != nil {
log.Fatalf("Server error: %v", err)
}
} else {
log.Printf("Run Stdio server")
if err := server.ServeStdio(mcpServer); err != nil {
log.Fatalf("Server error: %v", err)
}
}
return nil
}
func main() {
var (
transport string
accessToken string
host string
addr string
)
flag.StringVar(&transport, "transport", "stdio", "Transport type (stdio or sse)")
flag.StringVar(&addr, "addr", "http://localhost:8000", "The base URL for mcp Server")
flag.StringVar(&accessToken, "token", "", "1Panel api key")
flag.StringVar(&host, "host", "", "1Panel host (example:http://127.0.0.1:9999)")
flag.Parse()
if accessToken != "" {
utils.SetAccessToken(accessToken)
}
if host != "" {
utils.SetHost(host)
}
if err := runServer(transport, addr); err != nil {
fmt.Printf("server run error: %v\n", err)
panic(err)
}
}