Spring AI 结合 GitHub MCP 学习记录
1.简介
本文档记录了参考MCP-GitHub使用示例使用 Spring AI 框架集成 GitHub 作为 MCP (Model Context Protocol) 工具的过程。通过 MCP,AI 模型可以调用外部工具(如此处的 GitHub API)来获取实时信息或执行操作。
2. Maven 依赖配置 (pom.xml)
首先,我们需要在项目的 pom.xml
文件中添加必要的依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 父项目,继承 Spring Boot 的默认配置 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version> <!-- Spring Boot 版本 -->
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.miku</groupId>
<artifactId>spring-ai-alibaba-mcp-client</artifactId>
<version>0.0.1-SNAPSHOT</version> <!-- 项目版本 -->
<name>spring-ai-alibaba-mcp-client</name> <!-- 项目名称 -->
<description>spring-ai-alibaba-mcp-client</description> <!-- 项目描述 -->
<!-- 许可证、开发者、SCM 等信息,当前为空 -->
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<!-- 项目属性,用于管理版本号等 -->
<properties>
<java.version>17</java.version> <!-- Java 版本 -->
<!-- Spring AI 版本 -->
<spring-ai.version>1.0.0-M6</spring-ai.version>
<!-- Spring AI Alibaba Starter 版本 -->
<spring-ai-alibaba.version>1.0.0-M6.1</spring-ai-alibaba.version>
</properties>
<!-- 项目依赖 -->
<dependencies>
<!-- Spring Boot Web Starter,提供内嵌 Tomcat 和 Spring MVC 等 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 添加 Spring AI MCP 客户端 Starter 依赖 -->
<!-- 这个 Starter 提供了 MCP 客户端的核心自动配置和功能 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
</dependency>
<!-- 基于 Server-Sent Events (SSE)的 MCP 客户端实现 -->
<!-- 即使当前配置未使用 SSE,此依赖也提供了 SSE 传输的支持 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot DevTools,提供开发时的便利功能,如热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope> <!-- 只在运行时需要 -->
<optional>true</optional> <!-- 可选依赖 -->
</dependency>
<!-- Lombok 库,用于简化 Java 代码(如自动生成 getter/setter) -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional> <!-- 可选依赖-->
</dependency>
<!-- Spring Boot Test Starter,提供测试相关的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <!--只在测试时需要-->
</dependency>
<!-- Alibaba Cloud AI Starter,提供与阿里云 AI 服务的集成,包括 DashScope -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>${spring-ai-alibaba.version}</version> <!-- 使用属性中定义的版本-->
</dependency>
<!-- 引入 DashScope 所需依赖 (通常包含在 spring-ai-alibaba-starter 中) -->
<!-- Spring AI OpenAI Starter -->
<!-- 提供与 OpenAI API 兼容服务的集成 (如 OpenRouter)。 -->
<!-- 即使主要使用 DashScope,如果需要通过 OpenAI 兼容接口调用其他模型,也需要此依赖。 -->
<!-- 注意:同时引入 DashScope 和 OpenAI Starter 可能导致 ChatModel Bean 冲突,需要通过配置或 @Qualifier 解决。 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
<version>${spring-ai.version}</version> <!-- 使用 Spring AI BOM 中定义的版本-->
</dependency>
<!--需要在项目中接入具有 OpenAI API 规范的大模型时,只需要引入 spring-ai-openai-spring-boot-starter 即可。-->
</dependencies>
<!-- 依赖管理,使用 Spring AI BOM (Bill of Materials) 来统一管理 Spring AI 相关依赖的版本 -->
<!-- 引入 BOM 可以确保所有 Spring AI 模块使用兼容的版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version> <!-- 使用属性中定义的 Spring AI 版本-->
<type>pom</type> <!-- BOM 的类型是 pom-->
<scope>import</scope> <!-- 导入 BOM 中的依赖管理-->
</dependency>
</dependencies>
</dependencyManagement>
<!-- 构建配置 -->
<build>
<plugins>
<!-- Maven Compiler Plugin,配置 Java 编译器 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- Spring Boot Maven Plugin,用于打包可执行的 Spring Boot 应用 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
关键依赖包括:
- spring-boot-starter-web : 提供基础的 Web 功能。
- spring-ai-mcp-client-spring-boot-starter : Spring AI MCP 客户端的核心。
- spring-ai-mcp-client-webflux-spring-boot-starter : 提供基于 WebFlux (用于 SSE) 的 MCP 客户端支持。
- spring-ai-alibaba-starter : 用于集成阿里云 DashScope 模型。
- spring-ai-openai-spring-boot-starter : 用于集成 OpenAI 或兼容 API 的模型(本例中配置了 OpenRouter,但实际未使用)。
- spring-ai-bom : 统一管理 Spring AI 相关依赖的版本。
3. 应用配置 (application.yml)
接下来是 application.yml 文件,用于配置 Spring Boot 应用、Spring AI 模型以及 MCP 客户端。
# Web 服务器配置
server:
port: 10003 # 应用程序监听的端口号
# Spring 应用程序配置
spring:
application:
name: spring-ai-alibaba-mcp-client # 应用程序的名称
# Spring AI 配置
ai:
chat:
client:
enabled: false # 禁用 ChatClient.Builder Bean 的自动配置。
# 当您需要手动创建 ChatClient.Builder 并指定使用哪个 ChatModel 时(如本例中通过 @Qualifier 指定 DashScope),
# 通常需要禁用默认的自动配置,以避免 Bean 冲突或确保使用正确的 ChatModel。
# OpenAI/OpenRouter 配置 (注意:虽然配置了,但 Java 代码中通过 @Qualifier 明确使用了 DashScope)
openai:
api-key: ${AI_DeepSeek_API_KEY} # OpenAI 或兼容 OpenAI API 的服务的 API Key。
# 使用 ${...} 占位符表示从环境变量或外部配置源获取,推荐做法。
base-url: https://openrouter.ai/api/v1 # OpenAI 或兼容 OpenAI API 的服务的 Base URL。这里配置的是 OpenRouter 的地址。
chat:
options:
model: deepseek/deepseek-r1:free # 指定通过此配置使用的模型 ID。这里是 OpenRouter 上 DeepSeek 的一个免费模型。
# model: google/gemini-2.0-flash-thinking-exp:free # 其他可选的 OpenRouter 模型 (注释掉)
# model: deepseek/deepseek-chat-v3-0324:free # 其他可选的 OpenRouter 模型 (注释掉)
# model: google/gemini-2.0-flash-exp:free # 其他可选的 OpenRouter 模型 (注释掉)
# Chat 接口路径,与 OpenAI 接口保持一致
completions-path: /chat/completions # 指定聊天完成接口的路径,通常与 OpenAI API 兼容。
# Alibaba DashScope 配置 (Java 代码中通过 @Qualifier 明确使用了 DashScopeChatModel)
dashscope:
api-key: ${AI_DASHSCOPE_API_KEY} # 阿里云 DashScope 服务的 API Key。
# 使用 ${...} 占位符表示从环境变量或外部配置源获取,推荐做法。
# 注意:根据您使用的 DashScope 版本和区域,可能还需要配置 endpoint 或 region 等属性。
# 请查阅 spring-ai-alibaba-starter 的官方文档以获取完整的配置选项。
# MCP (Model Context Protocol) 客户端配置
mcp:
client:
toolcallback:
enabled: true # 启用 MCP 客户端作为 Spring AI 的 ToolCallbackProvider。
# 这使得 Spring AI 能够通过 MCP 调用外部工具。
# Stdio 传输配置 (用于启动外部进程作为 MCP 服务器并通过标准输入/输出通信)
stdio:
# 指定 MCP 服务器配置文件路径(推荐)
# classpath: 指示文件位于类路径下 (通常是 src/main/resources 目录)
servers-configuration: classpath:/mcp-servers-config.json # 指向定义要启动的 MCP 服务器进程的 JSON 配置文件。
# # SSE (Server-Sent Events) 传输配置 (用于连接到远程 MCP 服务器,当前注释掉未使用)
# sse:
# connections:
# server1:
# url:
#
# server2:
# url:
主要配置项:
- server.port : 应用运行端口。
- spring.ai.chat.client.enabled: false : 禁用了默认的 ChatClient.Builder 自动配置,因为我们将在代码中手动创建并指定模型。
- spring.ai.openai.* : 配置了 OpenRouter 的 API Key 和 Base URL(通过环境变量获取),但本例未使用。
- spring.ai.dashscope.api-key : 配置了 DashScope 的 API Key(通过环境变量获取)。
- spring.ai.mcp.client.toolcallback.enabled: true : 启用 MCP 作为工具回调提供者。
- spring.ai.mcp.client.stdio.servers-configuration : 指定了用于 Stdio 传输方式的 MCP 服务器配置文件路径。Stdio 方式意味着 Spring AI 会启动一个本地进程作为 MCP 服务器,并通过标准输入/输出进行通信。
- spring.ai.mcp.client.sse.* : 注释掉的部分是 SSE 传输方式的配置,用于连接远程运行的 MCP 服务器。
4. MCP 服务器配置 (mcp-servers-config.json)
这个 JSON 文件定义了 Spring AI 如何通过 Stdio 方式启动 GitHub MCP 服务器。
{
"mcpServers": {
"github": {
"command": "cmd",
"args": [
"/c",
"npx",
"-y",
"@modelcontextprotocol/server-github"
],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "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"
}
}
}
}
配置说明:
- mcpServers : 定义了一个名为 “github” 的 MCP 服务器。
- command : 指定启动服务器的命令(在 Windows 上是 cmd )。
- args : 传递给命令的参数。这里使用 npx -y @modelcontextprotocol/server-github 来下载并运行官方的 GitHub MCP 服务器。
- env : 设置启动服务器进程所需的环境变量。 GITHUB_PERSONAL_ACCESS_TOKEN 是必需的,用于授权 MCP 服务器访问 GitHub API。 请注意:直接在配置文件中硬编码 Token 存在安全风险,建议使用更安全的方式(如环境变量或密钥管理服务)传递。
5. Java 应用程序代码 (SpringAiAlibabaMcpClientApplication.java)
这是 Spring Boot 应用程序的主类和入口点。
package com.miku.springaialibabamcpclient;
import org.springframework.ai.chat.client.ChatClient; // 导入 Spring AI 的 ChatClient 类,用于与聊天模型交互
import org.springframework.ai.chat.model.ChatModel; // 导入 Spring AI 的 ChatModel 接口,代表一个聊天模型
import org.springframework.ai.tool.ToolCallbackProvider; // 导入 Spring AI 的 ToolCallbackProvider 接口,用于提供工具回调
import org.springframework.beans.factory.annotation.Qualifier; // 导入 Spring 的 Qualifier 注解,用于指定注入特定名称的 Bean
import org.springframework.boot.CommandLineRunner; // 导入 Spring Boot 的 CommandLineRunner 接口,用于在应用启动后执行特定代码
import org.springframework.boot.SpringApplication; // 导入 Spring Boot 的主应用类
import org.springframework.boot.autoconfigure.SpringBootApplication; // 导入 Spring Boot 的主应用注解,启用自动配置、组件扫描等
import org.springframework.context.ConfigurableApplicationContext; // 导入 Spring 的应用上下文接口,用于管理 Bean 生命周期等
import org.springframework.context.annotation.Bean; // 导入 Spring 的 Bean 注解,用于声明一个 Bean
// @SpringBootApplication 注解标记这是一个 Spring Boot 应用程序
@SpringBootApplication
public class SpringAiAlibabaMcpClientApplication {
// main 方法是应用程序的入口点
public static void main(String[] args) {
// 启动 Spring Boot 应用程序
SpringApplication.run(SpringAiAlibabaMcpClientApplication.class, args);
}
// @Bean 注解将此方法的返回值注册为 Spring 容器中的一个 Bean
// CommandLineRunner 接口使得这个 Bean 在应用程序启动后会自动执行 run 方法
@Bean
public CommandLineRunner predefineQuestions(
// @Qualifier("dashscopeChatModel") 注解指定注入名称为 "dashscopeChatModel" 的 ChatModel Bean
// 这个 Bean 通常由 spring-ai-alibaba-starter 自动配置提供,代表阿里云的 DashScope 模型
@Qualifier("dashscopeChatModel") ChatModel dashscopeChatModel,
// 注入 ToolCallbackProvider Bean,这个 Bean 通常由 spring-ai-mcp-client-spring-boot-starter 自动配置提供
// 它负责管理和调用通过 MCP 连接的工具
ToolCallbackProvider tools,
// 注入 ConfigurableApplicationContext,用于在任务完成后关闭应用上下文
ConfigurableApplicationContext context) {
// 返回一个 CommandLineRunner 实例
return args -> {
// 使用 ChatClient.builder() 创建一个 ChatClient 构建器
// 将注入的 dashscopeChatModel 设置为 ChatClient 使用的聊天模型
ChatClient.Builder builder = ChatClient.builder(dashscopeChatModel);
// 使用 defaultTools(tools) 方法将注入的 ToolCallbackProvider (MCP 客户端) 注册到 ChatClient 中
// 这使得 ChatClient 在需要时能够通过 MCP 调用工具
var chatClient = builder.defaultTools(tools).build();
// 定义用户输入的硬编码中文问题
// 硬编码可以避免配置文件编码问题,但实际应用中通常从外部获取输入
String userInput = "查询用户16Miku的github仓库每个代码仓库的详细内容?";
// 打印问题到控制台
System.out.println("Question" + userInput );
// 使用 chatClient 发送 prompt (问题)
// .call() 方法发送请求到配置的 ChatModel (DashScope)
// 如果模型判断需要使用工具,Spring AI 框架会通过注册的 ToolCallbackProvider (MCP) 调用工具
// .content() 方法获取 AI 模型返回的文本内容 (可能是直接回答或工具执行结果的总结)
// 打印 AI 模型返回的回答到控制台
System.out.println("Answer" + chatClient.prompt(userInput).call().content());
// 在任务完成后关闭 Spring 应用程序上下文
// 这对于 CommandLineRunner 类型的单次执行任务很有用,避免应用程序一直运行
context.close();
};
}
}
代码逻辑:
- @SpringBootApplication 标记主类。
- main 方法启动应用。
- predefineQuestions 方法使用 @Bean 和 CommandLineRunner 定义了一个在应用启动后执行的任务。
- 通过 @Qualifier(“dashscopeChatModel”) 注入 DashScope 聊天模型。
- 注入 ToolCallbackProvider ,它代表了通过 MCP 连接的工具(根据配置是 GitHub MCP Server)。
- 创建 ChatClient ,并将 DashScope 模型和 MCP 工具提供者 ( tools ) 关联起来。
- 定义一个查询 GitHub 仓库的问题 userInput 。
- 调用 chatClient.prompt(userInput).call().content() :
- 将问题发送给 DashScope 模型。
- DashScope 模型理解意图后,决定调用 GitHub 工具。
- Spring AI 通过 ToolCallbackProvider (MCP 客户端) 将工具调用请求发送给 Stdio 启动的 GitHub MCP 服务器。
- GitHub MCP 服务器执行查询并将结果返回给 Spring AI。
- Spring AI 将工具结果再发送给 DashScope 模型。
- DashScope 模型根据工具结果生成最终的回答。
- .content() 获取最终回答。
- 打印问题和回答。
- 关闭应用上下文。
6. 运行结果 (控制台输出)
以下是应用程序运行时的控制台输出日志和最终结果。
:: Spring Boot :: (v3.4.4)
2025-05-03T20:48:00.247+08:00 INFO 44960 --- [ restartedMain] .m.s.SpringAiAlibabaMcpClientApplication : Starting SpringAiAlibabaMcpClientApplication using Java 21.0.4 with PID 44960 (A:\study\javaee\Spring AI\spring-ai-alibaba-learn\spring-ai-alibaba-mcp\spring-ai-alibaba-mcp-client\target\classes started by in A:\study\javaee\Spring AI\spring-ai-alibaba-learn\spring-ai-alibaba-mcp)
2025-05-03T20:48:00.248+08:00 INFO 44960 --- [ restartedMain] .m.s.SpringAiAlibabaMcpClientApplication : No active profile set, falling back to 1 default profile: "default"
2025-05-03T20:48:00.459+08:00 INFO 44960 --- [ restartedMain] o.s.b.devtools.restart.ChangeableUrls : The Class-Path manifest attribute in A:\apache-maven-3.9.6-bin\mvn_resp\org\apache\opennlp\opennlp-tools\2.3.3\opennlp-tools-2.3.3.jar referenced one or more files that do not exist: file:/A:/apache-maven-3.9.6-bin/mvn_resp/org/apache/opennlp/opennlp-tools/2.3.3/slf4j-api-1.7.36.jar
2025-05-03T20:48:00.460+08:00 INFO 44960 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2025-05-03T20:48:00.460+08:00 INFO 44960 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2025-05-03T20:48:01.509+08:00 WARN 44960 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'com.alibaba.cloud.nacos.NacosConfigAutoConfiguration' of type [com.alibaba.cloud.nacos.NacosConfigAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). The currently created BeanPostProcessor [nacosAnnotationProcessor] is declared through a non-static factory method on that class; consider declaring it as static instead.
2025-05-03T20:48:01.516+08:00 WARN 44960 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'nacosConfigProperties' of type [com.alibaba.cloud.nacos.NacosConfigProperties] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected/applied to a currently created BeanPostProcessor [nacosAnnotationProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE.
2025-05-03T20:48:01.588+08:00 INFO 44960 --- [ restartedMain] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
2025-05-03T20:48:01.588+08:00 INFO 44960 --- [ restartedMain] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success.
2025-05-03T20:48:01.591+08:00 WARN 44960 --- [ restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'nacosConfigManager' of type [com.alibaba.cloud.nacos.NacosConfigManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected/applied to a currently created BeanPostProcessor [nacosAnnotationProcessor]? Check the corresponding BeanPostProcessor declaration and its dependencies/advisors. If this bean does not have to be post-processed, declare it with ROLE_INFRASTRUCTURE.
2025-05-03T20:48:02.025+08:00 INFO 44960 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 10003 (http)
2025-05-03T20:48:02.039+08:00 INFO 44960 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-05-03T20:48:02.039+08:00 INFO 44960 --- [ restartedMain] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.39]
2025-05-03T20:48:02.108+08:00 INFO 44960 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-05-03T20:48:02.109+08:00 INFO 44960 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1648 ms
2025-05-03T20:48:05.764+08:00 INFO 44960 --- [pool-5-thread-1] i.m.c.transport.StdioClientTransport : STDERR Message received: GitHub MCP Server running on stdio
2025-05-03T20:48:05.811+08:00 INFO 44960 --- [pool-2-thread-1] i.m.client.McpAsyncClient : Server response with Protocol: 2024-11-05, Capabilities: ServerCapabilities[experimental=null, logging=null, prompts=null, resources=null, tools=ToolCapabilities[listChanged=null]], Info: Implementation[name=github-mcp-server, version=0.6.2] and Instructions null
2025-05-03T20:48:06.333+08:00 INFO 44960 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2025-05-03T20:48:06.362+08:00 INFO 44960 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 10003 (http) with context path '/'
2025-05-03T20:48:06.370+08:00 INFO 44960 --- [ restartedMain] .m.s.SpringAiAlibabaMcpClientApplication : Started SpringAiAlibabaMcpClientApplication in 6.796 seconds (process running for 7.65)
Question查询用户16Miku的github仓库每个代码仓库的详细内容?
Answer下面是用户 16Miku 的所有公开代码仓库及其详细信息:
1. **MybatisPlus-Learning**
- 地址:[MybatisPlus-Learning](https://github.com/16Miku/MybatisPlus-Learning)
- 创建时间:2025-04-12T23:14:33Z
- 最后更新时间:2025-04-12T23:20:09Z
2. **sky-take-out**
- 地址:[sky-take-out](https://github.com/16Miku/sky-take-out)
- 创建时间:2025-04-12T23:24:27Z
- 最后更新时间:2025-04-12T23:25:26Z
3. **Machine-Learning**
- 地址:[Machine-Learning](https://github.com/16Miku/Machine-Learning)
- 描述:机器学习作业记录,包括KNN模型训练、TensorFlow和Keras构建的CNN模型进行水果识别、ITTI算法显著性特征提取等。
- 创建时间:2025-04-12T21:39:46Z
- 最后更新时间:2025-04-12T21:45:23Z
4. **RabbitMQ-Learning**
- 地址:[RabbitMQ-Learning](https://github.com/16Miku/RabbitMQ-Learning)
- 创建时间:2025-04-12T22:36:39Z
- 最后更新时间:2025-04-12T22:38:03Z
5. **chat2-spring-ai**
- 地址:[chat2-spring-ai](https://github.com/16Miku/chat2-spring-ai)
- 创建时间:2025-04-12T22:09:27Z
- 最后更新时间:2025-04-27T09:53:06Z
6. **bitstorm-java-project-241010** (私有仓库)
7. **Algorithm-Learning**
- 地址:[Algorithm-Learning](https://github.com/16Miku/Algorithm-Learning)
- 描述:算法题学习记录
- 创建时间:2025-04-28T11:57:27Z
- 最后更新时间:2025-05-03T03:53:29Z
8. **vanna** (私有仓库)
- 描述:大数据课程作业:基于vanna大模型和spark数据分析的医院管理系统项目
9. **MCP-Test**
- 地址:[MCP-Test](https://github.com/16Miku/MCP-Test)
- 描述:Test repository created by MCP
- 创建时间:2025-04-15T12:49:14Z
- 最后更新时间:2025-04-15T17:30:00Z
10. **spring-ai-alibaba-learn**
- 地址:[spring-ai-alibaba-learn](https://github.com/16Miku/spring-ai-alibaba-learn)
- 描述:Spring AI Alibaba学习记录
- 创建时间:2025-04-12T22:15:33Z
- 最后更新时间:2025-04-18T23:58:02Z
11. **hmall**
- 地址:[hmall](https://github.com/16Miku/hmall)
- 描述:黑马程序员SpringCloud微服务开发与实战,java黑马商城项目微服务实战开发(涵盖MybatisPlus、Docker、MQ、ES、Redis高级等)学习记录
- 创建时间:2025-04-12T22:30:36Z
- 最后更新时间:2025-04-12T22:32:51Z
12. **Streamlit-For-Yolo**
- 地址:[Streamlit-For-Yolo](https://github.com/16Miku/Streamlit-For-Yolo)
- 描述:服创作品
- 创建时间:2025-04-07T12:40:18Z
- 最后更新时间:2025-04-28T15:01:23Z
请注意,其中有一些仓库是私有的,因此无法提供它们的详细内容。其他仓库的信息已经列出,包括每个仓库的地址、描述、创建时间和最后更新时间。
2025-05-03T20:49:08.549+08:00 INFO 44960 --- [ restartedMain] o.s.b.w.e.tomcat.GracefulShutdown : Commencing graceful shutdown. Waiting for active requests to complete
2025-05-03T20:49:08.557+08:00 INFO 44960 --- [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown : Graceful shutdown complete
2025-05-03T20:49:08.679+08:00 WARN 44960 --- [onPool-worker-1] i.m.c.transport.StdioClientTransport : Process terminated with code 1
用AI解读本次运行信息
-
应用程序启动成功:
:: Spring Boot :: (v3.4.4) ... 2025-05-03T20:48:06.362+08:00 INFO 44960 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 10003 (http) with context path '/' 2025-05-03T20:48:06.370+08:00 INFO 44960 --- [ restartedMain] .m.s.SpringAiAlibabaMcpClientApplication : Started SpringAiAlibabaMcpClientApplication in 6.796 seconds (process running for 7.65)
这部分日志表明 Spring Boot 应用程序上下文和 Tomcat Web 服务器都已成功启动。
-
MCP 客户端连接成功:
2025-05-03T20:48:05.764+08:00 INFO 44960 --- [pool-5-thread-1] i.m.c.transport.StdioClientTransport : STDERR Message received: GitHub MCP Server running on stdio 2025-05-03T20:48:05.811+08:00 INFO 44960 --- [pool-2-thread-1] i.m.client.McpAsyncClient : Server response with Protocol: 2024-11-05, Capabilities: ..., Info: Implementation[name=github-mcp-server, version=0.6.2] and Instructions null
这部分日志确认 MCP 客户端成功连接到了通过 Stdio 运行的 “GitHub MCP Server”,并获取了其能力信息。
-
应用程序逻辑执行并成功获取 AI 回答:
Question查询用户16Miku的github仓库每个代码仓库的详细内容? Answer下面是用户 16Miku 的所有公开代码仓库及其详细信息: ... (详细的仓库列表和信息) ... 请注意,其中有一些仓库是私有的,因此无法提供它们的详细内容。其他仓库的信息已经列出,包括每个仓库的地址、描述、创建时间和最后更新时间。
这是 最关键的部分!
Question...
是您的CommandLineRunner
打印的输入问题。Answer...
后面跟着的是 AI 模型返回的 实际回答内容。- 回答内容详细列出了用户
16Miku
的 GitHub 公开仓库,包括名称、地址、描述、创建时间和更新时间,甚至提到了私有仓库无法提供详细信息。
这表明:
- 您的
ChatClient
调用成功了。 - AI 模型(根据之前的配置,应该是阿里云的 DashScope 模型)接收并理解了问题。
- AI 模型识别到这个问题需要使用工具(查询 GitHub 仓库)。
- AI 模型通过 Spring AI 和 MCP 框架,成功调用了 “GitHub MCP Server” 提供的工具。
- GitHub 工具成功执行,并返回了用户
16Miku
的仓库信息。 - AI 模型接收到工具的执行结果,并根据结果生成了结构化且相关的自然语言回答。
- 应用程序成功接收并打印了 AI 模型返回的回答。
-
应用程序正常关闭:
2025-05-03T20:49:08.549+08:00 INFO 44960 --- [ restartedMain] o.s.b.w.e.tomcat.GracefulShutdown : Commencing graceful shutdown. Waiting for active requests to complete 2025-05-03T20:49:08.557+08:00 INFO 44960 --- [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown : Graceful shutdown complete
这表明 Spring Boot 应用程序本身执行完
CommandLineRunner
中的逻辑后,进行了正常的关闭流程。 -
MCP 外部进程终止警告:
2025-05-03T20:49:08.679+08:00 WARN 44960 --- [onPool-worker-1] i.m.c.transport.StdioClientTransport : Process terminated with code 1
这个警告与之前出现的一样,表示通过 Stdio 连接的外部 MCP 进程在 Java 应用程序关闭时终止了,退出码为 1。这通常意味着外部进程没有以零退出码干净地退出,但它并没有阻止主应用程序完成其主要任务(调用 AI 并获取回答)。这可能只是 MCP 客户端库或外部进程本身在接收到关闭信号时的行为特性,通常不是一个严重的问题,只要核心功能正常即可。
结论
您的应用程序已经成功地:
- 启动了 Spring Boot 容器。
- 连接了 MCP 客户端到 GitHub 工具服务器。
- 使用 DashScope ChatModel 发送了需要工具调用的聊天请求。
- AI 模型成功调用了 GitHub 工具。
- 获取了 GitHub 仓库信息。
- 生成了基于工具结果的自然语言回答。
- 打印了回答。
- 正常关闭了应用程序。
至此已经成功地实现了 Spring AI、阿里云 DashScope 模型和 MCP 工具调用的集成。
7. 总结
本文档详细记录了使用 Spring AI 框架集成 GitHub 作为 MCP 工具的完整过程。通过配置 Maven 依赖、 application.yml 文件以及 mcp-servers-config.json ,我们成功地设置了 Spring AI 客户端,使其能够通过 Stdio 方式启动并与 GitHub MCP 服务器进行通信。
Java 应用程序代码演示了如何:
- 注入所需的 ChatModel (本例中为 DashScope) 和 ToolCallbackProvider (由 MCP 客户端提供)。
- 构建 ChatClient 并关联模型与工具。
- 向 AI 模型提出需要调用外部工具(GitHub API)的问题。
- Spring AI 框架自动处理工具调用流程:模型识别意图 -> 调用 MCP 客户端 -> MCP 客户端与 GitHub 服务器交互 -> 结果返回模型 -> 模型生成最终答案。
最终的控制台输出验证了整个流程的成功执行,AI 模型能够利用 GitHub MCP 工具获取所需信息并给出相应的回答。
此示例展示了 Spring AI MCP 的强大功能,它允许 AI 应用方便地扩展其能力,通过标准化的协议与各种外部工具和服务进行交互,从而处理更广泛、更复杂的任务。同时,也需要注意在生产环境中安全地管理 API 密钥和 Token。