Skip to content

Input and output definitions

A tool describes the data it accepts and returns with typed definitions. These definitions drive validation, the execute form, config schema, and how the tool is serialized for an AI function or MCP tool.

Input definitions

Inputs are declared with InputDefinition, which extends Drupal's ContextDefinition. Its constructor takes:

Argument Default Purpose
data_type required The typed data type, for example string, integer, boolean, email, entity.
label required Human readable label.
description required What the input is for.
required TRUE Whether the input must be provided.
multiple FALSE Whether the input accepts a list of values.
default_value NULL Default when none is provided.
constraints [] Typed Data validation constraints.
locked FALSE Whether the value is fixed and not user editable.

For structured and list inputs the module provides specialized definitions:

  • MapInputDefinition for an object with named properties.
  • ListInputDefinition for a repeated value.
  • EntityInputDefinition for an entity reference.

Output definitions

Outputs are declared with Drupal's ContextDefinition (or the module's map and list context definitions for structured output). Each output your tool returns from doExecute() must have a matching definition, keyed by the same name. The runtime sets only the output values that correspond to a declared definition; extra values in the result are not exposed as outputs, and output transforms are not applied to them.

The type system: adapters

Each data type is mapped to a typed data adapter, a plugin under Plugin/tool/TypedData/Adapter/. An adapter knows how to render a form element for the type, how to extract a submitted value, and how to build a config schema definition for it. The module ships adapters for:

Adapter Types it handles
StringAdapter Strings.
TextAdapter Long text.
NumberAdapter Integers and floats.
BooleanAdapter Booleans.
EmailAdapter Email addresses.
SelectAdapter A value from a fixed set of options.
ListAdapter Lists of values.
MapAdapter Structured maps.
EntityAdapter Entity references.
UndefinedAdapter Fallback for types without a specific adapter.

To support a new data type, add an adapter plugin with the #[TypedDataAdapter] attribute. The plugin.manager.tool.typed_data_adapter manager resolves the adapter for a given data definition.

Config schema

Adapters also produce the config schema for a tool's inputs. The Tool Explorer view page uses this to generate a suggested tool.plugin.<id> schema when one is missing. The suggested root mapping carries the FullyValidatable constraint, so once you add the schema Drupal validates stored input values against their constraints.

Refining inputs dynamically

A tool can adjust its input definitions based on the values of other inputs. To do this, pass input_definition_refiners to the #[Tool] attribute and implement InputDefinitionRefinerInterface. Its refineInputDefinition($name, $definition, $values) method receives the input being refined and the current values, and returns the refined definition. The attribute enforces the interface: declaring refiners on a class that does not implement it throws at definition build time.