Skip to content

ToolContext没有被传递 #3510

@pangzaliang

Description

@pangzaliang

大模型: 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
Image
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions