大模型: ollama qwen3:8b
MCP-Server: spring-ai-starter-mcp-server-webflux 1.1.0-SNAPSHOT
application.yml
server:
port: 8887
spring:
ai:
ollama:
base-url: http://192.168.1.5:11434
chat:
model: qwen3:8b
mcp:
client:
name: spring-ai-mcp-client
version: 1.0.0
request-timeout: 30s # MCP 客户端请求的超时持续时间
type: async # 异步的
sse:
connections:
server1:
url: http://127.0.0.1:8888
sse-endpoint: /sse
toolcallback:
enabled: true # 工具回调处于活动状态
code
@Service
public class WeatherService {
@Tool(name = "getWeather", description = "根据城市名称获取天气预报")
public String getWeather(@ToolParam(description = "城市名称") String city) {
Map<String, String> mockData = Map.of(
"西安", "晴天",
"北京", "小雨",
"上海", "大雨"
);
return mockData.getOrDefault(city, "抱歉:未查询到对应城市!");
}
}
/**
* 工具回调提供程序
* @param weatherService WeatherService
* @return ToolCallbackProvider
*/
@Bean
public ToolCallbackProvider weatherTools(@Autowired WeatherService weatherService) {
return MethodToolCallbackProvider.builder().toolObjects(weatherService).build();
}
MCP-Client: spring-ai-starter-mcp-client-webflux 1.1.0-SNAPSHOT spring-ai-starter-model-ollama 1.1.0-SNAPSHOT
application.yml
server:
port: 8887
spring:
ai:
ollama:
base-url: http://192.168.1.5:11434
chat:
model: qwen3:8b
mcp:
client:
name: spring-ai-mcp-client
version: 1.0.0
request-timeout: 30s # MCP 客户端请求的超时持续时间
type: async # 异步的
sse:
connections:
server1:
url: http://127.0.0.1:8888
sse-endpoint: /sse
toolcallback:
enabled: true # 工具回调处于活动状态
code
@RestController
public class DemoController {
private final ChatClient chatClient;
public DemoController(ChatClient.Builder builder,
ToolCallbackProvider callbackProvider) {
this.chatClient = builder
.defaultToolCallbacks(callbackProvider)
.build();
}
@GetMapping("/weather")
public Flux<String> getWeather() {
Flux<String> content = chatClient.prompt("今天西安的天气怎么样?")
.stream()
.content();
System.out.println("Response: " + content);
return content;
}
}
question: Using "Postman" to access "http://localhost:8887/weather" gives the following results
I don't know where to configure the ToolContext object
But I use spring-ai-starter-mcp-client 1.0.0 and spring-ai-starter-mcp-server-webmvc 1.0.0, (Servlet) does not have this problem
Don't laugh at my poor English, thank you.
Comment From: sunyuhan1998
Hi @pangzaliang , I actually just recently submitted a PR to fix an issue related to this: https://github.com/spring-projects/spring-ai/pull/3478 It was just merged 6 hours ago.I think it hasn't had a chance to be posted to the snapshot repository yet, so you can wait for that update to be posted, and if the problem still exists then, you can post the specific error message from the MCP client to here and I can help you pinpoint the problem further.
Comment From: pangzaliang
I saw your changes in the #3478 code, and I also checked that the package I introduced contained your latest code.
I found that this is my problem. I added the imported parameter ToolContext to the method decorated by annotation tool in the MCP server, and there was no problem.
@Tool(name = "getWeather", description = "根据城市名称获取天气预报")
public String getWeather(@ToolParam(description = "城市名称") String city, ToolContext toolContext) {
Map<String, String> mockData = Map.of(
"西安", "晴天",
"北京", "小雨",
"上海", "大雨"
);
return mockData.getOrDefault(city, "抱歉:未查询到对应城市!");
}
If I don't add it, Spring's log doesn't report an error, just the big model keeps looking for ToolContext. Anyway, thanks for your help