モデルコンテキストプロトコル入門 (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.
完全なチュートリアルとソースコード
📖 Blog Tutorial : Connect Your AI to Everything (英語)
💻 Complete Source Code : MCP Weather Example Repository [GitHub] (英語)
このチュートリアルでは、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 の例 [GitHub] (英語) リポジトリには多数の MCP 実装が含まれています。
推奨される出発点
アノテーションベースの例
Complete Annotations Example [GitHub] (英語) - すべてのアノテーション機能 (Client & Server)
Sampling with Annotations [GitHub] (英語) - 高度な双方向 AI (Client & Server)
MCP Weather Tutorial [GitHub] (英語) - Full tutorial source code (Client & Server)
Community Resources
素晴らしい Spring AI [GitHub] (英語) - Community examples and resources
Official MCP Java SDK [GitHub] (英語) - Java SDK developed by the Spring team