SAI MCP Server in WebAssembly Go

by example42

314 downloads
Not rated
GitHub

About

SAI MCP Server in WebAssembly Go is a Management Control Protocol (MCP) server implemented as a WebAssembly Go module. It executes the sai command line tool, captures its stdout, stderr, and exit code separately, and returns structured results to a JavaScript environment. The…

Details

Author
example42
Downloads
314
Categories
Other

- Executes the sai command with provided arguments from JavaScript
- Captures stdout, stderr, and exit code separately
- Parses output formats (JSON, YAML, or plain text)
- Returns structured results to the JavaScript environment
- Compatible with the wpcli framework for plugin integration
- Built as a WebAssembly Go module for cross-platform execution

Build the WebAssembly module using GOOS=js GOARCH=wasm go build -o sai_mcp.wasm ./cmd/main. Place the resulting sai_mcp.wasm file in a wpcli plugin directory and create a configuration file that defines available commands. From JavaScript, call the exposed executeSai function with command arguments to execute the sai tool and receive output, errors, and parsed results.

SAI MCP Server in WebAssembly Go

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.

Overview

The SAI MCP Server is a WebAssembly module that provides an interface for executing the SAI command line tool from JavaScript. It handles:

1. Executing the "sai" command with provided arguments
2. Capturing stdout, stderr, and exit code separately
3. Parsing output formats (YAML, JSON, or plain text)
4. Returning structured results to the JavaScript environment

Project Structure

``ini {"id":"01JRF9CWHEXANB157KKNTPE4H3"}
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


Implementation Details

WebAssembly Interface

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 command
-
stderr: Standard error output from the command
-
exitCode: Exit code (0 for success, non-zero for errors)
-
parsedOutput: (Optional) Parsed output if format is recognized
-
format: (Optional) Detected format of the output (json, yaml, text)

SAI Command Execution

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

Output Parsing

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.

Building the WebAssembly Module

To build the WebAssembly module:

go
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.

Integration with wpcli

To integrate with the wpcli framework:

1. Place the WebAssembly module in the appropriate plugin directory
2. Create a configuration file that defines the available commands
3. Register the plugin with the wpcli framework

Example configuration:

yaml
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...

Usage Examples

From JavaScript:

javascript {"id":"01JRF9CWHEXANB157KKR5AF0C9"}
// 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);
}


Error Handling

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

Testing

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):

bash
go test ./cmd/main -v
``

Limitations and Future Improvements

- 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

No reviews yet — be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.