Skip to content

[Feature]:Support Polymorphic Type Lookup in ContextStore for Tool Reuse Across Agents #1679

Open

Description

@AlbertoWang

[Feature]: Support Polymorphic Type Lookup in ContextStore for Tool Reuse Across Agents


Is your feature request related to a problem? Please describe.

When building multiple agents that each carry their own context type (e.g., AgentAContext extends BaseContext, AgentBContext extends BaseContext), we want to reuse the same tool class across all these agents. The tool should be able to retrieve the context via a shared parent type (e.g., BaseContext.class), without knowing the concrete agent context type.

Currently, ContextStore.get(Class<T> type) performs exact class matching against the internal Class → (Key → Object) map. If an agent registers an AgentAContext instance, calling store.get(BaseContext.class) returns null, even though AgentAContext extends BaseContext.

// Agent A setup
store.register("ctx", new AgentAContext());  // stored under AgentAContext.class

// Inside a reusable tool
BaseContext ctx = store.get(BaseContext.class);  // returns null — expected a child instance

This forces us to either write separate tool implementations per agent context type, or resort to fragile workarounds, both of which defeat the purpose of having a generic, reusable tool layer.

Describe the solution you'd like

Enhance ContextStore.get(Class<T> type) (and the keyed variant get(String key, Class<T> type)) to support polymorphic / assignable type lookup:

  1. First attempt an exact type match (preserving current behavior and performance).
  2. If no exact match is found, iterate over stored entries and return the first object whose type is assignable to the requested type (i.e., requestedType.isAssignableFrom(storedType)).
  3. The same logic should apply to the contains() methods for consistency.
// After the enhancement
store.register("ctx", new AgentAContext());  // AgentAContext extends BaseContext

BaseContext ctx = store.get(BaseContext.class);  // returns the AgentAContext instance
boolean exists = store.contains(BaseContext.class);  // returns true

If multiple stored types match the requested parent type, implementations could either return the first match or throw an ambiguity exception — either approach is acceptable as long as the behavior is documented.

Describe alternatives you've considered

  1. Register under a common key/type: All agents explicitly register their context under BaseContext.class. This works but requires manual discipline and breaks if a developer forgets to convert.
  2. Type-erased Object retrieval: Store and retrieve as Object, then cast manually in each tool. This defeats the type-safety that ContextStore provides.
  3. Separate tool per agent context type: Write AgentATool, AgentBTool, etc., each referencing its own context type. This leads to massive code duplication and is not maintainable.

Additional context

  • ContextStore is currently marked @Deprecated in favor of io.agentscope.core.agent.RuntimeContext. If RuntimeContext provides a similar type-based retrieval API, the same polymorphic lookup capability should be considered there as well, since this is a fundamental need for any context-aware tool reuse pattern.
  • The current DefaultContextStore uses a Map<Class<?>, Map<String, Object>> structure. Adding assignable-type fallback only requires iterating over the outer map keys when the exact lookup misses, which has minimal performance impact given the typically small number of stored context types.

Activity

  1. added
    enhancement
    on Jun 9, 2026
  2. changed the title [-][Feature]:[/-] [+][Feature]:Support Polymorphic Type Lookup in ContextStore for Tool Reuse Across Agents[/+] on Jun 9, 2026
  3. added
    area/core/agent
    area/harness
    on Jun 9, 2026
  4. moved this from Backlog to In progress in AgentScope Javaon Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/agentarea/harnessenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions