mcpui

package
v0.4.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mcpui is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.

Package mcpui attaches optional game UI widget tree inspection and activation tools to a running game's *mcp.Bridge.

Call Attach once alongside mcp.Attach on the engine goroutine:

  • game_ui_tree: walk a gameui.Root, gameui.BuiltScreen, gameui.SettingsMenu, or custom Provider, returning deterministic widget paths, IDs, kinds, labels, virtual and game-space rectangles, visibility, enabled, focus, and value state.
  • game_ui_activate: drive a widget by path, ID, or unique label (press/click, toggle, set_value/slider, select, focus) directly through the widget's own handlers so pressing "Play" runs the exact same code as a tap or click.

Keeping this package separate from pkg/mcp lets games without UI avoid linking gameui into their MCP bridge. Under -tags release, Attach is a zero-cost no-op.

Index

Examples

Constants

View Source
const (
	// ToolUITree inspects the running game's UI widget tree.
	ToolUITree = "game_ui_tree"
	// ToolUIActivate activates, toggles, adjusts, selects, or focuses a UI widget through its own handler.
	ToolUIActivate = "game_ui_activate"
)

Tool names registered on an *mcp.Bridge by Attach.

Variables

View Source
var ErrAmbiguous = errors.New("mcpui: label matches more than one widget")

ErrAmbiguous reports that an ID or label matches more than one widget; address the widget by its path instead.

View Source
var ErrDisabled = errors.New("mcpui: widget disabled")

ErrDisabled reports that the matched widget is disabled and cannot be activated.

View Source
var ErrHidden = errors.New("mcpui: widget hidden")

ErrHidden reports that the matched widget (or an ancestor) is hidden and cannot be activated.

View Source
var ErrNotFound = errors.New("mcpui: widget not found")

ErrNotFound reports that no widget matches the requested path, ID, or label.

View Source
var ErrUnsupportedAction = errors.New("mcpui: unsupported action for widget")

ErrUnsupportedAction reports that the requested action is not supported by the target widget kind.

Functions

func Activate

func Activate(p Provider, pathOrLabel string, action Action) error

Activate resolves pathOrLabel against the provider's current snapshot and passes the matched widget's path and the action to the provider. It returns ErrNotFound for a nil provider or no match and ErrDisabled for a disabled widget.

Example

ExampleActivate finds and activates widgets by path or label.

package main

import (
	"errors"
	"fmt"

	"github.com/nsgo-dev/game/pkg/mcp/mcpui"
)

// menu is a tiny in-memory Provider that records the actions it receives.
type menu struct{ widgets []mcpui.Widget }

func (m *menu) Snapshot() mcpui.Snapshot { return mcpui.Snapshot{Widgets: m.widgets} }

func (m *menu) Activate(path string, action mcpui.Action) error {
	fmt.Printf("activate %s: %s\n", path, action.Kind)
	return nil
}

// ExampleActivate finds and activates widgets by path or label.
func main() {
	m := &menu{widgets: []mcpui.Widget{
		{Path: "menu/play", Kind: "button", Label: "Play", Enabled: true},
		{Path: "menu/options", Kind: "button", Label: "Options", Enabled: true},
		{Path: "menu/continue", Kind: "button", Label: "Continue"},
	}}

	w, err := mcpui.Find(m.Snapshot(), "options")
	fmt.Println(w.Path, err)

	// Labels match case-insensitively; the provider receives the widget's path.
	fmt.Println(mcpui.Activate(m, "play", mcpui.Action{Kind: "press"}))

	err = mcpui.Activate(m, "Continue", mcpui.Action{Kind: "press"})
	fmt.Println(errors.Is(err, mcpui.ErrDisabled), err)

	err = mcpui.Activate(m, "Quit", mcpui.Action{Kind: "press"})
	fmt.Println(errors.Is(err, mcpui.ErrNotFound))
}
Output:
menu/options <nil>
activate menu/play: press
<nil>
true mcpui: widget disabled: menu/continue
true

func Attach

func Attach(bridge *mcp.Bridge, cfg Config) error

Attach registers game_ui_tree and game_ui_activate on bridge. Passing a nil bridge is a no-op returning nil. Call Attach on the engine goroutine alongside mcp.Attach.

Types

type Action

