Sponsored by Deepsite.site

SAI MCP Server in WebAssembly Go

Created By
example428 months ago
Sai Mcp Server
Content

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
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Playwright McpPlaywright MCP server
Tavily Mcp
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
ChatWiseThe second fastest AI chatbot™
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Amap Maps高德地图官方 MCP Server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
WindsurfThe new purpose-built IDE to harness magic
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
DeepChatYour AI Partner on Desktop
Serper MCP ServerA Serper MCP Server
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
CursorThe AI Code Editor