Switching Spring AI from DashScope to Ollama: Multi‑MCP Server Calls and Targeted Server Example
This guide walks through replacing DashScope with a local Ollama model in a Spring AI project, showing how to configure multiple MCP servers, adjust Maven and YAML settings, run zero‑code changes, and troubleshoot common issues.
As large language model (LLM) usage expands, developers often want to move projects built on commercial cloud APIs such as Alibaba Cloud DashScope to locally hosted Ollama models for lower cost, better privacy, and offline development.
Spring AI model abstraction layer
Spring AI defines a unified ChatModel interface for different model providers. Switching models only requires changing the starter dependency and configuration; the business logic remains untouched.
Advantages of Ollama
Free and runs locally – no API key needed.
Provides an OpenAI‑compatible HTTP API for easy integration.
Supports a rich ecosystem of open‑source models (e.g., Qwen, Llama, Mistral) that can be pulled with a single command.
Implementation steps
1. Modify pom.xml dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.5</version>
</parent>
<groupId>com.badao</groupId>
<artifactId>spring-ai-ollama-mcp-third-multipart</artifactId>
<version>1.0.0</version>
<properties>
<java.version>17</java.version>
<maven.compiler.release>17</maven.compiler.release>
<spring-ai.version>1.1.2</spring-ai.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
</dependency>
<!-- Spring AI MCP client -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-client</artifactId>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
<repository>
<id>aliyun</id>
<name>Aliyun Maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
</repositories>2. Adjust application.yml configuration
server:
port: 8082
spring:
ai:
ollama:
base-url: http://localhost:11434 # Ollama service address
chat:
model: qwen2.5:7b-instruct # model that supports tool calling
options:
temperature: 0.7
mcp:
client:
enabled: true
request-timeout: 60s
toolcallback:
enabled: true
streamable-http:
connections:
hacker-news:
url: https://hn.caseyjhand.com
nws-weather:
url: https://nws.caseyjhand.com/mcp
logging:
level:
org.springframework.ai.mcp: DEBUG3. Zero code changes
All Java classes (e.g., AiConfig, AiService, AiController) remain unchanged. Spring AI injects the active ChatModel via ChatClient.Builder, fully decoupling business logic from the underlying model implementation.
Test results
Two queries were sent:
{
"message": "Answer two questions: what is the hottest Hacker News article and what is the weather in New York?"
}The response is shown in the image below.
A single‑question query was also tested (image omitted for brevity).
Common issues and troubleshooting
DashScope API key must be set : Residual DashScope starter triggers auto‑configuration. Remove the starter and run mvn clean before restarting.
Missing dependency version : Some Spring AI starters are not fully managed by the BOM. Explicitly set ${spring-ai.version} for those artifacts.
Cannot find spring-ai-ollama-spring-boot-starter:1.1.2 : The correct artifactId in version 1.1.2 is spring-ai-starter-model-ollama. Update the dependency accordingly.
Connection reset after startup : HTTP/2 or SSE streams may be reset by network devices. Increase request-timeout to 60s and optionally set per‑connection timeouts. The framework will auto‑reconnect; the warning can be ignored or logged at a higher level.
Model does not support tool calling : Some Ollama models (e.g., early Llama 2) lack Function Calling. Switch to a model that explicitly supports it, such as qwen2.5:7b, llama3.1:8b, or mistral:7b.
Key takeaways
Dependency isolation: keep only one model starter in a Spring AI project to avoid conflicting auto‑configurations.
Configuration‑driven model switching: change the model by editing application.yml without touching business code.
Use the Spring AI BOM for version management; manually specify versions for any unmanaged artifacts.
Network resilience: the MCP client automatically retries; occasional connection resets are harmless.
Model capability matters: ensure the selected Ollama model supports Function Calling before relying on tool usage.
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.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
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.
