通过 Gitee MCP 在 Java 生态中的集成应用,结合两种传输模式(stdio/SSE)实现 AI 助手对代码仓库的智能管理,上手 Java MCP 开发
chmod +x mcp-gitee
<dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-mcp</artifactId> <version>1.0.0-beta2</version> </dependency> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-open-ai-spring-boot-starter</artifactId> <version>1.0.0-beta2</version> </dependency>
application.yml
langchain4j: open-ai: chat-model: api-key: sk- base-url: https://api.deepseek.com/v1 model-name: deepseek-chat log-requests: true
@Autowired private ChatLanguageModel chatLanguageModel; @SneakyThrows @Test void contextLoads() { // 创建 stdio 传输模式 McpTransport transport = new StdioMcpTransport.Builder() .command(List.of("/Users/lengleng/Downloads/mcp-gitee-darwin-arm64/mcp-gitee", "-token", "GITEE-TOKEN")) .logEvents(true) // 可选:在日志中显示通信内容 .build(); // 创建 MCP 客户端 @Cleanup McpClient mcpClient = new DefaultMcpClient.Builder() .transport(transport) .build(); // 创建工具提供者 ToolProvider toolProvider = McpToolProvider.builder() .mcpClients(List.of(mcpClient)) .build(); // 构建 Gitee AI 服务 GiteeAiService giteeAiService = AiServices.builder(GiteeAiService.class) .chatLanguageModel(chatLanguageModel) .toolProvider(toolProvider) .build(); // 使用 AI 服务查询 Gitee 信息 String result = giteeAiService.chat("获取 log4j/pig 开启的 issue 列表"); log.info("gitee mcp result: {}", result); }
mcp-gitee -transport sse -token GITEE-TOKEN
@Autowired private ChatLanguageModel chatLanguageModel; @SneakyThrows @Test void contextLoads() { // 创建 SSE 传输模式 McpTransport sseTransport = new HttpMcpTransport.Builder() .sseUrl("http://localhost:8000/sse") .logRequests(true) // 可选:记录请求日志 .logResponses(true) // 可选:记录响应日志 .build(); // 创建 MCP 客户端 @Cleanup McpClient mcpClient = new DefaultMcpClient.Builder() .transport(sseTransport) .build(); // 创建工具提供者 ToolProvider toolProvider = McpToolProvider.builder() .mcpClients(List.of(mcpClient)) .build(); // 构建 Gitee AI 服务 GiteeAiService giteeAiService = AiServices.builder(GiteeAiService.class) .chatLanguageModel(chatLanguageModel) .toolProvider(toolProvider) .build(); // 使用 AI 服务查询 Gitee 信息 String result = giteeAiService.chat("获取 log4j/pig 开启的 issue 列表"); log.info("gitee mcp result: {}", result); }
2025-03-16T23:19:51.211+08:00 INFO 67659 --- [ main] com.example.demo.DemoApplicationTests : gitee mcp result: 目前 log4j/pig 仓库中有以下开启的 issue: 1. **JDK17版本中oauth2.0的授权码模式,无法通过code获取到access_token** - 编号: IBQJ94 - 创建时间: 2025-03-04T13:04:53+08:00 - 链接: [https://gitee.com/log4j/pig/issues/IBQJ94](https://gitee.com/log4j/pig/issues/IBQJ94)