MCPcopy Create free account
hub / github.com/modelcontextprotocol/mcpb / cleanMcpb

Function cleanMcpb

src/node/validate.ts:186–274  ·  view source on GitHub ↗
(inputPath: string)

Source from the content-addressed store, hash-verified

184}
185
186export async function cleanMcpb(inputPath: string) {
187 const tmpDir = await fs.mkdtemp(resolve(os.tmpdir(), "mcpb-clean-"));
188 const mcpbPath = resolve(tmpDir, "in.mcpb");
189 const unpackPath = resolve(tmpDir, "out");
190
191 console.log(" -- Cleaning MCPB...");
192
193 try {
194 await fs.copyFile(inputPath, mcpbPath);
195 console.log(" -- Unpacking MCPB...");
196 await unpackExtension({ mcpbPath, silent: true, outputDir: unpackPath });
197
198 const manifestPath = resolve(unpackPath, "manifest.json");
199 const originalManifest = await fs.readFile(manifestPath, "utf-8");
200 const manifestData = JSON.parse(originalManifest);
201 const manifestVersion = getManifestVersionFromRawData(manifestData);
202 if (!manifestVersion) {
203 throw new Error("Unrecognized or unsupported manifest version");
204 }
205 const result =
206 MANIFEST_SCHEMAS_LOOSE[manifestVersion].safeParse(manifestData);
207
208 if (!result.success) {
209 throw new Error(
210 `Unrecoverable manifest issues, please run "mcpb validate"`,
211 );
212 }
213 await fs.writeFile(manifestPath, JSON.stringify(result.data, null, 2));
214
215 if (
216 originalManifest.trim() !==
217 (await fs.readFile(manifestPath, "utf8")).trim()
218 ) {
219 console.log(" -- Update manifest to be valid per MCPB schema");
220 } else {
221 console.log(" -- Manifest already valid per MCPB schema");
222 }
223
224 const nodeModulesPath = resolve(unpackPath, "node_modules");
225 if (existsSync(nodeModulesPath)) {
226 console.log(" -- node_modules found, deleting development dependencies");
227
228 const destroyer = new DestroyerOfModules({
229 rootDirectory: unpackPath,
230 });
231
232 try {
233 await destroyer.destroy();
234 } catch (error) {
235 // If modules have already been deleted in a previous clean, the walker
236 // will fail when it can't find required dependencies. This is expected
237 // and safe to ignore.
238 if (
239 error instanceof Error &&
240 error.message.includes("Failed to locate module")
241 ) {
242 console.log(
243 " -- Some modules already removed, skipping remaining cleanup",

Callers 1

cli.tsFile · 0.85

Calls 3

unpackExtensionFunction · 0.85
packExtensionFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…