Ver Fonte

feat:完成第三方MCP Server的调用;

yangyi há 7 meses atrás
pai
commit
c059a61635

+ 11 - 0
spring-ai-learn/src/main/java/space/anyi/springAiAlibabaLearn/controller/MCPController.java

@@ -4,12 +4,15 @@ 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.chat.prompt.PromptTemplate;
 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;
 
+import java.util.Map;
+
 @RestController
 @RequestMapping("/mcp")
 public class MCPController {
@@ -29,4 +32,12 @@ public class MCPController {
                 .call()
                 .content();
     }
+    @GetMapping("/fetch")
+    public String fetchTest(@RequestParam("url")String url){
+        log.debug("url:{}",url);
+        PromptTemplate promptTemplate = PromptTemplate.builder().template("总结网页的消息:{url}.").variables(Map.of("url", url)).build();
+        return chatClient.prompt(promptTemplate.create())
+                .call()
+                .content();
+    }
 }

+ 5 - 0
spring-ai-learn/src/main/resources/application.yaml

@@ -56,6 +56,11 @@ spring:
 #            配置mcp的连接消息,名称可以自定义
             yangyi-mcp-server:
               url: http://localhost:10001
+#              使用第三方提供的MCP server
+            fetch:
+#              modelscope的抓取网页MCP server
+              url: https://mcp.api-inference.modelscope.net
+              sse-endpoint: /07e4eb7504c34c/sse
 logging:
   level:
     root: DEBUG