22
22
23
23
import org .junit .jupiter .api .Test ;
24
24
25
+ import org .springframework .ai .chat .model .ToolContext ;
25
26
import org .springframework .ai .tool .definition .DefaultToolDefinition ;
26
27
import org .springframework .ai .tool .definition .ToolDefinition ;
27
28
@@ -137,6 +138,41 @@ void testNestedGenericType() throws Exception {
137
138
assertThat (result ).isEqualTo ("2 maps processed: [{a=1, b=2}, {c=3, d=4}]" );
138
139
}
139
140
141
+ @ Test
142
+ void testToolContextType () throws Exception {
143
+ // Create a test object with a method that takes a List<Map<String, Integer>>
144
+ TestGenericClass testObject = new TestGenericClass ();
145
+ Method method = TestGenericClass .class .getMethod ("processStringListInToolContext" , ToolContext .class );
146
+
147
+ // Create a tool definition
148
+ ToolDefinition toolDefinition = DefaultToolDefinition .builder ()
149
+ .name ("processToolContext" )
150
+ .description ("Process tool context" )
151
+ .inputSchema ("{}" )
152
+ .build ();
153
+
154
+ // Create a MethodToolCallback
155
+ MethodToolCallback callback = MethodToolCallback .builder ()
156
+ .toolDefinition (toolDefinition )
157
+ .toolMethod (method )
158
+ .toolObject (testObject )
159
+ .build ();
160
+
161
+ // Create an empty JSON input
162
+ String toolInput = """
163
+ {}
164
+ """ ;
165
+
166
+ // Create a toolContext
167
+ ToolContext toolContext = new ToolContext (Map .of ("foo" , "bar" ));
168
+
169
+ // Call the tool
170
+ String result = callback .call (toolInput , toolContext );
171
+
172
+ // Verify the result
173
+ assertThat (result ).isEqualTo ("1 entries processed {foo=bar}" );
174
+ }
175
+
140
176
/**
141
177
* Test class with methods that use generic types.
142
178
*/
@@ -154,6 +190,11 @@ public String processListOfMaps(List<Map<String, Integer>> listOfMaps) {
154
190
return listOfMaps .size () + " maps processed: " + listOfMaps ;
155
191
}
156
192
193
+ public String processStringListInToolContext (ToolContext toolContext ) {
194
+ Map <String , Object > context = toolContext .getContext ();
195
+ return context .size () + " entries processed " + context ;
196
+ }
197
+
157
198
}
158
199
159
200
}
0 commit comments