23
23
import org .junit .jupiter .api .Test ;
24
24
25
25
import org .springframework .ai .chat .model .ToolContext ;
26
+ import org .springframework .ai .tool .annotation .ToolParam ;
26
27
import org .springframework .ai .tool .definition .DefaultToolDefinition ;
27
28
import org .springframework .ai .tool .definition .ToolDefinition ;
28
29
@@ -173,6 +174,117 @@ void testToolContextType() throws Exception {
173
174
assertThat (result ).isEqualTo ("1 entries processed {foo=bar}" );
174
175
}
175
176
177
+ @ Test
178
+ void testToolParamAnnotationValueUsedAsBindingKey () throws Exception {
179
+ TestGenericClass testObject = new TestGenericClass ();
180
+ Method method = TestGenericClass .class .getMethod ("greetWithAlias" , String .class );
181
+
182
+ ToolDefinition toolDefinition = DefaultToolDefinition .builder ()
183
+ .name ("greet" )
184
+ .description ("Greet a user with alias binding" )
185
+ .inputSchema ("{}" )
186
+ .build ();
187
+
188
+ MethodToolCallback callback = MethodToolCallback .builder ()
189
+ .toolDefinition (toolDefinition )
190
+ .toolMethod (method )
191
+ .toolObject (testObject )
192
+ .build ();
193
+
194
+ String toolInput = """
195
+ {
196
+ "user_name": "Alice"
197
+ }
198
+ """ ;
199
+
200
+ String result = callback .call (toolInput );
201
+
202
+ assertThat (result ).isEqualTo ("\" Hello, Alice\" " );
203
+ }
204
+
205
+ @ Test
206
+ void testToolParamEmptyValueUsesParameterName () throws Exception {
207
+ TestGenericClass testObject = new TestGenericClass ();
208
+ Method method = TestGenericClass .class .getMethod ("greet" , String .class );
209
+
210
+ ToolDefinition toolDefinition = DefaultToolDefinition .builder ()
211
+ .name ("greet" )
212
+ .description ("Greet a user with implicit binding" )
213
+ .inputSchema ("{}" )
214
+ .build ();
215
+
216
+ MethodToolCallback callback = MethodToolCallback .builder ()
217
+ .toolDefinition (toolDefinition )
218
+ .toolMethod (method )
219
+ .toolObject (testObject )
220
+ .build ();
221
+
222
+ String toolInput = """
223
+ {
224
+ "name": "Bob"
225
+ }
226
+ """ ;
227
+
228
+ String result = callback .call (toolInput );
229
+
230
+ assertThat (result ).isEqualTo ("\" Hello, Bob\" " );
231
+ }
232
+
233
+ @ Test
234
+ void testToolParamMissingInputHandledAsNull () throws Exception {
235
+ TestGenericClass testObject = new TestGenericClass ();
236
+ Method method = TestGenericClass .class .getMethod ("greetWithAlias" , String .class );
237
+
238
+ ToolDefinition toolDefinition = DefaultToolDefinition .builder ()
239
+ .name ("greet" )
240
+ .description ("Greet a user with missing input" )
241
+ .inputSchema ("{}" )
242
+ .build ();
243
+
244
+ MethodToolCallback callback = MethodToolCallback .builder ()
245
+ .toolDefinition (toolDefinition )
246
+ .toolMethod (method )
247
+ .toolObject (testObject )
248
+ .build ();
249
+
250
+ String toolInput = """
251
+ {}
252
+ """ ;
253
+
254
+ String result = callback .call (toolInput );
255
+
256
+ assertThat (result ).isEqualTo ("\" Hello, null\" " );
257
+ }
258
+
259
+ @ Test
260
+ void testMultipleToolParamsBinding () throws Exception {
261
+ TestGenericClass testObject = new TestGenericClass ();
262
+ Method method = TestGenericClass .class .getMethod ("greetFullName" , String .class , String .class );
263
+
264
+ ToolDefinition toolDefinition = DefaultToolDefinition .builder ()
265
+ .name ("greetFullName" )
266
+ .description ("Greet a user by full name" )
267
+ .inputSchema ("{}" )
268
+ .build ();
269
+
270
+ MethodToolCallback callback = MethodToolCallback .builder ()
271
+ .toolDefinition (toolDefinition )
272
+ .toolMethod (method )
273
+ .toolObject (testObject )
274
+ .build ();
275
+
276
+ String toolInput = """
277
+ {
278
+ "first": "Jane",
279
+ "last": "Doe"
280
+ }
281
+ """ ;
282
+
283
+ String result = callback .call (toolInput );
284
+
285
+ assertThat (result ).isEqualTo ("\" Hello, Jane Doe\" " );
286
+ }
287
+
176
288
/**
177
289
* Test class with methods that use generic types.
178
290
*/
@@ -195,6 +307,18 @@ public String processStringListInToolContext(ToolContext toolContext) {
195
307
return context .size () + " entries processed " + context ;
196
308
}
197
309
310
+ public String greetWithAlias (@ ToolParam ("user_name" ) String name ) {
311
+ return "Hello, " + name ;
312
+ }
313
+
314
+ public String greet (@ ToolParam String name ) {
315
+ return "Hello, " + name ;
316
+ }
317
+
318
+ public String greetFullName (@ ToolParam ("first" ) String first , @ ToolParam ("last" ) String last ) {
319
+ return "Hello, " + first + " " + last ;
320
+ }
321
+
198
322
}
199
323
200
324
}
0 commit comments