|
| 1 | += Getting Started with Model Context Protocol (MCP) |
| 2 | + |
| 3 | +The Model Context Protocol (MCP) standardizes how AI applications interact with external tools and resources. |
| 4 | + |
| 5 | +Spring joined the MCP ecosystem early as a key contributor, helping to develop and maintain the link:https://github.com/modelcontextprotocol/java-sdk[official MCP Java SDK] that serves as the foundation for Java-based MCP implementations. Building on this contribution, Spring AI provides comprehensive MCP support through Boot Starters and annotations, making it easy to build both MCP servers and clients. |
| 6 | + |
| 7 | +== Introduction Video |
| 8 | + |
| 9 | +**link:https://www.youtube.com/watch?v=FLpS7OfD5-s[Introduction to Model Context Protocol (MCP) - YouTube]** |
| 10 | + |
| 11 | +Start here for an introductory overview of the Model Context Protocol, explaining core concepts and architecture. |
| 12 | + |
| 13 | +== Complete Tutorial and Source Code |
| 14 | + |
| 15 | +**📖 Blog Tutorial:** link:https://spring.io/blog/2025/09/16/connect-your-ai-to-everything-spring-ais-mcp-boot-starters[Connect Your AI to Everything: Spring AI's MCP Boot Starters] |
| 16 | + |
| 17 | +**💻 Complete Source Code:** link:https://github.com/tzolov/spring-ai-mcp-blogpost[MCP Weather Example Repository] |
| 18 | + |
| 19 | +The tutorial provides comprehensive coverage of MCP development with Spring AI, including production-ready examples, advanced features, and deployment patterns. All code examples below are taken from this tutorial. |
| 20 | + |
| 21 | +== Quick Start |
| 22 | + |
| 23 | +The fastest way to get started is with Spring AI's annotation-based approach. The following examples are from the blog tutorial: |
| 24 | + |
| 25 | +=== Simple MCP Server |
| 26 | + |
| 27 | +[source,java] |
| 28 | +---- |
| 29 | +@Service |
| 30 | +public class WeatherService { |
| 31 | +
|
| 32 | + @McpTool(description = "Get current temperature for a location") |
| 33 | + public String getTemperature( |
| 34 | + @McpToolParam(description = "City name", required = true) String city) { |
| 35 | + return String.format("Current temperature in %s: 22°C", city); |
| 36 | + } |
| 37 | +} |
| 38 | +---- |
| 39 | + |
| 40 | +Add the dependency and configure: |
| 41 | + |
| 42 | +[source,xml] |
| 43 | +---- |
| 44 | +<dependency> |
| 45 | + <groupId>org.springframework.ai</groupId> |
| 46 | + <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId> |
| 47 | +</dependency> |
| 48 | +---- |
| 49 | + |
| 50 | +[source,properties] |
| 51 | +---- |
| 52 | +spring.ai.mcp.server.protocol=STREAMABLE |
| 53 | +---- |
| 54 | + |
| 55 | +=== Simple MCP Client |
| 56 | + |
| 57 | +[source,java] |
| 58 | +---- |
| 59 | +@Bean |
| 60 | +public CommandLineRunner demo(ChatClient chatClient, ToolCallbackProvider mcpTools) { |
| 61 | + return args -> { |
| 62 | + String response = chatClient |
| 63 | + .prompt("What's the weather like in Paris?") |
| 64 | + .toolCallbacks(mcpTools) |
| 65 | + .call() |
| 66 | + .content(); |
| 67 | + System.out.println(response); |
| 68 | + }; |
| 69 | +} |
| 70 | +---- |
| 71 | + |
| 72 | +Configure the client connection: |
| 73 | + |
| 74 | +[source,yaml] |
| 75 | +---- |
| 76 | +spring: |
| 77 | + ai: |
| 78 | + mcp: |
| 79 | + client: |
| 80 | + streamable-http: |
| 81 | + connections: |
| 82 | + weather-server: |
| 83 | + url: http://localhost:8080 |
| 84 | +---- |
| 85 | + |
| 86 | +== Learning Resources |
| 87 | + |
| 88 | +=== Implementation Video |
| 89 | + |
| 90 | +**link:https://www.youtube.com/watch?v=hmEVUtulHTI[Spring AI Model Context Protocol (MCP) Integration - YouTube]** |
| 91 | + |
| 92 | +A comprehensive video walkthrough of Spring AI's MCP integration, covering both server and client implementations. |
| 93 | + |
| 94 | +== Additional Examples Repository |
| 95 | + |
| 96 | +Beyond the tutorial examples, the link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol[Spring AI Examples] repository contains numerous MCP implementations. |
| 97 | + |
| 98 | +=== Recommended Starting Points |
| 99 | + |
| 100 | +*Annotation-based examples* (recommended for best developer experience): |
| 101 | + |
| 102 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-annotations-server[MCP Annotations Server] - Comprehensive annotation patterns |
| 103 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-annotations/mcp-annotations-server[Complete Annotations Example] - All annotation features |
| 104 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/sampling/annotations/mcp-sampling-server-annotations[Sampling with Annotations] - Advanced bidirectional AI |
| 105 | + |
| 106 | +=== By Use Case |
| 107 | + |
| 108 | +**Weather Services:** |
| 109 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webflux-server[WebFlux Weather Server] |
| 110 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/weather/starter-webmvc-oauth2-server[OAuth2 Secured Weather Server] |
| 111 | + |
| 112 | +**Data Integration:** |
| 113 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/sqlite/chatbot[SQLite AI Chatbot] |
| 114 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/filesystem[Filesystem Access Server] |
| 115 | + |
| 116 | +**Web Integration:** |
| 117 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/web-search/brave-chatbot[Brave Search Chatbot] |
| 118 | + |
| 119 | +**Client Examples:** |
| 120 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/client-starter/starter-default-client[Basic MCP Client] |
| 121 | +* link:https://github.com/spring-projects/spring-ai-examples/tree/main/model-context-protocol/mcp-annotations/mcp-annotations-client[Annotations Client] |
| 122 | + |
| 123 | +== Key Concepts |
| 124 | + |
| 125 | +**MCP Servers** expose capabilities (tools, resources, prompts) to AI applications. |
| 126 | +**MCP Clients** connect AI applications to MCP servers. |
| 127 | + |
| 128 | +**Transport Options:** |
| 129 | +* *STDIO* - Desktop applications, IDE integrations |
| 130 | +* *Streamable-HTTP* - Production deployments, microservices |
| 131 | +* *SSE* - Real-time web applications |
| 132 | +* *Stateless* - Serverless environments |
| 133 | + |
| 134 | +**Why Annotations?** |
| 135 | +* Declarative approach - focus on business logic |
| 136 | +* Automatic JSON schema generation |
| 137 | +* Type-safe parameter handling |
| 138 | +* Spring Boot auto-configuration |
| 139 | +* Minimal boilerplate code |
| 140 | + |
| 141 | +== Community Resources |
| 142 | + |
| 143 | +* link:https://github.com/spring-ai-community/awesome-spring-ai[Awesome Spring AI] - Community examples and resources |
| 144 | +* link:https://modelcontextprotocol.org/[Official MCP Specification] |
| 145 | +* link:https://github.com/modelcontextprotocol/java-sdk[Official MCP Java SDK] - Java SDK developed by the Spring team |
| 146 | +* link:https://modelcontextprotocol.io/sdk/java/mcp-overview[MCP Java SDK Documentation] |
| 147 | + |
| 148 | +== Reference Documentation |
| 149 | + |
| 150 | +* xref:api/mcp/mcp-overview.adoc[MCP Overview and Architecture] |
| 151 | +* xref:api/mcp/mcp-annotations-overview.adoc[MCP Annotations Guide] |
| 152 | +* xref:api/mcp/mcp-server-boot-starter-docs.adoc[Server Boot Starters] |
| 153 | +* xref:api/mcp/mcp-client-boot-starter-docs.adoc[Client Boot Starters] |
| 154 | + |
| 155 | +== Next Steps |
| 156 | + |
| 157 | +1. Read the complete blog tutorial linked above |
| 158 | +2. Clone and explore the tutorial source code repository |
| 159 | +3. Watch the video tutorial for a visual walkthrough |
| 160 | +4. Try the annotation-based examples |
| 161 | +5. Explore the additional examples repository |
| 162 | +6. Read the reference documentation for advanced features |
| 163 | +7. Join the community and share your implementations |
0 commit comments