|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { |
| 3 | + mkdtempSync, |
| 4 | + mkdirSync, |
| 5 | + readFileSync, |
| 6 | + rmSync, |
| 7 | + writeFileSync, |
| 8 | +} from "node:fs"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import { join, resolve } from "node:path"; |
| 11 | + |
| 12 | +const root = resolve(import.meta.dirname, ".."); |
| 13 | +const packageJson = JSON.parse( |
| 14 | + readFileSync(join(root, "package.json"), "utf8"), |
| 15 | +); |
| 16 | +const client = "@modelcontextprotocol/client"; |
| 17 | +const server = "@modelcontextprotocol/server"; |
| 18 | + |
| 19 | +for (const role of [client, server]) { |
| 20 | + if (!packageJson.peerDependencies?.[role]) { |
| 21 | + throw new Error(`${role} must remain a peer dependency`); |
| 22 | + } |
| 23 | + if (packageJson.peerDependenciesMeta?.[role]?.optional !== true) { |
| 24 | + throw new Error(`${role} must be an optional peer dependency`); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const temporaryRoot = mkdtempSync(join(tmpdir(), "ext-apps-role-peers-")); |
| 29 | +try { |
| 30 | + const npmEnvironment = { |
| 31 | + ...process.env, |
| 32 | + npm_config_cache: join(temporaryRoot, "npm-cache"), |
| 33 | + }; |
| 34 | + const packOutput = JSON.parse( |
| 35 | + execFileSync( |
| 36 | + "npm", |
| 37 | + [ |
| 38 | + "pack", |
| 39 | + "--ignore-scripts", |
| 40 | + "--json", |
| 41 | + "--pack-destination", |
| 42 | + temporaryRoot, |
| 43 | + ], |
| 44 | + { cwd: root, encoding: "utf8", env: npmEnvironment }, |
| 45 | + ), |
| 46 | + ); |
| 47 | + const tarball = join(temporaryRoot, packOutput[0].filename); |
| 48 | + |
| 49 | + const consumers = [ |
| 50 | + { |
| 51 | + name: "app-only", |
| 52 | + dependencies: { |
| 53 | + "@types/node": packageJson.devDependencies["@types/node"], |
| 54 | + [client]: packageJson.devDependencies[client], |
| 55 | + "@modelcontextprotocol/ext-apps": `file:${tarball}`, |
| 56 | + }, |
| 57 | + absent: server, |
| 58 | + entry: |
| 59 | + 'import { App } from "@modelcontextprotocol/ext-apps"; import { AppBridge } from "@modelcontextprotocol/ext-apps/app-bridge"; console.log(App, AppBridge);', |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "server-only", |
| 63 | + dependencies: { |
| 64 | + "@types/node": packageJson.devDependencies["@types/node"], |
| 65 | + "@modelcontextprotocol/ext-apps": `file:${tarball}`, |
| 66 | + [server]: packageJson.devDependencies[server], |
| 67 | + }, |
| 68 | + absent: client, |
| 69 | + entry: |
| 70 | + 'import { registerAppTool } from "@modelcontextprotocol/ext-apps/server"; console.log(registerAppTool);', |
| 71 | + }, |
| 72 | + ]; |
| 73 | + |
| 74 | + for (const consumer of consumers) { |
| 75 | + const directory = join(temporaryRoot, consumer.name); |
| 76 | + mkdirSync(directory); |
| 77 | + writeFileSync( |
| 78 | + join(directory, "package.json"), |
| 79 | + JSON.stringify({ |
| 80 | + private: true, |
| 81 | + type: "module", |
| 82 | + dependencies: consumer.dependencies, |
| 83 | + }), |
| 84 | + ); |
| 85 | + execFileSync( |
| 86 | + "npm", |
| 87 | + [ |
| 88 | + "install", |
| 89 | + "--ignore-scripts", |
| 90 | + "--package-lock=false", |
| 91 | + "--no-audit", |
| 92 | + "--no-fund", |
| 93 | + ], |
| 94 | + { cwd: directory, stdio: "pipe", env: npmEnvironment }, |
| 95 | + ); |
| 96 | + |
| 97 | + const absentPath = join( |
| 98 | + directory, |
| 99 | + "node_modules", |
| 100 | + ...consumer.absent.split("/"), |
| 101 | + "package.json", |
| 102 | + ); |
| 103 | + try { |
| 104 | + readFileSync(absentPath); |
| 105 | + throw new Error( |
| 106 | + `${consumer.name} unexpectedly installed ${consumer.absent}`, |
| 107 | + ); |
| 108 | + } catch (error) { |
| 109 | + if (error?.code !== "ENOENT") throw error; |
| 110 | + } |
| 111 | + |
| 112 | + writeFileSync(join(directory, "entry.ts"), consumer.entry); |
| 113 | + writeFileSync( |
| 114 | + join(directory, "tsconfig.json"), |
| 115 | + JSON.stringify({ |
| 116 | + compilerOptions: { |
| 117 | + lib: ["ES2020", "DOM"], |
| 118 | + module: "ESNext", |
| 119 | + moduleResolution: "bundler", |
| 120 | + noEmit: true, |
| 121 | + skipLibCheck: false, |
| 122 | + strict: true, |
| 123 | + target: "ES2020", |
| 124 | + }, |
| 125 | + include: ["entry.ts"], |
| 126 | + }), |
| 127 | + ); |
| 128 | + execFileSync( |
| 129 | + process.execPath, |
| 130 | + [join(root, "node_modules", "typescript", "bin", "tsc")], |
| 131 | + { cwd: directory, stdio: "inherit" }, |
| 132 | + ); |
| 133 | + |
| 134 | + const metafile = join(directory, "bundle-meta.json"); |
| 135 | + execFileSync( |
| 136 | + join(root, "node_modules", ".bin", "esbuild"), |
| 137 | + [ |
| 138 | + "entry.ts", |
| 139 | + "--bundle", |
| 140 | + "--platform=browser", |
| 141 | + "--outfile=bundle.js", |
| 142 | + `--metafile=${metafile}`, |
| 143 | + ], |
| 144 | + { cwd: directory, stdio: "pipe" }, |
| 145 | + ); |
| 146 | + const bundleInputs = Object.keys( |
| 147 | + JSON.parse(readFileSync(metafile, "utf8")).inputs, |
| 148 | + ).join("\n"); |
| 149 | + if (bundleInputs.includes(`/node_modules/${consumer.absent}/`)) { |
| 150 | + throw new Error( |
| 151 | + `${consumer.name} unexpectedly bundled ${consumer.absent}`, |
| 152 | + ); |
| 153 | + } |
| 154 | + } |
| 155 | +} finally { |
| 156 | + rmSync(temporaryRoot, { recursive: true, force: true }); |
| 157 | +} |
| 158 | + |
| 159 | +console.log("Role peer dependency isolation checks passed."); |
0 commit comments