Skip to content

feat(mcp): add spawn, destroy and component edit tools (#464) - #708

Merged
houko merged 3 commits into
mainfrom
feat/mcp-spawn-edit-464
Nov 24, 2026
Merged

houko merged 3 commits into
mainfrom
feat/mcp-spawn-edit-464

Conversation

@houko

@houko houko commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Implements object spawning, entity destruction, and live component editing for packages/game/mcp (parent: #460).

  • MCP mutation tools (packages/game/mcp/edit.go):
    • game_prefabs_list: lists registered prefabs (custom, entity_prefab, scene) in deterministic sorted order.
    • game_entity_spawn: spawns a registered prefab (or empty entity) with optional overrides, parent ref, x/y Transform2D convenience position, and initial components map; rolls back spawned entities atomically if parenting, component setup or the read-back fails.
    • game_entity_destroy: destroys a live entity by stable generational ref (entity:<index>:<generation>) with optional hierarchy policy (cascade, orphan, keep_world, reparent, the last keeping children's world transforms), reporting the policy that actually ran (the hierarchy's default when omitted, none for a childless entity), and rejecting stale/invalid refs cleanly.
    • game_component_set: sets or patches a named component on a live entity across entities.RegisterCodec codecs, built-in transform2d/object_ref (propagating Hierarchy2D dirty flags), and bridge-registered editors (RegisterMutableComponent, RegisterComponentEditor). Refuses unknown fields, trailing data, non-finite floats (NaN/±Inf), invalid nulls, and read-only/derived components without partially mutating state. Patches keep untouched fields exactly as they were (unexported, json:"-" and pointer fields included). A failed read-back after a successful write is reported in read_error.
    • game_component_remove: removes a named editable component from a live entity.
  • Prefab & editor registration:
    • Config.Prefabs, Bridge.RegisterPrefab, Bridge.RegisterEntityPrefab, and Bridge.RegisterScenePrefab (exactly one top-level object per scene prefab).
    • mcp.RegisterMutableComponent[T] and mcp.RegisterComponentEditor[T] (which upgrades only a RegisterComponent[T] registration of the same type).
  • Codec helpers (packages/game/entities/snapshot.go):
    • World.DecodeComponent, World.PatchComponent, and World.RemoveComponent with strict JSON decoding, isolated patching through internal/jsonedit and refusal of values the codec cannot encode back (such as NaN).
  • Docs, changelog & engineering note:
    • Updated docs/en/mcp.md, docs/zh/mcp.md, packages/game/mcp/doc.go, and packages/game/mcp/example_test.go.
    • Added changelog.d/mcp-spawn-destroy-edit.md and .agents/notes/implemented/feature/2026-11-24-mcp-spawn-destroy-edit.md.

Verification

  • gofmt clean
  • go tool nsgo test for ./internal/jsonedit, ./entities and ./mcp, and go tool nsgo check -target linux,web for each
  • full go tool nsgo test . in packages/game (3419 passed)
  • go run ./tools/changelog check passed

Closes #464

@github-actions github-actions Bot added enhancement New feature or request size/XL documentation Improvements or additions to documentation pkg/entities area/agents conflict Conflicts with the target branch; resolve before merging labels Nov 24, 2026
…dec edits

PatchComponent built its starting value with a JSON round trip, which zeroed unexported fields and json:"-" fields and replaced every pointee with a copy. Patches now go through internal/jsonedit, which decodes onto a clone whose exported pointers, maps, slices and interfaces are freshly allocated and then takes only the fields the patch changed, so untouched runtime state and pointer identity survive and a failed decode never reaches the live value.

DecodeComponent and PatchComponent also refuse a decoded value that cannot be encoded back, such as a NaN produced by a custom UnmarshalJSON, which would otherwise break every later EncodeComponent and Snapshot of the entity.
…hat ran

- Typed component patches use internal/jsonedit, so unexported, json:"-" and pointer fields survive and a decode error or a rejecting editor leaves the live component untouched, including components with callback fields that cannot be marshalled.
- game_entity_spawn rolls back when reading the new entity back fails; game_component_set reports a failed read-back in read_error instead of an error, since the write already stands.
- game_entity_destroy reports the hierarchy's actual default policy (or "none" for a childless entity), "reparent" keeps children's world transforms, and children are checked before any of them moves.
- RegisterScenePrefab requires exactly one top-level object, and RegisterComponentEditor only upgrades a RegisterComponent[T] registration of the same type.
- Docs, the design note and hard-wrapped comments are updated to match.
@houko
houko force-pushed the feat/mcp-spawn-edit-464 branch from 14d1dd6 to e8edb28 Compare November 24, 2026 19:13
@github-actions github-actions Bot added pkg/internal pkg/game and removed conflict Conflicts with the target branch; resolve before merging labels Nov 24, 2026
@houko
houko merged commit c0e4dbb into main Nov 24, 2026
19 of 36 checks passed
@houko
houko deleted the feat/mcp-spawn-edit-464 branch November 24, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agents documentation Improvements or additions to documentation enhancement New feature or request pkg/entities pkg/game pkg/internal size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP: spawn, destroy and edit objects

1 participant