|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | +) |
| 14 | + |
| 15 | +// Input type for executing shell scripts |
| 16 | +type ExecuteCommandInput struct { |
| 17 | + Script string `json:"script" jsonschema:"the shell script to execute"` |
| 18 | + Timeout int `json:"timeout,omitempty" jsonschema:"optional timeout in seconds (default: 30)"` |
| 19 | +} |
| 20 | + |
| 21 | +// Output type for script execution results |
| 22 | +type ExecuteCommandOutput struct { |
| 23 | + Script string `json:"script" jsonschema:"the script that was executed"` |
| 24 | + Stdout string `json:"stdout" jsonschema:"standard output from the script"` |
| 25 | + Stderr string `json:"stderr" jsonschema:"standard error from the script"` |
| 26 | + ExitCode int `json:"exit_code" jsonschema:"exit code of the script (0 means success)"` |
| 27 | + Success bool `json:"success" jsonschema:"whether the script executed successfully"` |
| 28 | + Error string `json:"error,omitempty" jsonschema:"error message if execution failed"` |
| 29 | +} |
| 30 | + |
| 31 | +// getShellCommand returns the shell command to use, defaulting to "sh" if not set |
| 32 | +func getShellCommand() string { |
| 33 | + shellCmd := os.Getenv("SHELL_CMD") |
| 34 | + if shellCmd == "" { |
| 35 | + shellCmd = "sh -c" |
| 36 | + } |
| 37 | + return shellCmd |
| 38 | +} |
| 39 | + |
| 40 | +// ExecuteCommand executes a shell script and returns the output |
| 41 | +func ExecuteCommand(ctx context.Context, req *mcp.CallToolRequest, input ExecuteCommandInput) ( |
| 42 | + *mcp.CallToolResult, |
| 43 | + ExecuteCommandOutput, |
| 44 | + error, |
| 45 | +) { |
| 46 | + // Set default timeout if not provided |
| 47 | + timeout := input.Timeout |
| 48 | + if timeout <= 0 { |
| 49 | + timeout = 30 |
| 50 | + } |
| 51 | + |
| 52 | + // Create a context with timeout |
| 53 | + cmdCtx, cancel := context.WithTimeout(ctx, time.Duration(timeout)*time.Second) |
| 54 | + defer cancel() |
| 55 | + |
| 56 | + // Get shell command from environment variable (default: "sh") |
| 57 | + shellCmd := getShellCommand() |
| 58 | + |
| 59 | + // Parse shell command - support both single command and command with args |
| 60 | + shellParts := strings.Fields(shellCmd) |
| 61 | + shellExec := shellParts[0] |
| 62 | + shellArgs := append(shellParts[1:], input.Script) |
| 63 | + |
| 64 | + // Execute script using the configured shell |
| 65 | + cmd := exec.CommandContext(cmdCtx, shellExec, shellArgs...) |
| 66 | + |
| 67 | + // Create buffers to capture stdout and stderr separately |
| 68 | + var stdoutBuf, stderrBuf bytes.Buffer |
| 69 | + cmd.Stdout = &stdoutBuf |
| 70 | + cmd.Stderr = &stderrBuf |
| 71 | + |
| 72 | + // Execute command |
| 73 | + err := cmd.Run() |
| 74 | + |
| 75 | + exitCode := 0 |
| 76 | + success := true |
| 77 | + errorMsg := "" |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + success = false |
| 81 | + errorMsg = err.Error() |
| 82 | + |
| 83 | + // Try to get exit code if available |
| 84 | + if exitError, ok := err.(*exec.ExitError); ok { |
| 85 | + exitCode = exitError.ExitCode() |
| 86 | + } else { |
| 87 | + // Context timeout or other error |
| 88 | + if cmdCtx.Err() == context.DeadlineExceeded { |
| 89 | + errorMsg = "Command timed out" |
| 90 | + } |
| 91 | + exitCode = -1 |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + output := ExecuteCommandOutput{ |
| 96 | + Script: input.Script, |
| 97 | + Stdout: stdoutBuf.String(), |
| 98 | + Stderr: stderrBuf.String(), |
| 99 | + ExitCode: exitCode, |
| 100 | + Success: success, |
| 101 | + Error: errorMsg, |
| 102 | + } |
| 103 | + |
| 104 | + return nil, output, nil |
| 105 | +} |
| 106 | + |
| 107 | +func main() { |
| 108 | + // Create MCP server for shell command execution |
| 109 | + server := mcp.NewServer(&mcp.Implementation{ |
| 110 | + Name: "shell", |
| 111 | + Version: "v1.0.0", |
| 112 | + }, nil) |
| 113 | + |
| 114 | + // Add tool for executing shell scripts |
| 115 | + mcp.AddTool(server, &mcp.Tool{ |
| 116 | + Name: "execute_command", |
| 117 | + Description: "Execute a shell script and return the output, exit code, and any errors. The shell command can be configured via SHELL_CMD environment variable (default: 'sh')", |
| 118 | + }, ExecuteCommand) |
| 119 | + |
| 120 | + // Run the server |
| 121 | + if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil { |
| 122 | + log.Fatal(err) |
| 123 | + } |
| 124 | +} |
0 commit comments