2222
2323import org .junit .jupiter .api .Test ;
2424
25+ import org .springframework .ai .chat .model .ToolContext ;
2526import org .springframework .ai .tool .definition .DefaultToolDefinition ;
2627import org .springframework .ai .tool .definition .ToolDefinition ;
2728
@@ -137,6 +138,41 @@ void testNestedGenericType() throws Exception {
137138 assertThat (result ).isEqualTo ("2 maps processed: [{a=1, b=2}, {c=3, d=4}]" );
138139 }
139140
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+
140176 /**
141177 * Test class with methods that use generic types.
142178 */
@@ -154,6 +190,11 @@ public String processListOfMaps(List<Map<String, Integer>> listOfMaps) {
154190 return listOfMaps .size () + " maps processed: " + listOfMaps ;
155191 }
156192
193+ public String processStringListInToolContext (ToolContext toolContext ) {
194+ Map <String , Object > context = toolContext .getContext ();
195+ return context .size () + " entries processed " + context ;
196+ }
197+
157198 }
158199
159200}
0 commit comments