Spring AI MCP Client与MCP Server交互异常排查求助
问题背景
我正在实现一个简单的MCP Server与Client,Server仅提供创建文件的单一工具。Server端通过Postman测试MCP请求正常,但Client端已确认和Server建立连接,调用Azure OpenAI大模型时却无法触发Server端的工具执行。日志显示Client已接收Server返回的工具元数据,求排查问题原因。
Server端实现
工具服务代码
@Slf4j @Service public class FileSystemService { @Tool(name = "Create File", description = "Create a file with the provided fileName on the file system") public String createFile(String fileName) { log.info("Request to create a file: {}", fileName); Path path = Paths.get(fileName); try { Files.createFile(path); log.info("File created: {}", path.toAbsolutePath()); } catch (IOException e) { log.error("Error creating file: {}", e.getMessage()); } return "File [" + fileName + "] created successfully"; } }
Server工具暴露配置
@Configuration public class ToolConfiguration { @Bean public ToolCallbackProvider tools(FileSystemService fileSystemService) { return MethodToolCallbackProvider.builder() .toolObjects(fileSystemService) .build(); } }
Server配置文件(application.yaml)
# Spring configurations spring: application: name: mcp-filesystem-server # Spring AI configurations ai: mcp: server: enabled: true stdio: false name: mcp-filesystem-server version: 0.0.1 resource-change-notification: true tool-change-notification: true prompt-change-notification: true sse-endpoint: /api/v1/sse sse-message-endpoint: /api/v1/mcp type: async
Client端实现
Client配置文件(application.yaml)
spring: application: name: mcp-client-x # Spring AI configurations ai: # Azure OpenAI configurations azure: openai: chat: options: deployment-name: gpt-4o-mini endpoint: https://ai-foundry-deployment.openai.azure.com/ api-key: bsbfldsablf embeddings: options: deployment-name: text-embedding-ada-002 # MCP Client to Server Connection Configuration mcp: client: enabled: true name: mcp-client-x version: 0.0.1 initialized: true request-timeout: 20s root-change-notification: true type: async toolcallback.enabled: true sse: connections: server1: url: http://localhost:8018 sse-endpoint: /api/v1/sse
ChatClient配置代码
@Configuration public class ModelConfiguration { @Bean public ChatClient chatClient(AzureOpenAiChatModel azureOpenAiChatModel, List<ToolCallback> mcpAsyncToolCallbacks) { return ChatClient.builder(azureOpenAiChatModel) .defaultToolCallbacks(mcpAsyncToolCallbacks) .build(); } }
LLM调用代码
@Slf4j @Service @RequiredArgsConstructor public class ChatServiceAzureOpenAi implements ChatService { private final ChatClient chatClient; @Override public Flux<String> process(UserPrompt userPrompt) { log.info("User prompt: {}", userPrompt); var SYSTEM_PROMPT = """ You are a helpful assistant. Your task is to perform the task based on the user's prompt while making use of the available tools. Message: {message} """; PromptTemplate promptTemplate = PromptTemplate.builder() .template(SYSTEM_PROMPT) .build(); Prompt prompt = promptTemplate.create(Map.of("message", userPrompt.getMessage())); return chatClient .prompt(prompt) .stream() .content(); } }
关键日志信息
2025-08-05T09:54:26.443+05:30 DEBUG 92639 --- [mcp-client-x] [ctor-http-nio-2] io.modelcontextprotocol.spec.McpSchema : Received JSON message: {"jsonrpc":"2.0","id":"8e7ffaaf-1", "result":{"tools": [{"name":"Create File", "description":"Create a file with the provided fileName on the file system", "inputSchema": {"type":"object","properties":{"arg0":{"type":"string"}},"required":["arg0"],"additionalProperties":false}}]}}
排查方向
- 工具参数命名不匹配:日志显示工具输入参数名为
arg0,但工具方法参数是fileName,LLM无法正确关联参数。建议给工具方法参数添加注解指定明确名称:public String createFile(@Tool.Argument(name = "fileName") String fileName) - 系统提示词不够明确:当前提示词未明确告知LLM调用工具的规则,优化提示词明确要求调用
Create File工具并传入fileName参数:var SYSTEM_PROMPT = """ You are a helpful assistant. When the user asks to create a file, you must use the `Create File` tool with the `fileName` parameter set to the requested file name. Perform the task based on the user's prompt using available tools. Message: {message} """; - 验证工具回调注入:在
ModelConfiguration中添加日志,确认mcpAsyncToolCallbacks是否正确加载:@Bean public ChatClient chatClient(AzureOpenAiChatModel azureOpenAiChatModel, List<ToolCallback> mcpAsyncToolCallbacks) { log.info("Loaded {} MCP tool callbacks", mcpAsyncToolCallbacks.size()); return ChatClient.builder(azureOpenAiChatModel) .defaultToolCallbacks(mcpAsyncToolCallbacks) .build(); } - 检查Azure OpenAI部署配置:确认gpt-4o-mini部署已启用函数调用(Function Calling)功能。
- 开启MCP客户端DEBUG日志:查看工具调用请求是否正常发送到Server端,排查异步通信是否存在问题。
内容的提问来源于stack exchange,提问作者Gagan
相关产品推荐
相关产品推荐

