spring-ai: Help me, my mcp tool doesn't work。I would be very grateful if anyone could help me。

Bug description Now it can be called successfully. But it will not call the tools in mcp。 What I can confirm is that there are tools in sse, and sse can be linked normally。 openAiClientManager is my custom built OpenAiChatModel and it works fine. I have tried many methods and asked cursor. I have also read the relevant documents of springai, but I still can’t find the reason for this problem. I have been troubled for 3 days. I would be very grateful if anyone could help me。

maven

  ai:
    mcp:
      client:
        enabled: true
        name: mcp-test-client
        version: 1.0.0
        request-timeout: 30s
        type: SYNC  # 同步客户端类型
        sse:
          connections:
            mcp-server: # MCP服务器连接名称
              url: http://0.0.0.0:3000
              sse-endpoint: /sse  # SSE端点
<spring-ai.version>1.1.0-SNAPSHOT </spring-ai.version>
<dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-mcp-client</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-model-openai</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>

    @Autowired
    List<McpSyncClient> mcpSyncClients;

    @Test
    public void predefinedQuestions() {
        HashSet<String> names = new HashSet<>();
        OpenAiChatModel client = openAiClientManager.getClient(OpenAiProvider.xxxx);
        for (McpSyncClient mcpClient : mcpSyncClients) {
            for (McpSchema.Tool tool : mcpClient.listTools().tools()) {
                String name = tool.name();
                String description = tool.description();
                //System.err.println("name:" + name);
                //System.err.println("description:" + description);
                names.add("mcp_test_client_mcp_server_" + name);
            }
        }


        var mcpToolProvider = new SyncMcpToolCallbackProvider(mcpSyncClients);
        ToolCallback[] toolCallbacks = mcpToolProvider.getToolCallbacks();
        ChatClient chatClient = ChatClient.builder(client).defaultToolCallbacks(mcpToolProvider).build();
        String userQuestion = """
                What tools are available?
                """;

        Prompt prompt = new Prompt(userQuestion, OpenAiChatOptions.builder()
                .model("gpt-4o") 
                .build());

        System.out.println("> USER: " + userQuestion);
        System.out.println("> ASSISTANT: " + chatClient.prompt(prompt).call().content());

    }
    private OpenAiChatModel buildModel(OpenAiProvider provider) {
        OpenAiApi openAiApi = OpenAiApi.builder().apiKey(cfg.getApiKey()).baseUrl(cfg.getBaseUrl()).build();
        OpenAiChatModel chatModel = OpenAiChatModel.builder().openAiApi(openAiApi).build();
        return chatModel;
    }

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 32 (3 by maintainers)

Most upvoted comments

一群中国人在这用英文交流看起来很怪异, chatClient.defaultToolCallBack(ToolCallBackProvider).build(); 然后直接chatclient.prompt(YourQuestion).call().content(); 就可以了, toolcallbackprovider直接从容器中获取 不要自己配置, 这样就可以了, 在使用指定的mcpclient的时候 ,直接在容器中获取 mcpclients 然后过滤出你想使用的指定mcpclient 就能使用指定mcpclient 了,如果不单独获取 它,chatclient会自动根据所有mcpserver中工具的描述自己选择使用哪个工具来回答问题了

Hi @qiuyueovo , it seems your issue hasn’t been resolved yet. I suggest you organize the code that can reproduce the problem into a minimal project and share it. This might make it easier for others to help you identify the issue.

The core code related to springai in the project has been pasted in the issues. There is almost no configuration. I also followed the official website documentation step by step. But it didn’t work, which made me crazy. If it still doesn’t work, I will streamline my project and share it

Hi @qiuyueovo , it seems your issue hasn’t been resolved yet. I suggest you organize the code that can reproduce the problem into a minimal project and share it. This might make it easier for others to help you identify the issue.