@@ -145,11 +145,19 @@ bearer token:
145145public class MyToolsService {
146146
147147 // Note: you can also use Spring AI's @Tool
148- @McpTool (name = " greeter" , description = " A tool that greets a user, by name" )
149148 @PreAuthorize (" isAuthenticated()" )
150- public String greet (@ToolParam (description = " The name of the user" ) String name , ToolContext toolContext ) {
151- return " Hello, " + name + " !" ;
152-
149+ @McpTool (name = " greeter" , description = " A tool that greets you, in the selected language" )
150+ public String greet (
151+ @ToolParam (description = " The language for the greeting (example: english, french, ...)" ) String language
152+ ) {
153+ if (! StringUtils . hasText(language)) {
154+ language = " " ;
155+ }
156+ return switch (language. toLowerCase()) {
157+ case " english" - > " Hello you!" ;
158+ case " french" - > " Salut toi!" ;
159+ default - > " I don't understand language \" %s\" . So I'm just going to say Hello!" . formatted(language);
160+ };
153161 }
154162
155163}
@@ -160,12 +168,22 @@ Note that you can also access the current authentication directly from the tool
160168
161169``` java
162170
163- @McpTool (name = " greeter" , description = " A tool that greets a user, by name" )
171+ @McpTool (name = " greeter" , description = " A tool that greets the user by name, in the selected language " )
164172@PreAuthorize (" isAuthenticated()" )
165- public String greet(ToolContext toolContext) {
173+ public String greet(
174+ @ToolParam (description = " The language for the greeting (example: english, french, ...)" ) String language
175+ ) {
176+ if (! StringUtils . hasText(language)) {
177+ language = " " ;
178+ }
166179 var authentication = SecurityContextHolder . getContext(). getAuthentication();
167- return " Hello, " + authentication. getName() + " !" ;
168-
180+ var name = authentication. getName();
181+ return switch (language. toLowerCase()) {
182+ case " english" - > " Hello, %s!" . formatted(name);
183+ case " french" - > " Salut %s!" . formatted(name);
184+ default - > (" I don't understand language \" %s\" . " +
185+ " So I'm just going to say Hello %s!" ). formatted(language, name);
186+ };
169187}
170188```
171189
0 commit comments