Class ToolContextHelper
This class provides type-safe, user-friendly methods to access frequently used
context data. For custom metadata or advanced use cases, you can still use
ToolContextConstants keys directly with ToolContext.getContext().
Two ways to access ToolContext data:
1. Helper way (recommended for standard data):
// Get RunnableConfig - type-safe, no casting needed Optional<RunnableConfig> config = ToolContextHelper.getConfig(toolContext); // Get OverAllState Optional<OverAllState> state = ToolContextHelper.getState(toolContext); // Get custom metadata with type safety Optional<String> userId = ToolContextHelper.getMetadata(toolContext, "user_id", String.class);
2. Direct key access (for custom data or batch operations):
// Access using ToolContextConstants
Map<String, Object> context = toolContext.getContext();
RunnableConfig config = (RunnableConfig) context.get(ToolContextConstants.AGENT_CONFIG_CONTEXT_KEY);
// Or access your custom keys
Object myData = context.get("my_custom_key");
- See Also:
-
ToolContextToolContextConstants
-
Method Summary
Modifier and TypeMethodDescriptiongetAllContext(org.springframework.ai.chat.model.ToolContext toolContext) Get all context data as a Map.static Optional<RunnableConfig>getConfig(org.springframework.ai.chat.model.ToolContext toolContext) Get the RunnableConfig from ToolContext.static <T> Optional<T>getMetadata(org.springframework.ai.chat.model.ToolContext toolContext, String key, Class<T> type) Get metadata value by key with type safety.static <T> TgetMetadataOrDefault(org.springframework.ai.chat.model.ToolContext toolContext, String key, Class<T> type, T defaultValue) Get metadata value with default fallback.static Optional<OverAllState>getState(org.springframework.ai.chat.model.ToolContext toolContext) Get the agent state (OverAllState) from ToolContext.getStateForUpdate(org.springframework.ai.chat.model.ToolContext toolContext) Get the state map for update from ToolContext.static booleanCheck if ToolContext contains a specific key.
-
Method Details
-
getConfig
public static Optional<RunnableConfig> getConfig(@Nullable org.springframework.ai.chat.model.ToolContext toolContext) Get the RunnableConfig from ToolContext.This is a convenience method equivalent to:
toolContext.getContext().get(ToolContextConstants.AGENT_CONFIG_CONTEXT_KEY)- Parameters:
toolContext- the tool context- Returns:
- Optional containing RunnableConfig if present
-
getState
public static Optional<OverAllState> getState(@Nullable org.springframework.ai.chat.model.ToolContext toolContext) Get the agent state (OverAllState) from ToolContext.This is a convenience method equivalent to:
toolContext.getContext().get(ToolContextConstants.AGENT_STATE_CONTEXT_KEY)- Parameters:
toolContext- the tool context- Returns:
- Optional containing OverAllState if present
-
getStateForUpdate
public static Optional<Map<String,Object>> getStateForUpdate(@Nullable org.springframework.ai.chat.model.ToolContext toolContext) Get the state map for update from ToolContext. This is used when the tool needs to modify state.This is a convenience method equivalent to:
toolContext.getContext().get(ToolContextConstants.AGENT_STATE_FOR_UPDATE_CONTEXT_KEY)- Parameters:
toolContext- the tool context- Returns:
- Optional containing state map if present
-
getMetadata
public static <T> Optional<T> getMetadata(@Nullable org.springframework.ai.chat.model.ToolContext toolContext, String key, Class<T> type) Get metadata value by key with type safety.Works with both framework keys and your custom keys:
// Framework key via constant Optional<RunnableConfig> config = ToolContextHelper.getMetadata( toolContext, ToolContextConstants.AGENT_CONFIG_CONTEXT_KEY, RunnableConfig.class); // Your custom key Optional<String> myData = ToolContextHelper.getMetadata(toolContext, "my_key", String.class);- Type Parameters:
T- the type of the metadata value- Parameters:
toolContext- the tool contextkey- the metadata key (can be fromToolContextConstantsor custom)type- the expected type class- Returns:
- Optional containing typed value if present and type matches
-
getMetadataOrDefault
public static <T> T getMetadataOrDefault(@Nullable org.springframework.ai.chat.model.ToolContext toolContext, String key, Class<T> type, T defaultValue) Get metadata value with default fallback.- Type Parameters:
T- the type of the metadata value- Parameters:
toolContext- the tool contextkey- the metadata keytype- the expected type classdefaultValue- the default value if key not found or type mismatch- Returns:
- the value if present and type matches, otherwise defaultValue
-
hasKey
public static boolean hasKey(@Nullable org.springframework.ai.chat.model.ToolContext toolContext, String key) Check if ToolContext contains a specific key.- Parameters:
toolContext- the tool contextkey- the key to check- Returns:
- true if the key exists
-
getAllContext
public static Map<String,Object> getAllContext(@Nullable org.springframework.ai.chat.model.ToolContext toolContext) Get all context data as a Map. Returns empty map if context is null.- Parameters:
toolContext- the tool context- Returns:
- unmodifiable view of context data, or empty map
-