Bladeren bron

调用智能体mcp服务接口

lamphua 3 dagen geleden
bovenliggende
commit
599e51bfbb

+ 99
- 3
llm-back/ruoyi-agent/pom.xml Bestand weergeven

@@ -8,6 +8,7 @@
8 8
         <version>3.8.9</version>
9 9
     </parent>
10 10
     <modelVersion>4.0.0</modelVersion>
11
+    <packaging>jar</packaging>
11 12
 
12 13
     <artifactId>ruoyi-agent</artifactId>
13 14
 
@@ -15,21 +16,116 @@
15 16
         cmc智能体
16 17
     </description>
17 18
 
19
+    <properties>
20
+        <solon.version>3.3.3</solon.version>
21
+    </properties>
22
+
18 23
     <dependencies>
19 24
 
20 25
         <dependency>
21
-            <groupId>com.ruoyi</groupId>
22
-            <artifactId>ruoyi-common</artifactId>
26
+            <groupId>org.noear</groupId>
27
+            <artifactId>solon-lib</artifactId>
28
+        </dependency>
29
+
30
+        <dependency>
31
+            <groupId>org.noear</groupId>
32
+            <artifactId>solon-web-servlet</artifactId>
23 33
         </dependency>
24 34
 
35
+        <!-- Spring Boot Web -->
36
+        <dependency>
37
+            <groupId>org.springframework.boot</groupId>
38
+            <artifactId>spring-boot-starter-web</artifactId>
39
+        </dependency>
25 40
 
26 41
         <!-- Solon模型上下文协议 -->
27 42
         <dependency>
28 43
             <groupId>org.noear</groupId>
29 44
             <artifactId>solon-ai-mcp</artifactId>
30
-            <version>3.3.1</version>
45
+            <exclusions>
46
+                <exclusion>
47
+                    <groupId>org.slf4j</groupId>
48
+                    <artifactId>*</artifactId>
49
+                </exclusion>
50
+            </exclusions>
31 51
         </dependency>
32 52
 
33 53
     </dependencies>
34 54
 
55
+    <dependencyManagement>
56
+        <dependencies>
57
+            <dependency>
58
+                <groupId>org.noear</groupId>
59
+                <artifactId>solon-parent</artifactId>
60
+                <version>${solon.version}</version>
61
+                <type>pom</type>
62
+                <scope>import</scope>
63
+            </dependency>
64
+        </dependencies>
65
+    </dependencyManagement>
66
+
67
+    <build>
68
+        <finalName>${project.name}</finalName>
69
+        <plugins>
70
+            <plugin>
71
+                <groupId>org.apache.maven.plugins</groupId>
72
+                <artifactId>maven-compiler-plugin</artifactId>
73
+                <version>3.11.0</version>
74
+                <configuration>
75
+                    <compilerArgument>-parameters</compilerArgument>
76
+                    <source>8</source>
77
+                    <target>8</target>
78
+                    <encoding>UTF-8</encoding>
79
+                </configuration>
80
+            </plugin>
81
+
82
+            <plugin>
83
+                <groupId>org.apache.maven.plugins</groupId>
84
+                <artifactId>maven-assembly-plugin</artifactId>
85
+                <configuration>
86
+                    <finalName>${project.name}</finalName>
87
+                    <appendAssemblyId>false</appendAssemblyId>
88
+                    <descriptorRefs>
89
+                        <descriptorRef>jar-with-dependencies</descriptorRef>
90
+                    </descriptorRefs>
91
+                    <archive>
92
+                        <manifest>
93
+                            <mainClass>webapp.HelloApp</mainClass>
94
+                        </manifest>
95
+                    </archive>
96
+                </configuration>
97
+                <executions>
98
+                    <execution>
99
+                        <id>make-assembly</id>
100
+                        <phase>package</phase>
101
+                        <goals>
102
+                            <goal>single</goal>
103
+                        </goals>
104
+                    </execution>
105
+                </executions>
106
+            </plugin>
107
+        </plugins>
108
+    </build>
109
+
110
+
111
+    <repositories>
112
+        <repository>
113
+            <id>sonatype-snapshots</id>
114
+            <name>Sonatype Snapshots</name>
115
+            <url>https://central.sonatype.com/repository/maven-snapshots/</url>
116
+            <releases>
117
+                <enabled>false</enabled>
118
+            </releases>
119
+        </repository>
120
+    </repositories>
121
+    <pluginRepositories>
122
+        <pluginRepository>
123
+            <id>sonatype-snapshots</id>
124
+            <name>Sonatype Snapshots</name>
125
+            <url>https://central.sonatype.com/repository/maven-snapshots/</url>
126
+            <releases>
127
+                <enabled>false</enabled>
128
+            </releases>
129
+        </pluginRepository>
130
+    </pluginRepositories>
35 131
 </project>

+ 3
- 6
llm-back/ruoyi-agent/src/main/java/com/ruoyi/agent/RuoYiAgentApplication.java Bestand weergeven

@@ -1,22 +1,19 @@
1 1
 package com.ruoyi.agent;
2 2
 
3
-import org.noear.solon.Solon;
3
+import org.springframework.boot.SpringApplication;
4 4
 import org.springframework.boot.autoconfigure.SpringBootApplication;
5
-import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
6 5
 
7 6
 /**
8 7
  * 启动程序
9 8
  * 
10 9
  * @author ruoyi
11 10
  */
