mcp-golang-wasm-example

mcp-golang-wasm-example

An MCP server running in the browser via WebAssembly, providing tools like getCurrentTime and add for streamable HTTP communication.

Category
Visit Server

README

MCP Go WASM Server

A Model Context Protocol (MCP) server implementation in Go that runs in the browser via WebAssembly. This project demonstrates how to build an MCP server that can be executed client-side using WASM, enabling streamable HTTP communication without external dependencies.

๐ŸŒŸ Features

  • Pure Go Implementation: Built using Go's standard library with no external dependencies
  • WebAssembly Support: Runs entirely in the browser via WASM
  • MCP Protocol Compliant: Implements the Model Context Protocol specification (version 2024-11-05)
  1. Go WASM Module: Compiles to WebAssembly and exposes JavaScript functions
  2. JavaScript Bridge: Handles communication between the browser and WASM
  3. JSON-RPC Protocol: Standard request/response pattern for tool invocation
  4. MCP Compliance: Follows the Model Context Protocol specification

๐Ÿš€ Quick Start

Prerequisites

  • Go 1.21 or higher
  • Python 3 (for local HTTP server)
  • Modern web browser with WebAssembly support
  • Or use the included Dev Container for a pre-configured environment

Using Dev Container (Recommended)

This project includes a Dev Container configuration for VS Code. You do not need to install Go or Python locally.

Steps:

  1. Open the project in VS Code.
  2. If prompted, "Reopen in Container" (or use the Command Palette: Dev Containers: Reopen in Container).
  3. The container will build with Go and Python pre-installed, and VS Code extensions for Go and Python enabled.
  4. Use the integrated terminal to run make build and make serve as described below.

This ensures a consistent development environment for all contributors.

Build and Run

# Build the WASM binary
make build

# Serve the application (builds automatically)
make serve

# Or combine both steps
make run

Then open your browser to http://localhost:8080

Manual Build

# Build WASM
GOOS=js GOARCH=wasm go build -o main.wasm main.go

# Copy the Go WASM runtime
cp "$(go env GOROOT)/misc/wasm/wasm_exec.js" .

# Serve with any HTTP server
python3 -m http.server 8080

๐Ÿ“– Usage

Using the Web Interface

  1. Initialize Server: Click "Initialize Server" to establish the MCP connection
  2. List Tools: View all available tools and their schemas
  3. Call Tool: Select a tool from the dropdown, enter parameters (space-separated), and execute the tool. For example, use add 2 3 to get the sum of 2 and 3, or select getCurrentTime with no parameters.

Programmatic Usage

// Send a JSON-RPC request to the MCP server
const request = {
    jsonrpc: "2.0",
    id: 1,
    method: "tools/call",
    params: {
        name: "add",
        arguments: { a: 2, b: 3 }
    }
};

const responseJSON = mcpHandleRequest(JSON.stringify(request));
const response = JSON.parse(responseJSON);
console.log(response.result);

๐Ÿ› ๏ธ MCP Protocol Implementation

Supported Methods

Method Description
initialize Initialize the MCP server connection
tools/list List all available tools
tools/call Execute a specific tool

Example Tools

getCurrentTime

{
    "name": "getCurrentTime",
    "description": "Returns the current time in RFC3339 format",
    "inputSchema": {
        "type": "object",
        "properties": {},
        "required": []
    }
}

add

{
    "name": "add",
    "description": "Returns the sum of two numbers (a + b)",
    "inputSchema": {
        "type": "object",
        "properties": {
            "a": { "type": "number" },
            "b": { "type": "number" }
        },
        "required": ["a", "b"]
    }
}

๐Ÿ”ง Adding New Tools

๐Ÿ”ง Adding or Using Tools

To use a tool, select it from the dropdown in the UI and enter its parameters (space-separated). For example:

  • For add, enter: 2 3 (returns 5)
  • For getCurrentTime, no parameters are needed

To add a new tool to the server, define it in NewMCPServer() and implement its handler in handleToolCall(), then rebuild the WASM binary.

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ main.go           # MCP server implementation
โ”œโ”€โ”€ index.html        # Web interface
โ”œโ”€โ”€ go.mod           # Go module definition
โ”œโ”€โ”€ Makefile         # Build automation
โ””โ”€โ”€ README.md        # This file

๐ŸŽจ Design Philosophy

This implementation prioritizes:

  • Simplicity: No external dependencies, pure Go standard library
  • Efficiency: Minimal overhead, direct JavaScript bridge
  • Idiomatic Go: Follows Go best practices and conventions
  • Browser-First: Designed specifically for WASM/browser environments
  • MCP Compliance: Adheres to the Model Context Protocol specification

๐Ÿ” Technical Details

WASM Bridge

The Go code exposes a single function to JavaScript:

js.Global().Set("mcpHandleRequest", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
    requestJSON := args[0].String()
    response := server.HandleRequest(requestJSON)
    return response
}))

JSON-RPC 2.0

All communication follows the JSON-RPC 2.0 specification:

  • Request: { jsonrpc, id, method, params }
  • Response: { jsonrpc, id, result } or { jsonrpc, id, error }

Error Codes

Standard JSON-RPC error codes:

  • -32700: Parse error
  • -32601: Method not found
  • -32602: Invalid params

๐Ÿงช Testing

Test the server using the web interface or browser console:

// Initialize
mcpHandleRequest('{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}')

// List tools
mcpHandleRequest('{"jsonrpc":"2.0","id":2,"method":"tools/list"}')

// Call tool
mcpHandleRequest('{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"getCurrentTime"}}')

๐Ÿ“ License

MIT License - feel free to use this in your own projects!

๐Ÿค Contributing

Contributions are welcome! Feel free to:

  • Add new example tools
  • Improve the UI
  • Enhance error handling
  • Add tests
  • Improve documentation

๐Ÿ”— Resources


Built with โค๏ธ using Go and WebAssembly

Recommended Servers

playwright-mcp

playwright-mcp

A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.

Official
Featured
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.

Official
Featured
Local
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.

Official
Featured
Local
TypeScript
VeyraX MCP

VeyraX MCP

Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.

Official
Featured
Local
graphlit-mcp-server

graphlit-mcp-server

The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.

Official
Featured
TypeScript
Kagi MCP Server

Kagi MCP Server

An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.

Official
Featured
Python
E2B

E2B

Using MCP to run code via e2b.

Official
Featured
Neon Database

Neon Database

MCP server for interacting with Neon Management API and databases

Official
Featured
Exa Search

Exa Search

A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.

Official
Featured
Qdrant Server

Qdrant Server

This repository is an example of how to create a MCP server for Qdrant, a vector search engine.

Official
Featured