I’m encountering the same problem when trying to pass data from an MCP client to an MCP server using Spring AI. I haven’t found a solution yet — any help or direction would be greatly appreciated!
MCP Client Code:
@Service
public class ChatbotService {
protected Logger logger = LoggerFactory.getLogger(getClass());
private ChatClient chatClient;
public ChatbotService(ChatClient chatClient) {
this.chatClient = chatClient;
logger.info("ChatbotService is ready");
}
String chat(String question, String apiKey ) {
return chatClient
.prompt()
.user(question)
.toolContext(Map.of("apiKey", apiKey))
.call()
.content();
}
}
MCP Server Code:
@Tool(name = "getUserPermissionsByUsername", description = "Get user permissions by username. This tool is used to retrieve the permissions of a user by their username. Must supply the username and an apiKey for authentication.")
private List<String> getUserPermissionsByUsername(@ToolParam(description = "User Name") String username, String apiKey, ToolContext toolContext) {
try {
//apiKey not passed at String or at toolContext
return userProxy.getUserPermissionsByUsername(username);
} catch (Exception e) {
return new ArrayList<>();
}
}