Skip to content

feat(inventory): expose NBT/component data in inventory MCP tools#3163

Merged
milutinke merged 1 commit into
MCCTeam:masterfrom
italoseara:feat/inventory-nbt
Jul 4, 2026
Merged

feat(inventory): expose NBT/component data in inventory MCP tools#3163
milutinke merged 1 commit into
MCCTeam:masterfrom
italoseara:feat/inventory-nbt

Conversation

@italoseara

Copy link
Copy Markdown
Contributor

Right now the inventory tools only return item type and count, which makes it impossible to distinguish a renamed item from a plain one, read server-specific metadata (custom_data), or check enchantments and potion contents. The material name alone isn't enough for most real automation tasks.

This adds a nullable Nbt field to MccInventorySnapshotSlot, MccInventorySearchMatch, and MccItemStackSnapshot:

  • On 1.20.6+ it's a map of component name -> serialized component, e.g. "minecraft:custom_data", "minecraft:enchantments", "minecraft:custom_name"
  • On older versions it falls back to the raw NBT dictionary
  • Omitted entirely when the item has no extra data (plain Stone, Dirt, etc. come back clean with no Nbt field)

To make this work, StructuredComponent now stores the registry name it was parsed under (ComponentName), set in ParseComponent() alongside the existing TypeId. Both are marked [JsonIgnore] so they don't appear in the serialized output. BuildNbt uses c.GetType() when calling JsonSerializer.SerializeToElement - necessary because the list elements are typed as the abstract base class and the default serializer would otherwise produce empty objects.

Tested against a 1.21.11 server with a mix of custom items (custom_data, enchantments, block_entity_data) and vanilla items.

Add a nullable `Nbt` field to `MccInventorySnapshotSlot`,
`MccInventorySearchMatch`, and `MccItemStackSnapshot` so that callers
can access the full item metadata beyond just material and count.

For 1.20.6+ servers the field is a map of component name
(e.g. "minecraft:custom_data") to the serialized component object.
For legacy servers it is the raw NBT dictionary. The field is omitted
entirely for vanilla items that carry no extra data, so there is no
noise for plain items like Stone or Dirt.

Implementation:
- `StructuredComponent`: add `ComponentName` property (set by the
  registry during parse) and mark both it and `TypeId` with
  `[JsonIgnore]` so they are excluded from component serialization.
- `StructuredComponentRegistry.ParseComponent`: assign `ComponentName`
  after instantiation.
- `MccGameCommon.BuildNbt`: new helper that serializes components by
  runtime type (fixing the polymorphism issue with System.Text.Json)
  keyed by component name, falling back to the legacy NBT dictionary.
- Snapshot and search query builders updated to call `BuildNbt`.
@milutinke

Copy link
Copy Markdown
Member

Thank you for your contribution.
Merging

@milutinke
milutinke merged commit 99f5744 into MCCTeam:master Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants