モデルコンテキストプロトコル入門 (MCP)

モデルコンテキストプロトコル (MCP) は、AI アプリケーションが外部ツールやリソースと対話する方法を標準化します。

Spring joined the MCP ecosystem early as a key contributor, helping to develop and maintain the official MCP Java SDK [GitHub] (英語) that serves as the foundation for Java-based MCP implementations. Building on this contribution, Spring AI provides MCP support through Boot Starters and annotations, making it easy to build both MCP servers and clients.

導入ビデオ

モデルコンテキストプロトコルの概要とコア概念およびアーキテクチャの説明については、ここから始めてください。

完全なチュートリアルとソースコード

このチュートリアルでは、Spring AI を使用した MCP 開発の基本、高度な機能、デプロイパターンなどを網羅しています。以下のコード例はすべてこのチュートリアルから抜粋したものです。

クイックスタート

最も早く始める方法は、Spring AI のアノテーションベースのアプローチを利用することです。以下の例はブログチュートリアルから抜粋したものです。

シンプルな MCP サーバー

@Service
public class WeatherService {

    @McpTool(description = "Get current temperature for a location")
    public String getTemperature(
            @McpToolParam(description = "City name", required = true) String city) {
        return String.format("Current temperature in %s: 22°C", city);
    }
}

依存関係を追加して構成します。

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>
</dependency>
spring.ai.mcp.server.protocol=STREAMABLE

シンプルな MCP クライアント

@Bean
public CommandLineRunner demo(ChatClient chatClient, ToolCallbackProvider mcpTools) {
    return args -> {
        String response = chatClient
            .prompt("What's the weather like in Paris?")
            .toolCallbacks(mcpTools)
            .call()
            .content();
        System.out.println(response);
    };
}

依存関係を追加して構成します。

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
spring:
  ai:
    mcp:
      client:
        streamable-http:
          connections:
            weather-server:
              url: http://localhost:8080

学習リソース

実装ビデオ

Spring AI の MCP 統合のビデオウォークスルー。サーバーとクライアントの両方の実装をカバーしています。

追加の例リポジトリ

チュートリアルの例以外にも、Spring AI の例 [GitHub] (英語) リポジトリには多数の MCP 実装が含まれています。

アノテーションベースの例