iT邦幫忙

2025 iThome 鐵人賽

DAY 20
0
Software Development

事件驅動電力交易平台:Spring Boot 實戰系列 第 20

Day 20 | 我的MCP服務 基礎設定與相關依賴使用

  • 分享至 

  • xImage
  •  

本文說明如何為 eap-mcp 模組準備 Gradle 依賴與常見設定,讓 MCP 工具可以順利編譯、注入以及與其他服務(如 wallet/order)整合。

目標

  • 確保 Spring Boot、MCP SDK、Feign、Jackson 等套件可用

重要相依

eap-mcp/build.gradle包含下列重點:

  • Spring Boot 各類依賴(starter-web, starter-actuator, starter-validation)
  • Spring Cloud OpenFeign(使用 Feign 呼叫外部 wallet/order 服務)
  • MCP / Model Context Protocol 相關套件

本專案實際的 build.gradle

下面是 eap-mcp/build.gradle 的完整內容,我會在之後針對關鍵相依做逐段說明:

plugins {
        id 'java'
        id 'org.springframework.boot' version '3.5.3'
        id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.eap'
version = '0.0.1-SNAPSHOT'

java {
        sourceCompatibility = '17'
}

configurations {
        compileOnly {
                extendsFrom annotationProcessor
        }
}

repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
        // EAP Common DTOs
        implementation project(':eap-common')
    
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.boot:spring-boot-starter-validation'
        implementation 'org.springframework.boot:spring-boot-starter-actuator'
    
        // 使用spring-ai 提供的 MCP Server WebMVC Starter
        implementation 'org.springframework.ai:spring-ai-starter-mcp-server-webmvc:1.1.0-M1'
    
        // HTTP Client for calling order-service
        implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    
    
        // Logging and metrics
        implementation 'io.micrometer:micrometer-registry-prometheus'
    
        // Lombok
        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'
    
        // Testing
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

為什麼需要這些 Spring 套件

  • org.springframework.boot:spring-boot-starter-web

    • 提供 Spring MVC、內嵌Tomcat伺服器,以及路由/Controller 等基礎。MCP server 與工具管理端通常會以 HTTP endpoint 暴露,因此需要 web starter。
  • org.springframework.boot:spring-boot-starter-validation

    • 提供 javax.validation/Jakarta Bean Validation(例如 @Valid@NotNull),方便在工具的方法參數上做入參驗證,避免錯誤的輸入造成系統異常。
  • org.springframework.boot:spring-boot-starter-actuator

    • 提供健康檢查 (/actuator/health)、metrics、和運維端點,好在部署與監控上整合 Prometheus / Grafana。
  • org.springframework.ai:spring-ai-starter-mcp-server-webmvc:1.1.0-M1

    • 這是專門為 MCP pattern 用的 Spring AI starter(示例名稱),把 MCP tool 掃描、Tool 註冊與 WebMVC 介面整合起來,讓工具可以透過 MCP 協定被呼叫。
  • org.springframework.cloud:spring-cloud-starter-openfeign

    • 提供 Feign Client 支援,方便使用 interface 定義遠端 HTTP 呼叫(此專案中用來呼叫 WalletServiceClientOrderServiceClient)。
  • io.micrometer:micrometer-registry-prometheus

    • Micrometer 提供應用層 metrics;Prometheus registry 能把 metrics 握手到 Prometheus,配合 Actuator 使用。(預備使用,為了提供更高可觀測性而先準備)
  • lombok(compileOnly + annotationProcessor)

    • 減少樣板程式碼(getter/setter/constructor),但要在 IDE 中啟用 lombok 插件以獲得良好開發體驗。


上一篇
Day 19 | 在 order-service 增加 McpApiController:把「給機器用」與「給人用」的入口拆開
下一篇
Day 21 | 開始實作我的MCP Server ,使用Spring 內建功能做好基礎設定
系列文
事件驅動電力交易平台:Spring Boot 實戰22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言