|
@@ -0,0 +1,32 @@
|
|
|
|
|
+package space.anyi.springAiAlibabaLearn.controller;
|
|
|
|
|
+
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.ai.chat.client.ChatClient;
|
|
|
|
|
+import org.springframework.ai.chat.model.ChatModel;
|
|
|
|
|
+import org.springframework.ai.tool.ToolCallbackProvider;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@RequestMapping("/mcp")
|
|
|
|
|
+public class MCPController {
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(MCPController.class);
|
|
|
|
|
+ private final ChatClient chatClient;
|
|
|
|
|
+
|
|
|
|
|
+ public MCPController(ChatModel chatModel, ToolCallbackProvider toolCallbackProvider) {
|
|
|
|
|
+ //构建chatClient,配置使用MCP server提供的tool
|
|
|
|
|
+ this.chatClient = ChatClient.builder(chatModel)
|
|
|
|
|
+ .defaultToolCallbacks(toolCallbackProvider)
|
|
|
|
|
+ .build();
|
|
|
|
|
+ }
|
|
|
|
|
+ @GetMapping("/test")
|
|
|
|
|
+ public String MCPTest(@RequestParam("message")String message){
|
|
|
|
|
+ log.debug("message:{}",message);
|
|
|
|
|
+ return chatClient.prompt(message)
|
|
|
|
|
+ .call()
|
|
|
|
|
+ .content();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|