Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
132 lines (110 loc) · 3.37 KB
/
Copy pathmain.go
File metadata and controls
132 lines (110 loc) · 3.37 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
package main
import (
"fmt"
"os"
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"
"github.com/rs/zerolog"
)
var version = "dev"
func main() {
if len(os.Args) > 1 && os.Args[1] == sandboxArg {
if err := runSandboxShim(); err != nil {
fmt.Fprintf(os.Stderr, "sandbox: %v\n", err)
os.Exit(127)
}
}
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run() error {
cfg, err := loadConfig()
if err != nil {
return fmt.Errorf("failed to load configuration: %w", err)
}
if version != "dev" {
cfg.Server.Version = version
}
log, err := newLogger(cfg.Logging)
if err != nil {
return fmt.Errorf("failed to initialize logger: %w", err)
}
configFile := os.Getenv("MCP_SHELL_SEC_CONFIG_FILE")
switch {
case configFile != "":
log.Info().Str("config_file", configFile).Msg("Loading security config from file")
case cfg.Security.Enabled:
log.Info().Msg("No security config file specified, using built-in secure defaults")
default:
log.Warn().Msg("SECURITY DISABLED via MCP_SHELL_ALLOW_UNSAFE - all commands run unrestricted")
}
log.Info().
Str("server_name", cfg.Server.Name).
Str("version", cfg.Server.Version).
Str("log_level", cfg.Logging.Level).
Str("log_format", cfg.Logging.Format).
Bool("security_enabled", cfg.Security.Enabled).
Msg("Configuration loaded")
if cfg.Security.Enabled {
log.Info().
Str("working_dir", cfg.Security.WorkingDirectory).
Dur("max_execution_time", cfg.Security.MaxExecutionTime).
Int("max_output_size", cfg.Security.MaxOutputSize).
Bool("audit_log", cfg.Security.AuditLog).
Bool("writes_enabled", cfg.Security.WritesEnabled).
Bool("sandbox", cfg.Security.Sandbox).
Int("scripts", len(cfg.Security.Scripts)).
Msg("Security configuration")
}
executor := newCommandExecutor(cfg.Security, log)
s := server.NewMCPServer(
cfg.Server.Name,
cfg.Server.Version,
server.WithToolCapabilities(false),
)
if err := registerTools(s, cfg, executor, log); err != nil {
return fmt.Errorf("register tools: %w", err)
}
log.Info().Msg("MCP server initialized, serving on stdio")
if err := server.ServeStdio(s); err != nil {
return fmt.Errorf("server error: %w", err)
}
return nil
}
func registerTools(s *server.MCPServer, cfg *Config, executor *CommandExecutor, log zerolog.Logger) error {
if cfg.Security.Enabled {
ws, err := newWorkspace(cfg.Security.WorkingDirectory)
if err != nil {
return fmt.Errorf("workspace: %w", err)
}
if cfg.Security.WritesEnabled {
log.Warn().Msg("writes_enabled: file and git write tools are exposed")
}
newFSTools(ws, cfg.Security.MaxOutputSize, cfg.Security.WritesEnabled, log).register(s)
newGitTools(ws, executor, cfg.Security.WritesEnabled, log).register(s)
newScriptTools(cfg.Security.Scripts, executor, log).register(s)
return nil
}
shellHandler := newShellHandler(executor, log)
shellTool := mcp.NewTool(
"shell_exec",
mcp.WithDescription(
"UNRESTRICTED: runs the command through bash -c with no validation. Only available with MCP_SHELL_ALLOW_UNSAFE=1.",
),
mcp.WithString("command",
mcp.Required(),
mcp.Description("Shell command to execute"),
),
mcp.WithBoolean(
"base64",
mcp.DefaultBool(false),
mcp.Description(
"Return stdout/stderr as base64-encoded strings (useful for binary data)",
),
),
)
s.AddTool(shellTool, shellHandler.handle)
return nil
}