spring-projects / spring-projects/spring-ai
Unable to use Tools - No @Tool annotated methods found
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 9.5k
- Forks
- 2.9k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 6
Description
Bug description
Two parts in this issue
- When using toolObjects
- when using toolNames
1 ) ToolObjects -
Lets say I have created a class MyDbTools and its a component and it has a service class autowired to help in tool logic execution.
`@Component
public class MyDbTools {
@Autowired
private MyDBService myDbService;
@Tool(description = "Execute SQL Query on DB")
String runSQL(@ToolParam(description = "valid SQL query that needs to be run against database") String sql) {
System.out.println("Running sql query");
myDbService.run(sql);
..... .. . .. ... .
}
}
When I am trying to configure toolObjects on chatClient, I cannot directly instantiate like new MyDbTools() as it has a autowire. Hence I do it as shown below (.tools(myDbTools))
`@Autowired
private MyDbTools myDbTools;
. . . .
String response = chatClient.prompt()
.options(chatOptions)
.system(enhancedPrompt)
.user(request.getUserQuery())
.tools(myDbTools)
.call()
.content();
But this results in error inside MethodToolCallbackProvider -> assertToolAnnotatedMethodsPresent saying
'No @Tool annotated methods found in com.test.xyz.ai.tools.MyDbTools$$SpringCGLIB$$0@5d41afe4.Did you mean to pass a ToolCallback or ToolCallbackProvider? If so, you have to use .toolCallbacks() instead of .tool()' error.
But I do have @ tool defined already inside it.
This method ReflectionUtils.getDeclaredMethods returns 8 methods, none having annotation called Tool. Hence it fails. I suspect something to do with spring proxy objects for beans and how they are being handled.
If I configure like shown below (where 'DateTimeTools' is a simple class with one tool and no Autowired classes. It works just fine.
`String response = chatClient.prompt()
.options(chatOptions)
.system(enhancedPrompt)
.user(request.getUserQuery())
.tools(new DateTimeTools())
.call()
.content();
Can somebody let me know is this a bug or am I doing something wrong?
### 2nd Part of issue with tool calling
2. ToolNames
If I configure chatOptions with toolNames
@Component
public class DateTimeTools {
@Tool(description = "Set a user alarm for the given time")
void setAlarm(@ToolParam(description = "Time in ISO-8601 format") String time) {
LocalDateTime alarmTime = LocalDateTime.parse(time, DateTimeFormatter.ISO_DATE_TIME);
System.out.println("Alarm set for " + alarmTime);
}
}
And call llm
`String response = chatClient.prompt()
.options(chatOptions)
.system(enhancedPrompt)
.user(request.getUserQuery())
.toolNames("setAlarm")
.call()
.content();
Then it throws error at runtime saying -
Caused by: java.lang.IllegalStateException: No ToolCallback found for tool name: setAlarm
at org.springframework.ai.model.tool.DefaultToolCallingManager.resolveToolDefinitions(DefaultToolCallingManager.java:111) ~[spring-ai-model-1.0.0.jar:1.0.0]
at org.springframework.ai.ollama.OllamaChatModel.ollamaChatRequest(OllamaChatModel.java:473) ~[spring-ai-ollama-1.0.0.jar:1.0.0]
Isnt the expectation of it resolving automatically based on toolName correct?
https://docs.spring.io/spring-ai/reference/api/tools.html
Environment
spring AI version: 1.0.0
Java 21
Spring boot 3.3.4
Steps to reproduce
As shown above, create a class for tool, and use it in chatClient interface and run the application.
Expected behavior
Tools should be detected and be available to chatModels.
Please do let me know if additional information is required.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with MethodToolCallbackProvider and its assertToolAnnotatedMethodsPresent path, then follow DefaultToolCallingManager.resolveToolDefinitions and OllamaChatModel. Reproduce both cases from the issue: a Spring-proxied tool object with an autowired service and a toolNames("setAlarm") lookup. Done means annotated methods are detected on the proxied bean and the named tool resolves as documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- ai, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100