12
-@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
11
+@SpringBootApplication
13 12
 public class RuoYiAgentApplication
14 13
 {
15 14
     public static void main(String[] args)
16 15
     {
17
-        Solon.start(RuoYiAgentApplication.class, args, app -> {
18
-            app.cfg().setProperty("server.port", "8081");
19
-        });
16
+        SpringApplication.run(RuoYiAgentApplication.class, args);
20 17
         System.out.println("智能体启动成功 \n");
21 18
     }
22 19
 }

+ 73
- 0
llm-back/ruoyi-agent/src/main/java/com/ruoyi/agent/service/McpServerConfig.java Bestand weergeven

@@ -0,0 +1,73 @@
1
+package com.ruoyi.agent.service;
2
+
3
+import org.noear.solon.Solon;
4
+import org.noear.solon.ai.chat.tool.MethodToolProvider;
5
+import org.noear.solon.ai.mcp.server.McpServerEndpointProvider;
6
+import org.noear.solon.ai.mcp.server.annotation.McpServerEndpoint;
7
+import org.noear.solon.ai.mcp.server.prompt.MethodPromptProvider;
8
+import org.noear.solon.ai.mcp.server.resource.MethodResourceProvider;
9
+import org.noear.solon.web.servlet.SolonServletFilter;
10
+import org.springframework.aop.support.AopUtils;
11
+import org.springframework.boot.web.servlet.FilterRegistrationBean;
12
+import org.springframework.context.annotation.Bean;
13
+import org.springframework.context.annotation.Configuration;
14
+import org.springframework.core.annotation.AnnotationUtils;
15
+
16
+import javax.annotation.PostConstruct;
17
+import javax.annotation.PreDestroy;
18
+import java.util.List;
19
+
20
+/**
21
+ * 这个类独立一个目录,可以让 Solon 扫描范围最小化
22
+ * */
23
+@Configuration
24
+public class McpServerConfig {
25
+    @PostConstruct
26
+    public void start() {
27
+        Solon.start(McpServerConfig.class, new String[]{});
28
+    }
29
+
30
+    @PreDestroy
31
+    public void stop() {
32
+        if (Solon.app() != null) {
33
+            Solon.stopBlock(false, Solon.cfg().stopDelay());
34
+        }
35
+    }
36
+
37
+    @Bean
38
+    public McpServerConfig init(List<IMcpService> serverEndpoints) {
39
+        //提取实现容器里 IMcpService 接口的 bean ,并注册为服务端点
40
+        for (IMcpService serverEndpoint : serverEndpoints) {
41
+            Class<?> serverEndpointClz = AopUtils.getTargetClass(serverEndpoint);
42
+            McpServerEndpoint anno = AnnotationUtils.findAnnotation(serverEndpointClz, McpServerEndpoint.class);
43
+
44
+            if (anno == null) {
45
+                continue;
46
+            }
47
+
48
+            McpServerEndpointProvider serverEndpointProvider = McpServerEndpointProvider.builder()
49
+                    .from(serverEndpointClz, anno)
50
+                    .build();
51
+
52
+            serverEndpointProvider.addTool(new MethodToolProvider(serverEndpointClz, serverEndpoint));
53
+            serverEndpointProvider.addResource(new MethodResourceProvider(serverEndpointClz, serverEndpoint));
54
+            serverEndpointProvider.addPrompt(new MethodPromptProvider(serverEndpointClz, serverEndpoint));
55
+
56
+            serverEndpointProvider.postStart();
57
+
58
+            //可以再把 serverEndpointProvider 手动转入 SpringBoot 容器
59
+        }
60
+
61
+        //为了能让这个 init 能正常运行
62
+        return this;
63
+    }
64
+
65
+    @Bean
66
+    public FilterRegistrationBean mcpServerFilter() {
67
+        FilterRegistrationBean<SolonServletFilter> filter = new FilterRegistrationBean<>();
68
+        filter.setName("SolonFilter");
69
+        filter.addUrlPatterns("/llm/*");
70
+        filter.setFilter(new SolonServletFilter());
71
+        return filter;
72
+    }
73
+}

+ 0
- 7
llm-back/ruoyi-agent/src/main/java/com/ruoyi/agent/service/impl/McpServiceImpl.java Bestand weergeven

@@ -1,7 +1,6 @@
1 1
 package com.ruoyi.agent.service.impl;
2 2
 
3 3
 import com.ruoyi.agent.service.IMcpService;
4
-
5 4
 import org.noear.solon.ai.annotation.PromptMapping;
6 5
 import org.noear.solon.ai.annotation.ResourceMapping;
7 6
 import org.noear.solon.ai.annotation.ToolMapping;
@@ -13,16 +12,10 @@ import org.springframework.stereotype.Service;
13 12
 import java.util.Arrays;
14 13
 import java.util.Collection;
15 14
 
16
-/**
17
- * 自动构建服务端点服务(使用 springboot 容器)
18
- * */
19 15
 @Service
20 16
 @McpServerEndpoint(sseEndpoint = "/llm/mcp/sse")
21 17
 public class McpServiceImpl implements IMcpService {
22 18
 
23
-    //
24
-    // 建议开启编译参数:-parameters (否则,最好再配置参数的 name)
25
-    //
26 19
     @ToolMapping(description = "查询天气预报")
27 20
     public String getWeather(@Param(description = "城市位置") String location) {
28 21
         return "晴,14度";

Laden…
Annuleren
Opslaan