This project implements a Management Control Protocol (MCP) server in WebAssembly Go that executes the "sai" command and handles its output and exit code. The implementation is compatible with the wpcli framework.
The SAI MCP Server is a WebAssembly module that provides an interface for executing the SAI command line tool from JavaScript. It handles:
- Executing the "sai" command with provided arguments
- Capturing stdout, stderr, and exit code separately
- Parsing output formats (YAML, JSON, or plain text)
- Returning structured results to the JavaScript environment
sai_mcp/
├── cmd/
│ └── main/
│ ├── main.go # WebAssembly entry point and JavaScript interface
│ ├── sai_executor.go # SAI command execution logic
│ ├── parser.go # Output format parsing (YAML, JSON)
│ └── main_test.go # Test cases for the implementation
└── go.mod # Go module definition
The WebAssembly module exposes a JavaScript function executeSai
that takes command arguments and returns an object with the following properties:
stdout
: Standard output from the commandstderr
: Standard error output from the commandexitCode
: Exit code (0 for success, non-zero for errors)parsedOutput
: (Optional) Parsed output if format is recognizedformat
: (Optional) Detected format of the output (json, yaml, text)
The SaiExecutor
component handles the actual execution of the SAI command:
- Creates a new process for the SAI command
- Captures stdout and stderr separately
- Extracts the exit code from the process
- Handles execution errors appropriately
The implementation includes parsers for different output formats:
- JSON: Parses JSON output into a structured object
- YAML: Parses YAML output into a structured object
- Text: Returns plain text as-is
The format is automatically detected based on the command and output content.
To build the WebAssembly module:
GOOS=js GOARCH=wasm go build -o sai_mcp.wasm ./cmd/main
This will produce a WebAssembly file (sai_mcp.wasm
) that can be loaded by the wpcli framework.
To integrate with the wpcli framework:
- Place the WebAssembly module in the appropriate plugin directory
- Create a configuration file that defines the available commands
- Register the plugin with the wpcli framework
Example configuration:
name: sai
description: SAI command line tool integration
uuid: sai-plugin-uuid
versions:
- version: 1.0.0
wasm: sai_mcp.wasm
conf: sai_config.yaml
commands:
- name: install
description: Install software
usage: install <software> [options]
args:
- name: software
type: string
description: The name of the software to install
required: true
# Additional commands...
From JavaScript:
// Execute a SAI command
const result = executeSai("install", "nginx");
// Check the result
console.log("Exit code:", result.exitCode);
console.log("Output:", result.stdout);
if (result.stderr) {
console.error("Error:", result.stderr);
}
// Access parsed output if available
if (result.parsedOutput) {
console.log("Parsed output:", result.parsedOutput);
}
The implementation handles various error scenarios:
- Command not found: Returns appropriate error in stderr and non-zero exit code
- Command execution failure: Captures error message in stderr and returns the actual exit code
- Output parsing errors: Falls back to returning raw output if parsing fails
The implementation includes comprehensive tests that verify:
- Basic command execution
- Output format detection and parsing
- Error handling
- Exit code propagation
To run the tests (requires native Go environment, not WebAssembly):
go test ./cmd/main -v
- The current implementation assumes the SAI command is available in the PATH
- Output format detection is based on simple heuristics and could be improved
- More sophisticated parsing could be added for specific SAI commands
- WebAssembly has limitations regarding file system access and process execution