|
6 | 6 |
|
7 | 7 | import {execSync} from 'node:child_process'; |
8 | 8 | import fs from 'node:fs'; |
| 9 | +import os from 'node:os'; |
| 10 | +import path from 'node:path'; |
9 | 11 |
|
10 | | -const serverJsonFilePath = './server.json'; |
| 12 | +const serverJsonFilePath = path.join(process.cwd(), 'server.json'); |
11 | 13 | const serverJson = JSON.parse(fs.readFileSync(serverJsonFilePath, 'utf-8')); |
12 | | -fs.unlinkSync(serverJsonFilePath); |
13 | 14 |
|
14 | | -// Create the new server.json |
15 | | -execSync('./mcp-publisher init'); |
| 15 | +const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mcp-verify-')); |
16 | 16 |
|
17 | | -const newServerJson = JSON.parse(fs.readFileSync(serverJsonFilePath, 'utf-8')); |
18 | | - |
19 | | -const propertyToVerify = ['$schema']; |
20 | | -const diffProps = []; |
| 17 | +try { |
| 18 | + const osName = os.platform(); |
| 19 | + const arch = os.arch(); |
| 20 | + let platform = ''; |
| 21 | + if (osName === 'darwin') { |
| 22 | + platform = 'darwin'; |
| 23 | + } else if (osName === 'linux') { |
| 24 | + platform = 'linux'; |
| 25 | + } |
| 26 | + // mcp-publisher does not support windows |
| 27 | + else { |
| 28 | + throw new Error(`Unsupported platform: ${osName}`); |
| 29 | + } |
21 | 30 |
|
22 | | -for (const prop of propertyToVerify) { |
23 | | - if (serverJson[prop] !== newServerJson[prop]) { |
24 | | - diffProps.push(prop); |
| 31 | + let archName = ''; |
| 32 | + if (arch === 'x64') { |
| 33 | + archName = 'amd64'; |
| 34 | + } else if (arch === 'arm64') { |
| 35 | + archName = 'arm64'; |
| 36 | + } else { |
| 37 | + throw new Error(`Unsupported architecture: ${arch}`); |
25 | 38 | } |
26 | | -} |
27 | 39 |
|
28 | | -fs.writeFileSync('./server.json', JSON.stringify(serverJson, null, 2)); |
| 40 | + const osArch = `${platform}_${archName}`; |
| 41 | + const binName = 'mcp-publisher'; |
| 42 | + const downloadUrl = `https://github.com/modelcontextprotocol/registry/releases/latest/download/${binName}_${osArch}.tar.gz`; |
| 43 | + |
| 44 | + console.log(`Downloading ${binName} from ${downloadUrl}`); |
| 45 | + const downloadCmd = `curl -L "${downloadUrl}" | tar xz -C "${tmpDir}" ${binName}`; |
| 46 | + execSync(downloadCmd, {stdio: 'inherit'}); |
| 47 | + |
| 48 | + const publisherPath = path.join(tmpDir, binName); |
| 49 | + fs.chmodSync(publisherPath, 0o755); |
| 50 | + console.log(`Downloaded to ${publisherPath}`); |
29 | 51 |
|
30 | | -if (diffProps.length) { |
31 | | - throw new Error( |
32 | | - `The following props did not match the latest init value:\n${diffProps.map( |
33 | | - prop => `- "${prop}": "${newServerJson[prop]}"`, |
34 | | - )}`, |
35 | | - ); |
| 52 | + // Create the new server.json in the temporary directory |
| 53 | + execSync(`${publisherPath} init`, {cwd: tmpDir, stdio: 'inherit'}); |
| 54 | + |
| 55 | + const newServerJsonPath = path.join(tmpDir, 'server.json'); |
| 56 | + const newServerJson = JSON.parse(fs.readFileSync(newServerJsonPath, 'utf-8')); |
| 57 | + |
| 58 | + const propertyToVerify = ['$schema']; |
| 59 | + const diffProps = []; |
| 60 | + |
| 61 | + for (const prop of propertyToVerify) { |
| 62 | + if (serverJson[prop] !== newServerJson[prop]) { |
| 63 | + diffProps.push(prop); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (diffProps.length) { |
| 68 | + throw new Error( |
| 69 | + `The following props in ${serverJsonFilePath} did not match the latest init value:\n${diffProps.map( |
| 70 | + prop => |
| 71 | + `- "${prop}": expected "${newServerJson[prop]}", got "${serverJson[prop]}"`, |
| 72 | + )}`, |
| 73 | + ); |
| 74 | + } |
| 75 | +} finally { |
| 76 | + fs.rmSync(tmpDir, {recursive: true, force: true}); |
36 | 77 | } |
0 commit comments