Skip to content

Using @Tool annotation for Function beans? #2632

Open
@markpollack

Description

@markpollack

Discussed in #2581

Originally posted by henningSaulCM March 27, 2025
We have a bunch of tools implemented as Functions and instantiated as Spring Beans (with a custom annotation), e.g.:

  @AcmeTool
  public BiFunction<SomeAcmeTool.Request, ToolContext, AcmeToolResponse<Record>> someAcmeTool(SomeService someService) {
    return new SomeAcmeTool(someService);
 }

To register the tools, we've implemented a custom ToolCallbackProvider that looks for this custom annotation:

public FunctionCallback[] getToolCallbacks() {
    List<FunctionCallback> tools = new ArrayList<>();
    // create ToolCallback for each AcmeTool
    beanFactory.getBeansWithAnnotation(AcmeTool.class).forEach((beanName, acmeTool) -> {
      try {
        ToolCallback toolCallback = springBeanToolCallbackResolver.resolve(beanName);
        tools.add(toolCallback);
      } catch (Exception e) {
        LOG.error("Failed to create ToolCallback for AcmeTool bean '{}'", beanName, e);
        throw e;
      }

    });
    return tools.toArray(new FunctionCallback[0]);
  }

We're wondering if there's a better way...

Can the Spring AI tool annotation somehow be used instead of our custom annotation for our Function beans?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions