Artificial Intelligence 9 min read

Dynamic Tool Management with Spring AI’s Model Context Protocol (MCP) – A Hands‑On Guide

Learn how to implement Spring AI’s Model Context Protocol (MCP) in Spring Boot 3.4.2, enabling dynamic runtime tool updates, configuring MCP server and client, building sample tools with code snippets, and demonstrating add/remove operations and testing via Cline and REST endpoints.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Dynamic Tool Management with Spring AI’s Model Context Protocol (MCP) – A Hands‑On Guide

1. Introduction

Model Context Protocol (MCP) is a powerful feature in Spring AI that standardizes how AI models access external tools and resources, and it supports dynamic runtime updates of available tools.

Understanding Model Context Protocol

MCP provides a standardized client‑server interface allowing AI applications and agents to access external tools, retrieve resources, and use prompt templates. The MCP server exposes tools and resources, while MCP clients connect to the server to use them. Spring AI offers a complete implementation of MCP, including both client and server components.

For more details see the referenced article "王炸!Spring AI+MCP 三步实现智能体开发".

2. Practical Example

2.1 Environment Preparation

Spring Boot version 3.4.2 is used.

2.2 Add Dependencies

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-starter-mcp-server&lt;/artifactId&gt;
  &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-starter-mcp-server-webmvc&lt;/artifactId&gt;
  &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;</code>

2.3 Application Configuration (application.yml)

<code>spring:
  ai:
    mcp:
      server:
        enabled: true
        name: ai_mcp_server
        version: 1.0.0
        type: SYNC</code>

2.4 Write Tools

Define a WeatherService tool:

<code>@Component
public class WeatherService {
  @Tool(description = "获取当前天气预报")
  WeatherResponse getCurrentWeather(WeatherRequest request) { /* ... */ }

  @Tool(description = "获取IP地址详细信息")
  String getIpAddressInfo(String ip) { /* ... */ }
}</code>

Define a MathService tool that will be managed dynamically:

<code>@Service
public class MathService {
  @Tool(name = "sum", description = "计算2个数的和")
  public int sum(int a, int b) { return a + b + 1000; }

  @Tool(name = "sub", description = "计算2个数的差值")
  public int sub(int a, int b) { return a - b - 1000; }
}</code>

2.5 Register WeatherService

<code>@Configuration
public class McpToolConfig {
  @Bean
  ToolCallbackProvider weatherTools(WeatherService weatherService) {
    return MethodToolCallbackProvider.builder()
        .toolObjects(weatherService)
        .build();
  }
}</code>

2.6 Dynamic Add/Remove Tools (Controller)

<code>@RequestMapping("/mcp")
public class McpToolController {
  private final McpSyncServer mcpSyncServer;
  private final MathService mathService;

  public McpToolController(McpSyncServer mcpSyncServer, MathService mathService) {
    this.mcpSyncServer = mcpSyncServer;
    this.mathService = mathService;
  }

  @GetMapping("/add")
  public ResponseEntity<?> addTool() {
    List<SyncToolSpecification> newTools = McpToolUtils
        .toSyncToolSpecifications(ToolCallbacks.from(this.mathService));
    for (SyncToolSpecification newTool : newTools) {
      this.mcpSyncServer.addTool(newTool);
    }
    return ResponseEntity.ok("添加成功");
  }

  @GetMapping("/remove")
  public ResponseEntity<?> removeTool(String toolName) {
    this.mcpSyncServer.removeTool(toolName);
    return ResponseEntity.ok("删除工具【" + toolName + "】成功");
  }
}</code>

The server can automatically notify clients of tool changes; this behavior can be toggled via:

<code>spring:
  ai:
    mcp:
      server:
        tool-change-notification: false | true</code>

2.7 Testing with Cline

Configure Cline to connect to the MCP server, verify that the WeatherService tools are discovered automatically, then call the /add endpoint to register the MathService tools. After adding, the client updates its tool list without restarting the server.

Cline test result
Cline test result

2.8 MCP Client Configuration

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-starter-mcp-client&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-M7&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-starter-mcp-client-webflux&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-M7&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;com.alibaba.cloud.ai&lt;/groupId&gt;
  &lt;artifactId&gt;spring-ai-alibaba-starter&lt;/artifactId&gt;
  &lt;version&gt;1.0.0-M6.1&lt;/version&gt;
&lt;/dependency&gt;</code>
<code>spring:
  ai:
    mcp:
      client:
        name: ai-mcp-client
        initialized: true
        type: ASYNC
        sse:
          connections:
            server1:
              url: http://localhost:8888</code>

2.9 Using Tools from a REST Controller

<code>@RestController
@RequestMapping("/tools")
public class ToolController {
  private final ChatClient chatClient;

  public ToolController(ChatClient.Builder aiClientBuilder, ToolCallbackProvider mcpTools) {
    this.chatClient = aiClientBuilder.defaultTools(mcpTools).build();
  }

  @GetMapping("/sum")
  public ResponseEntity<String> calcSum(String prompt) {
    String response = this.chatClient.prompt(prompt).call().content();
    return ResponseEntity.ok(response);
  }
}</code>

After adding the MathService tools, the client automatically discovers them and can invoke them through prompts, confirming successful dynamic tool management.

MCPBackend DevelopmentSpring BootSpring AIDynamic Tools
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.