iT邦幫忙

2025 iThome 鐵人賽

DAY 29
0
生成式 AI

nutc_imac_Agent拼裝車系列 第 29

Day29|Spring AI MCP 實戰篇:打造自己的 AI 工具伺服器(MCP Server)

  • 分享至 

  • xImage
  •  

到目前為止,我們已經學會了如何透過 RAG 讓 AI 理解與回答。
今天,我們將邁入更高階的整合主題 ——
讓 AI 不只是「會說」,還能「會做」!

這篇我們將使用 Spring AI 的 MCP Server
建立一個能執行實際任務的 AI 工具伺服器(例如:加法、乘法)。


一、什麼是 MCP(Model Context Protocol)?

MCP 是由 Anthropic 提出的新一代 AI 工具協定,
全名為 Model Context Protocol

它的核心理念是:

讓 LLM 能安全、結構化地呼叫外部工具(Tool)或資源。

在 Spring AI 中,我們可以輕鬆地使用 MCP 定義「可被模型呼叫」的 Java 方法,
並自動生成一個符合 MCP 標準的 API 伺服器。


二、專案設定

application.yml

server:
  port: 8080 

spring:
  application:
    name: mcp-server 
  ai:
    mcp:
      server:
        protocol: STREAMABLE
        name: AIE MCP Modify Quotations Server
        version: 1.0.0
        type: SYNC
        resource-change-notification: true
        tool-change-notification: true
        prompt-change-notification: true
        streamable-http:
          mcp-endpoint: /api/mcp
          keep-alive-interval: 30s

logging:
  level:
    org.springframework.web: DEBUG
    org.springframework.boot: DEBUG

✅ 啟動說明

以上設定會啟動一個 MCP 伺服器,
提供 /api/mcp 為工具通訊端點,
讓外部模型(例如 Claude、Spring AI ChatClient)能呼叫我們定義的工具。


三、註冊 MCP 工具

建立 MCP 設定類別

@Configuration
public class McpConfig {

    @Bean
    public List<ToolCallback> modifyQuotationTool(TestMcpServer mcpServices) {
        return List.of(ToolCallbacks.from(mcpServices));
    }
}

這裡將 TestMcpServer 中標記為 @Tool 的方法註冊為 MCP 可用工具。
Spring AI 會自動掃描並提供 API 給 LLM 呼叫。


四、定義 MCP 工具服務

@Slf4j
@Service
public class TestMcpServer {

    /**
     * 範例一:加法工具
     */
    @Tool(
            name = "add_numbers",
            description = "將兩個數字相加並回傳結果"
    )
    private Map<String, Object> addNumbers(
            @ToolParam(description = "num,例如:1") String n1,
            @ToolParam(description = "num,例如:1") String n2
    ) {
        return Map.of("result", Integer.parseInt(n1) + Integer.parseInt(n2));
    }

    /**
     * 範例二:乘法工具
     */
    @Tool(
            name = "multiply_numbers",
            description = "將兩個數字相乘並回傳結果"
    )
    private Map<String, Object> multiply(
            @ToolParam(description = "num,例如:1") String n1,
            @ToolParam(description = "num,例如:1") String n2
    ) {
        return Map.of("result", Integer.parseInt(n1) * Integer.parseInt(n2));
    }
}

每個 @Tool 標註的方法都會:

  • 自動被 MCP 掃描並註冊;
  • 能被模型或外部工具呼叫;
  • 回傳的 Map<String, Object> 會被序列化成 JSON。

五、啟動與測試

啟動 Spring Boot 專案後,
伺服器會監聽 /api/mcp 端點。

你可以用任何支援 MCP 協定的客戶端(例如 Anthropic Claude Desktop 或 Spring AI ChatClient)
來呼叫剛剛定義的工具。


📡 範例測試

呼叫「加法工具」

https://ithelp.ithome.com.tw/upload/images/20251007/20150369oXdMoFKeDB.png

呼叫「乘法工具」

https://ithelp.ithome.com.tw/upload/images/20251007/20150369Dvxt7qSmpL.png


六、小結

模組 功能
@Tool 定義 AI 可呼叫的方法
MCP Server 讓 LLM 與你的系統互通
Spring AI 自動註冊、抽象化 LLM 工具呼叫
Streamable HTTP 提供高效、持續的工具溝通協定

這篇我們打造了一個「會算數的 AI MCP 伺服器」。
下一篇,我們將延伸這個概念,讓 AI 不只算數,而能「整合企業內部服務」。



上一篇
Day28|RAG 實戰篇 (四):從文件到對話 — 檔案上傳與 ChatBot 問答
下一篇
Day30|最後一天我有話想說
系列文
nutc_imac_Agent拼裝車30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言