Getting Started with Model Context Protocol (MCP)

The Model Context Protocol (MCP) standardizes how AI applications interact with external tools and resources.spring-doc.cn

Spring joined the MCP ecosystem early as a key contributor, helping to develop and maintain the official MCP Java SDK 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-doc.cn

Introduction Video

Start here for an introductory overview of the Model Context Protocol, explaining core concepts and architecture.spring-doc.cn

Complete Tutorial and Source Code

💻 Complete Source Code: MCP Weather Example Repositoryspring-doc.cn

The tutorial covers the essentials of MCP development with Spring AI, including advanced features, and deployment patterns. All code examples below are taken from this tutorial.spring-doc.cn

Quick Start

The fastest way to get started is with Spring AI’s annotation-based approach. The following examples are from the blog tutorial:spring-doc.cn

Simple MCP Server

@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);
    }
}

Add the dependency and configure:spring-doc.cn

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

Simple MCP Client

@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);
    };
}

Add the dependency and configure:spring-doc.cn

<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

Learning Resources

Implementation Video

A video walkthrough of Spring AI’s MCP integration, covering both server and client implementations.spring-doc.cn

Additional Examples Repository

Beyond the tutorial examples, the Spring AI Examples repository contains numerous MCP implementations.spring-doc.cn

Annotation-based examplesspring-doc.cn

Community Resources