-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Help me, my mcp tool doesn't work。I would be very grateful if anyone could help me。 #3291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I tried to reproduce your scenario using the Qwen model combined with the Amap MCP server, but couldn't replicate the issue. In my tests, the model successfully called the tools in MCP. The only difference from your code is that I used like this:
|
But I don't know how to integrate the above code into my code. Can you modify it based on the code I provided? There is no configuration about mcpClientTransport in my code. The reference document of the code is: https://docs.spring.io/spring-ai/reference/1.1-SNAPSHOT/api/mcp/mcp-client-boot-starter-docs.html. I would be grateful if you could help me |
After I commented out the content in your screenshot, I added the above configuration file to the code, but the mcp tool was still not called correctly. This problem has troubled me for 3 days, and I still have no idea. |
Do these two lines of comments print any content? |
They can print out the complete tool in the mcp, including name and description. And the above results are consistent with the tools I viewed in Cherry Studio. They are all the tools in my mcp But unfortunately, my mcp tool is not printed out in ASSISTANT. |
try it: |
Unfortunately, it still doesn't work. System.out.println("> ASSISTANT: " + chatClient.prompt(prompt).call().content());
System.out.println("> ASSISTANT: " + chatClient.prompt().user(userQuestion).call().content()); There is no difference between these two calling methods. The result is the same. There is no output of mcp tool |
I almost replicated your code. The line missing is: |
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. |
OpenAiClientManager
private final Map<OpenAiProvider, OpenAiChatModel> clientMap = new EnumMap<>(OpenAiProvider.class);
@Resource(name = "aClient")
private OpenAiChatModel aClient;
@Resource(name = "bClient")
private OpenAiChatModel bClient;
@PostConstruct
public void init() {
clientMap.put(OpenAiProvider.a, aClient);
clientMap.put(OpenAiProvider.b, bClient);
}
public OpenAiChatModel getClient(OpenAiProvider provider) {
return clientMap.get(provider);
} @Configuration
public class OpenAiClientConfig {
@Autowired
private AiProperties aiProperties;
@Bean("aClient")
public OpenAiChatModel aClient() {
return buildModel(OpenAiProvider.a);
}
@Bean("bClient")
public OpenAiChatModel bClient() {
return buildModel(OpenAiProvider.b);
}
private OpenAiChatModel buildModel(OpenAiProvider provider) {
AiProperties.AiProviderConfig cfg = aiProperties.getProviders().get(key);
if (cfg == null) throw new IllegalArgumentException("没有配置:" + provider);
//return new OpenAiChatModel(new OpenAiApi(cfg.getApiKey(), cfg.getBaseUrl()));
OpenAiApi openAiApi = OpenAiApi.builder().apiKey(cfg.getApiKey()).baseUrl(cfg.getBaseUrl()).build();
OpenAiChatModel chatModel = OpenAiChatModel.builder().openAiApi(openAiApi).build();
return chatModel;
}
} The role of OpenAiClientManager is that I can freely choose and switch when I have multiple service providers. |
I only set the base url and api key. Nothing else changed. |
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 |
There is a fatal bug in your code: two @bean methods have the same name, both are "aClient". |
Sorry. I made a typo when mosaicking part of my code. The actual code is correct |
I think you should minimize your code. This is more helpful for analysis. |
Thank you. My question may be complicated. I sent you an email |
一群中国人在这用英文交流看起来很怪异, chatClient.defaultToolCallBack(ToolCallBackProvider).build(); 然后直接chatclient.prompt(YourQuestion).call().content(); 就可以了, toolcallbackprovider直接从容器中获取 不要自己配置, 这样就可以了, 在使用指定的mcpclient的时候 ,直接在容器中获取 mcpclients 然后过滤出你想使用的指定mcpclient 就能使用指定mcpclient 了,如果不单独获取 它,chatclient会自动根据所有mcpserver中工具的描述自己选择使用哪个工具来回答问题了 |
// 2. 在应用程序中使用 MCP 服务
@Resource
private ToolCallbackProvider toolCallbackProvider;
ChatClient chatClient = ChatClient.builder(client).defaultToolCallbacks(toolCallbackProvider).build();
String userQuestion = """
What tools are available?
""";
System.out.println("> USER: " + userQuestion);
System.out.println("> ASSISTANT: " + chatClient.prompt(userQuestion).call().content()); 这样还是不行。 Although it is very strange, after all, most of the issues in springai are in English, so I also submitted it in English. There are translation software that can understand it. // 2. Use MCP service in the application
@Resource
private ToolCallbackProvider toolCallbackProvider;
ChatClient chatClient = ChatClient.builder(client).defaultToolCallbacks(toolCallbackProvider).build();
String userQuestion = """
What tools are available?
""";
System.out.println("> USER: " + userQuestion);
System.out.println("> ASSISTANT: " + chatClient.prompt(userQuestion).call().content()); This still doesn't work. |
而当这样使用的时候又会提示:
|
是呀,我在没有调用 mcp 之前。其他功能都是正常使用的。 |
兄弟,你的问题是mcp-client 配置有问题。可以使用如下配置。添加 工具调用toolcallback为开启状态。 |
当这里我用了自己自定义的 时就不行。 ChatClient chatClient = ChatClient.builder(client).defaultToolCallbacks(mcpToolProvider).build(); 哪怕是我启动时候手动用框架手动注入都不行 @Bean
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder, ToolCallbackProvider tools,
ConfigurableApplicationContext context) {
String userQuestion = """
What tools are available?
""";
return args -> {
var chatClient = chatClientBuilder
.defaultToolCallbacks(tools)
.build();
System.out.println("\n>>> QUESTION: " + userQuestion);
System.out.println("\n>>> ASSISTANT: " + chatClient.prompt(userQuestion).call().content());
context.close();
};
} |
这种也还是不行 就很奇怪 @Resource
private SyncMcpToolCallbackProvider toolCallbackProvider;
OpenAiChatModel client = openAiClientManager.getClient(OpenAiProvider.a);
ChatClient chatClient = ChatClient.builder(client).defaultToolCallbacks(toolCallbackProvider.getToolCallbacks()).build();
System.out.println("> ASSISTANT: " + chatClient.prompt(prompt).call().content()); |
感谢各位。 Thanks everyone. |
Interesting, I tested GPT-4.1 and GPT-4o, and in my tests, both models were able to call the MCP tool normally — that's really strange. May I ask whether the GPT-series model interface you're using is officially provided by OpenAI, or are you accessing it through a third-party agent? |
The above models are all accessed through third-party agents, but Cherry Studio can call them normally.That means that a third-party agent is normal. |
Uh oh!
There was an error while loading. Please reload this page.
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
The text was updated successfully, but these errors were encountered: