跳转到内容

LLM Control (MCP)

此内容尚不支持你的语言。

Wails v3 has a built-in Model Context Protocol (MCP) server that lets LLM agents — Claude Code, IDE assistants, or any MCP client — inspect, test, and drive a running Wails application.

When enabled, an agent connected to your app can:

  • List and control windows — size, position, focus, fullscreen, devtools, reload, …
  • Inspect the DOM — query elements, get HTML, take a structural snapshot
  • Evaluate JavaScript — run arbitrary code inside any window and get the result
  • Simulate user input — mouse moves, clicks, drags and scrolls rendered with an animated on-screen cursor so you can watch the agent work
  • Type and press keys — realistic per-character events that work with React controlled inputs
  • Call bound Go methods and emit/await application events

The MCP server is compiled into your application only when the mcp build tag is present. Without the tag the server code is completely absent from the binary — no runtime overhead, no open ports, no attack surface.

When the tag is present the server starts automatically inside App.Run(), binds to 127.0.0.1:9099 by default, and logs its endpoint. No user code is required.

Step 1 — write a normal Wails application

Section titled “Step 1 — write a normal Wails application”

MCP requires no imports and no registration. Create your app as you normally would:

main.go
package main
import (
"embed"
"log"
"github.com/wailsapp/wails/v3/pkg/application"
)
//go:embed assets
var assets embed.FS
func main() {
app := application.New(application.Options{
Name: "My App",
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
})
app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "My App",
Width: 1024, Height: 768,
})
if err := app.Run(); err != nil {
log.Fatal(err)
}
}

Set WAILS_MCP=1 and the Wails CLI adds the mcp tag for you:

Terminal window
# Development
WAILS_MCP=1 wails3 dev
# Production build
WAILS_MCP=1 wails3 build

On startup the application logs the MCP endpoint:

INFO MCP server started. Connect MCP clients using the streamable HTTP transport.
url=http://127.0.0.1:9099/mcp

The server speaks the MCP streamable HTTP transport. Connect with any MCP-compatible client.

Terminal window
claude mcp add --transport http my-app http://127.0.0.1:9099/mcp

Then ask Claude to interact with your app:

Click the "Submit" button, then verify a success toast appears.

Ask the agent to exercise your application. Here are some example prompts:

Take a DOM snapshot of the main window.
Click the "Add item" button, type "Hello world" in the input field,
then press Enter and verify the item appears in the list.
Call the bound method main.GreetService.Greet with argument ["World"]
and return the result.
Wait for the event "save:complete" while clicking the Save button.

All configuration is via environment variables — no code changes required.

Environment variableDefaultDescription
WAILS_MCP(unset)Set to 1, true, on or yes to add the mcp build tag automatically when using the Wails CLI.
WAILS_MCP_HOST127.0.0.1Interface to bind to. Change only in trusted, isolated networks.
WAILS_MCP_PORT9099Port to listen on. Set to 0 for a randomly assigned free port (printed in the log).
WAILS_MCP_TIMEOUT30000Default JS evaluation timeout in milliseconds.
WAILS_MCP_HIDE_CURSOR(unset)Set to 1 or true to disable the animated cursor overlay.

Example — custom port and a 60-second timeout:

Terminal window
WAILS_MCP=1 WAILS_MCP_PORT=9200 WAILS_MCP_TIMEOUT=60000 wails3 dev
ToolPurpose
app_infoApplication information: platform, architecture, all windows, MCP endpoint
windows_listList all windows with geometry and state
window_controlFocus, resize, move, fullscreen, devtools, reload, set URL, … (22 actions)
js_evalEvaluate JavaScript in a window (async body, return for value)
dom_htmlGet the HTML of the page or a specific element
dom_queryFind elements by CSS selector — tag, text, bounds, visibility
screenshot_domStructural snapshot of the visible page (DOM-based, no pixels)
mouse_moveAnimate the cursor to a point or CSS selector
mouse_clickClick with the animated cursor (left/right/middle, double-click, modifiers)
mouse_dragDrag with the animated cursor (supports HTML5 drag-and-drop elements)
mouse_scrollScroll at a point or element
keyboard_typeType text character by character with realistic events
keyboard_pressPress a single key (Enter, Tab, Escape, ArrowDown, …) with optional modifiers
call_bound_methodCall a bound Go service method, e.g. main.GreetService.Greet
emit_eventEmit a Wails application event
wait_for_eventWait for a Wails application event and return its data

All tools that act on a window accept an optional window argument containing the window’s name (set via WebviewWindowOptions.Name). When omitted, the tool targets the currently focused window, or the first window if none is focused.

List all windows, then click the "New" button in the window named "editor".

Mouse and keyboard tools accept either:

  • CSS selectorselector: "#submit-btn" (element is scrolled into view automatically)
  • Coordinatesx: 400, y: 300 (CSS pixels relative to the viewport)

For drag operations, prefix with from_ and to_:

Drag from selector: ".card" to selector: ".dropzone"
  • The server binds to 127.0.0.1 by default and rejects non-local browser origins (DNS-rebinding protection per the MCP spec).
  • Production builds should not include the mcp tag. The Wails CLI only adds it when WAILS_MCP=1 is explicitly set, and the default wails3 build has none of the server code.
  • If you need to expose the server on a non-loopback interface (e.g. LAN testing), set WAILS_MCP_HOST=0.0.0.0 and ensure your network is trusted.

A complete playground application demonstrating all tools is available at v3/examples/mcp. It includes:

  • Counter with increment/reset buttons
  • Name input with Greet, Add, Shout bound methods
  • HTML5 drag-and-drop source and target
  • Scrollable list (50 items)
  • Event log

Run it with:

Terminal window
cd v3/examples/mcp
go run -tags mcp .

Then connect Claude Code or any MCP client to http://127.0.0.1:9099/mcp and ask it to exercise the UI.

The built-in MCP server is an experiment, and your feedback decides where it goes. If you try it, we’d love to hear which client and tools you used, what you expected and what actually happened, and whether letting an agent drive your app was useful — the most useful reports say exactly what you ran. Tell us in the MCP Server feedback discussion.