type Action struct {
	Kind  string
	Value string
}

Action is what to do to a widget, such as Kind "press", "click", "toggle", "focus", "select", or "set" / "set_value" / "slider" with Value holding the new value.

type Config

type Config struct {
	// Root is the active gameui.Root for widget tree inspection and activation.
	Root *gameui.Root
	// RootFunc dynamically resolves the active gameui.Root on each tool call (for example when switching between title, gameplay HUD, and pause screens).
	RootFunc func() *gameui.Root

	// Screen is an active gameui.BuiltScreen built from a *.screen.yaml file; its Root, Navigator, and node IDs are inspected automatically.
	Screen *gameui.BuiltScreen
	// ScreenFunc dynamically resolves the active gameui.BuiltScreen on each tool call.
	ScreenFunc func() *gameui.BuiltScreen

	// Settings is an optional gameui.SettingsMenu whose Root and Navigator are exposed when active.
	Settings *gameui.SettingsMenu
	// SettingsFunc dynamically resolves the active gameui.SettingsMenu on each tool call.
	SettingsFunc func() *gameui.SettingsMenu

	// Navigator is an optional gameui.SpatialNavigator kept in sync when focusing widgets.
	Navigator *gameui.SpatialNavigator
	// NavigatorFunc dynamically resolves the active gameui.SpatialNavigator on each tool call.
	NavigatorFunc func() *gameui.SpatialNavigator

	// Viewport optionally maps virtual UI coordinates into game/window coordinates when Root.Viewport is nil. When nil, falls back to the attached Bridge's Viewport.
	Viewport *screen.Viewport
	// ViewportFunc dynamically resolves the active screen.Viewport on each tool call.
	ViewportFunc func() screen.Viewport

	// WidgetID optionally assigns stable IDs to widgets not built by BuiltScreen.
	WidgetID func(gameui.Widget) string

	// Provider optionally supplies a custom non-gameui UI tree and activation handler.
	Provider Provider
}

Config names the gameui (or custom Provider) source exposed over MCP by Attach. Every field is optional; *Func getters override the corresponding static pointer when returning non-nil.

type Provider

type Provider interface {
	Snapshot() Snapshot
	Activate(path string, action Action) error
}

Provider is implemented by a game over custom widgets when not using gameui directly. Activate must run the widget's own handlers, so the same code runs as for a tap or a key press.

type Rect

type Rect struct {
	X      float32 `json:"x"`
	Y      float32 `json:"y"`
	Width  float32 `json:"width"`
	Height float32 `json:"height"`
}

Rect is a 2D axis-aligned rectangle in virtual or game/window coordinates.

type Snapshot

type Snapshot struct {
	Widgets []Widget `json:"widgets"`
}

Snapshot is the game's UI tree flattened in traversal order.

type Widget

type Widget struct {
	Path      string   `json:"path"`
	Kind      string   `json:"kind,omitempty"`
	Label     string   `json:"label,omitempty"`
	X         float32  `json:"x"`
	Y         float32  `json:"y"`
	Width     float32  `json:"width"`
	Height    float32  `json:"height"`
	Enabled   bool     `json:"enabled"`
	Focused   bool     `json:"focused"`
	Value     string   `json:"value,omitempty"`
	ID        string   `json:"id,omitempty"`
	Parent    string   `json:"parent,omitempty"`
	Visible   bool     `json:"visible"`
	Focusable bool     `json:"focusable,omitempty"`
	Checked   *bool    `json:"checked,omitempty"`
	Min       *float32 `json:"min,omitempty"`
	Max       *float32 `json:"max,omitempty"`
	Selected  *int     `json:"selected,omitempty"`
	Items     []string `json:"items,omitempty"`
	GameRect  *Rect    `json:"gameRect,omitempty"`
}

Widget is one widget in a UI snapshot. Path is unique within a snapshot; X, Y, Width and Height are the widget's rectangle in the game's virtual coordinates.

func Find

func Find(snapshot Snapshot, pathOrLabel string) (Widget, error)

Find returns the widget whose path equals pathOrLabel, else the single widget whose ID equals it, or else the single widget whose label equals it case-insensitively. A path match wins over an ID match, which wins over a label match; a shared ID or label returns ErrAmbiguous.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL