SAI MCP Server logo

SAI MCP Server

by example42

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, capturing output, and parsing results.

View on GitHub

Last updated: N/A

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

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:

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:

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:

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

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