|
| 1 | +package com.microsoft.azure.toolkit.intellij.azuremcp; |
| 2 | + |
| 3 | +import com.intellij.openapi.application.PathManager; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | +import org.apache.commons.codec.digest.DigestUtils; |
| 6 | +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; |
| 7 | +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; |
| 8 | +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; |
| 9 | +import org.apache.commons.lang3.StringUtils; |
| 10 | +import org.apache.commons.lang3.SystemUtils; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | + |
| 13 | +import javax.annotation.Nullable; |
| 14 | +import java.io.File; |
| 15 | +import java.io.FileInputStream; |
| 16 | +import java.io.FileOutputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.nio.file.Files; |
| 19 | +import java.nio.file.Path; |
| 20 | +import java.nio.file.StandardOpenOption; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +@Slf4j |
| 24 | +public class AzureMcpPackageManager { |
| 25 | + private final GithubClient gitHubClient; |
| 26 | + private final String platform; |
| 27 | + |
| 28 | + public AzureMcpPackageManager() { |
| 29 | + this.gitHubClient = new GithubClient(); |
| 30 | + this.platform = getPlatformIdentifier(); |
| 31 | + } |
| 32 | + |
| 33 | + @Nullable |
| 34 | + public synchronized File getAzureMcpExecutable() { |
| 35 | + try { |
| 36 | + final GithubRelease latestRelease = gitHubClient.getLatestAzureMcpRelease(); |
| 37 | + if (latestRelease != null && latestRelease.getAssets() != null) { |
| 38 | + final String tagName = latestRelease.getTagName(); |
| 39 | + log.info("Latest version of Azure MCP: " + tagName); |
| 40 | + |
| 41 | + final Optional<GithubAsset> githubAsset = latestRelease.getAssets() |
| 42 | + .stream() |
| 43 | + .filter(asset -> asset.getName().contains(platform)) |
| 44 | + .findFirst(); |
| 45 | + |
| 46 | + if (githubAsset.isPresent()) { |
| 47 | + final GithubAsset asset = githubAsset.get(); |
| 48 | + log.info("Azure MCP package for current platform: " + asset.getName()); |
| 49 | + final long startTime = System.currentTimeMillis(); |
| 50 | + final String azMcpDir = PathManager.getPluginsPath() + "/azure-toolkit-for-intellij/azmcp"; |
| 51 | + final File azMcpDirFile = new File(azMcpDir); |
| 52 | + if (azMcpDirFile.exists() || azMcpDirFile.mkdirs()) { |
| 53 | + final Path versionFile = Path.of(azMcpDir + "/version.txt"); |
| 54 | + |
| 55 | + final File extractedDir = new File(azMcpDirFile, "/azmcp_package_" + tagName); |
| 56 | + extractedDir.mkdirs(); |
| 57 | + final String executablePath = extractedDir.getAbsolutePath() + getExecutableRelativePath(); |
| 58 | + final File azMcpExe = new File(executablePath); |
| 59 | + if (!azMcpExe.exists()) { |
| 60 | + Files.writeString(versionFile, tagName, StandardOpenOption.CREATE, StandardOpenOption.WRITE); |
| 61 | + final File azMcpTgz = new File(azMcpDir, "azmcp_" + tagName + ".tgz"); |
| 62 | + log.info("Downloading Azure MCP Server to: " + azMcpTgz.getAbsolutePath()); |
| 63 | + final boolean downloaded = gitHubClient.downloadToFile(asset.getBrowserDownloadUrl(), azMcpTgz); |
| 64 | + if (downloaded && digestMatches(azMcpTgz, asset.getDigest())) { |
| 65 | + log.info("Downloaded Azure MCP Server successfully in " + (System.currentTimeMillis() - startTime) + " ms"); |
| 66 | + log.info("Extracting Azure MCP Server to: " + extractedDir.getAbsolutePath()); |
| 67 | + extractTarGz(azMcpTgz, extractedDir); |
| 68 | + log.info("Azure MCP Server extracted successfully to: " + extractedDir.getAbsolutePath()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (azMcpExe.exists() && (azMcpExe.canExecute() || azMcpExe.setExecutable(true))) { |
| 73 | + log.info("Azure MCP Server executable found at: " + azMcpExe.getAbsolutePath()); |
| 74 | + return azMcpExe; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (final IOException e) { |
| 80 | + System.err.println("Error reading Azure MCP Server version: " + e.getMessage()); |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private boolean digestMatches(File azMcpTgz, String expectedDigest) { |
| 86 | + try { |
| 87 | + // GitHub releases API computes the SHA-256 digest of the file contents. |
| 88 | + // https://github.blog/changelog/2025-06-03-releases-now-expose-digests-for-release-assets/ |
| 89 | + final String downloadFileDigest = DigestUtils.sha256Hex(new FileInputStream(azMcpTgz)); |
| 90 | + return StringUtils.equalsIgnoreCase("sha256:" + downloadFileDigest, expectedDigest); |
| 91 | + } catch (final Exception e) { |
| 92 | + log.error("Failed to calculate file digest", e); |
| 93 | + return false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public synchronized void cleanup() { |
| 98 | + try { |
| 99 | + final String azMcpDir = PathManager.getPluginsPath() + "/azure-toolkit-for-intellij/azmcp"; |
| 100 | + final File azMcpDirFile = new File(azMcpDir); |
| 101 | + if (!azMcpDirFile.exists()) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + final Path versionFile = Path.of(azMcpDir + "/version.txt"); |
| 106 | + String currentVersion = null; |
| 107 | + if (versionFile.toFile().exists()) { |
| 108 | + currentVersion = new String(Files.readAllBytes(versionFile)); |
| 109 | + } |
| 110 | + |
| 111 | + final Path currentPackage = Path.of(azMcpDir + "/azmcp_package_" + currentVersion).toAbsolutePath(); |
| 112 | + Files.list(Path.of(azMcpDir)) |
| 113 | + .filter(path -> !path.equals(currentPackage)) |
| 114 | + .filter(path -> !path.equals(versionFile)) |
| 115 | + .forEach(path -> { |
| 116 | + delete(path); |
| 117 | + }); |
| 118 | + |
| 119 | + } catch (final Exception exception) { |
| 120 | + System.err.println("Error cleaning up Azure MCP Server: " + exception.getMessage()); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static void delete(Path path) { |
| 125 | + try { |
| 126 | + if (path.toFile().isDirectory()) { |
| 127 | + Files.list(path).forEach(AzureMcpPackageManager::delete); |
| 128 | + } |
| 129 | + Files.delete(path); |
| 130 | + } catch (final IOException e) { |
| 131 | + System.err.println("Error deleting file: " + path.toString()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private @NotNull String getExecutableRelativePath() { |
| 136 | + String executablePath = "/package/dist/azmcp"; |
| 137 | + if (SystemUtils.IS_OS_WINDOWS) { |
| 138 | + executablePath += ".exe"; |
| 139 | + } |
| 140 | + return executablePath; |
| 141 | + } |
| 142 | + |
| 143 | + private void extractTarGz(File tarGzFile, File destDir) throws IOException { |
| 144 | + try (final TarArchiveInputStream tarIn = new TarArchiveInputStream( |
| 145 | + new GzipCompressorInputStream( |
| 146 | + new FileInputStream(tarGzFile)))) { |
| 147 | + TarArchiveEntry entry; |
| 148 | + while ((entry = tarIn.getNextTarEntry()) != null) { |
| 149 | + final File outputFile = new File(destDir, entry.getName()); |
| 150 | + if (entry.isDirectory()) { |
| 151 | + if (!outputFile.exists()) { |
| 152 | + outputFile.mkdirs(); |
| 153 | + } |
| 154 | + } else { |
| 155 | + outputFile.getParentFile().mkdirs(); |
| 156 | + try (final FileOutputStream fos = new FileOutputStream(outputFile)) { |
| 157 | + tarIn.transferTo(fos); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private static String getPlatformIdentifier() { |
| 165 | + // Operating System detection |
| 166 | + String os = null; |
| 167 | + if (SystemUtils.IS_OS_WINDOWS) { |
| 168 | + os = "win32"; |
| 169 | + } else if (SystemUtils.IS_OS_LINUX) { |
| 170 | + os = "linux"; |
| 171 | + } else if (SystemUtils.IS_OS_MAC) { |
| 172 | + os = "darwin"; |
| 173 | + } else { |
| 174 | + throw new RuntimeException("Unsupported OS " + SystemUtils.OS_NAME); |
| 175 | + } |
| 176 | + final String arch = getArch(); |
| 177 | + return os + "-" + arch; |
| 178 | + } |
| 179 | + |
| 180 | + private static String getArch() { |
| 181 | + final String arch = SystemUtils.OS_ARCH.toLowerCase(); |
| 182 | + if (arch.contains("amd64") || arch.contains("x86_64") || arch.contains("x64")) { |
| 183 | + return "x64"; |
| 184 | + } |
| 185 | + if (arch.contains("aarch64") || arch.contains("arm64")) { |
| 186 | + return "arm64"; |
| 187 | + } |
| 188 | + throw new RuntimeException("Unsupported architecture: " + arch); |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | +} |
0 commit comments