Understanding Spring AI MCP Client Startup Auto‑Configuration Flow
This article walks through the Spring AI MCP Client source code, showing how to set up a Java project, configure the MCP client, implement a demo controller, and dissect the auto‑configuration classes that assemble transport, client, and tool‑callback components during application startup.
The author first revisits the MCP‑AI workflow and explains why studying the source code improves technical depth, framework mastery, debugging speed, interview readiness, and open‑source contribution.
Project Setup
A new Spring Boot project is created with the following Maven dependencies (excerpt):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.2</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
</dependencies>The application.yml configures the MCP server endpoint, client name, version, request timeout, and transport type (HTTP SSE):
spring:
application:
name: mcp
ai:
mcp:
client:
enabled: true
name: my-mcp-client
version: 1.0.0
request-timeout: 30s
type: SYNC # or ASYNC for reactive applications
sse:
connections:
server1:
url: http://localhost:3000
openai:
api-key: xxxxxxxxxxx
base-url: https://xxxxxxx.comDemo Controller and Application
A simple @RestController uses ChatClient to call the MCP weather service:
@RestController
public class FirstMcpClientDemoController {
private ChatClient defaultChatClient;
private ToolCallbackProvider toolCallbackProvider;
public FirstMcpClientDemoController(ChatModel chatModel, ToolCallbackProvider tools) {
this.defaultChatClient = ChatClient.builder(chatModel)
.defaultSystem("""你能够通过调用工具获取天气""")
.defaultToolCallbacks(tools)
.defaultAdvisors(MessageChatMemoryAdvisor.builder(MessageWindowChatMemory.builder().build()).build())
.build();
this.toolCallbackProvider = tools;
}
@GetMapping("/test1")
public String test1() {
String generation = defaultChatClient.prompt("查询下北京的天气情况").call().content();
System.out.println("##generation
" + generation);
return generation;
}
}
@SpringBootApplication
public class DynamicClientMcpServerApplication {
public static void main(String[] args) {
SpringApplication.run(DynamicClientMcpServerApplication.class, args);
}
}Running the application and accessing http://localhost:8080/test1 returns a formatted weather report generated by the AI model.
Startup Auto‑Configuration Walk‑through
The Maven spring-ai-starter-mcp-client brings in several auto‑configuration classes. The author inspects them in the IDE and presents the key ones.
SseHttpClientTransportAutoConfiguration – activated when the classpath contains McpSchema and McpSyncClient but not the WebFlux transport class. It reads the SSE settings from application.yml, creates a NamedClientMcpTransport, and registers it as a Spring bean.
McpClientAutoConfiguration – receives the transport beans, instantiates a list of McpSyncClient objects (one per transport), and makes them available for later stages.
McpToolCallbackAutoConfiguration – wraps each McpSyncClient into a ToolCallbackProvider (synchronous or asynchronous) so that the AI model can discover and invoke MCP‑provided tools.
ToolCallingAutoConfiguration – registers the ToolCallingManager and configures static and Spring‑bean‑based tool resolvers.
OpenAIChatModel integration – the OpenAI model bean obtains the ToolCallingManager, converts the tool definitions into OpenAI Function Calling format, and includes them in the request payload.
Annotations such as @AutoConfiguration(after = …), @ConditionalOnClass, and @ConditionalOnMissingClass control when each class is loaded, ensuring correct ordering and avoiding conflicts.
Overall Flow
During application startup, Spring Boot processes the auto‑configuration classes in order, producing:
Transport layer objects that know how to talk to the MCP server.
Client objects that use those transports.
Tool‑callback providers that expose the server’s tools to the AI model.
A tool‑calling manager that the OpenAI model queries to generate function‑calling specifications.
This chain enables the AI model to call the weather‑service tool defined on the MCP server, as demonstrated by the demo controller.
The article concludes that understanding this assembly process helps developers customize MCP client behavior for their own business scenarios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ubiquitous Tech
A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
