Merge pull request #208 from modelcontextprotocol/feat/pack-manifest-flag

feat: add --manifest flag to pack command
This commit is contained in:
Bryan Thompson 2026-04-14 07:42:57 -05:00 • committed by GitHub
commit a2c331b513
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 127 additions and 18 deletions

View file

@ -101,22 +101,33 @@ program
program program
.command("pack [directory] [output]") .command("pack [directory] [output]")
.description("Pack a directory into an MCPB extension") .description("Pack a directory into an MCPB extension")
.action((directory: string = process.cwd(), output?: string) => { .option(
void (async () => { "-m, --manifest <path>",
try { "Path to manifest file (defaults to manifest.json in directory)",
const success = await packExtension({ )
extensionPath: directory, .action(
outputPath: output, (
}); directory: string = process.cwd(),
process.exit(success ? 0 : 1); output: string | undefined,
} catch (error) { options: { manifest?: string },
console.error( ) => {
`ERROR: ${error instanceof Error ? error.message : "Unknown error"}`, void (async () => {
); try {
process.exit(1); const success = await packExtension({
} extensionPath: directory,
})(); outputPath: output,
}); manifestPath: options.manifest,
});
process.exit(success ? 0 : 1);
} catch (error) {
console.error(
`ERROR: ${error instanceof Error ? error.message : "Unknown error"}`,
);
process.exit(1);
}
})();
},
);
// Unpack command // Unpack command
program program

View file

@ -22,6 +22,7 @@ interface PackOptions {
extensionPath: string; extensionPath: string;
outputPath?: string; outputPath?: string;
silent?: boolean; silent?: boolean;
manifestPath?: string;
} }
function formatFileSize(bytes: number): string { function formatFileSize(bytes: number): string {
@ -50,6 +51,7 @@ export async function packExtension({
extensionPath, extensionPath,
outputPath, outputPath,
silent, silent,
manifestPath: customManifestPath,
}: PackOptions): Promise<boolean> { }: PackOptions): Promise<boolean> {
const resolvedPath = resolve(extensionPath); const resolvedPath = resolve(extensionPath);
const logger = getLogger({ silent }); const logger = getLogger({ silent });
@ -60,9 +62,18 @@ export async function packExtension({
return false; return false;
} }
// Check if manifest exists // Resolve manifest path
const manifestPath = join(resolvedPath, "manifest.json"); const manifestPath = customManifestPath
? resolve(customManifestPath)
: join(resolvedPath, "manifest.json");
if (!existsSync(manifestPath)) { if (!existsSync(manifestPath)) {
if (customManifestPath) {
// When --manifest is explicitly provided, error immediately
logger.error(`ERROR: Manifest file not found: ${customManifestPath}`);
return false;
}
logger.log(`No manifest.json found in ${extensionPath}`); logger.log(`No manifest.json found in ${extensionPath}`);
const shouldInit = await confirm({ const shouldInit = await confirm({
message: "Would you like to create a manifest.json file?", message: "Would you like to create a manifest.json file?",
@ -139,6 +150,15 @@ export async function packExtension({
mcpbIgnorePatterns, mcpbIgnorePatterns,
); );
// When using a custom manifest path, inject the manifest into the bundle
if (customManifestPath) {
const manifestStat = statSync(manifestPath);
files["manifest.json"] = {
data: readFileSync(manifestPath),
mode: manifestStat.mode,
};
}
// Print package header // Print package header
logger.log(`\n📦 ${manifest.name}@${manifest.version}`); logger.log(`\n📦 ${manifest.name}@${manifest.version}`);

View file

@ -265,6 +265,84 @@ describe("DXT CLI", () => {
expect(originalFile2).toEqual(unpackedFile2); expect(originalFile2).toEqual(unpackedFile2);
}); });
it("should pack with --manifest pointing to a separate directory", () => {
const projectDir = join(__dirname, "temp-manifest-project");
const manifestDir = join(__dirname, "temp-manifest-separate");
const manifestPackedPath = join(__dirname, "test-manifest-flag.mcpb");
try {
// Create project directory with source files (no manifest)
fs.mkdirSync(join(projectDir, "server"), { recursive: true });
fs.writeFileSync(
join(projectDir, "server", "index.js"),
"console.log('hello');",
);
// Create separate manifest directory
fs.mkdirSync(manifestDir, { recursive: true });
fs.writeFileSync(
join(manifestDir, "manifest.json"),
JSON.stringify({
manifest_version: DEFAULT_MANIFEST_VERSION,
name: "Test Separate Manifest",
version: "1.0.0",
description: "Test with separate manifest",
author: { name: "MCPB" },
server: {
type: "node",
entry_point: "server/index.js",
mcp_config: { command: "node" },
},
}),
);
const result = execSync(
`node ${cliPath} pack ${projectDir} ${manifestPackedPath} --manifest ${join(manifestDir, "manifest.json")}`,
{ encoding: "utf-8" },
);
expect(fs.existsSync(manifestPackedPath)).toBe(true);
expect(result).toContain("Validating manifest");
} finally {
fs.rmSync(projectDir, { recursive: true, force: true });
fs.rmSync(manifestDir, { recursive: true, force: true });
if (fs.existsSync(manifestPackedPath)) {
fs.unlinkSync(manifestPackedPath);
}
}
});
it("should fail with --manifest pointing to nonexistent file", () => {
const projectDir = join(__dirname, "temp-manifest-missing-project");
try {
fs.mkdirSync(projectDir, { recursive: true });
fs.writeFileSync(join(projectDir, "index.js"), "console.log('hello');");
expect(() => {
execSync(
`node ${cliPath} pack ${projectDir} /tmp/out.mcpb --manifest /nonexistent/manifest.json`,
{ encoding: "utf-8", stdio: "pipe" },
);
}).toThrow();
try {
execSync(
`node ${cliPath} pack ${projectDir} /tmp/out.mcpb --manifest /nonexistent/manifest.json`,
{ encoding: "utf-8", stdio: "pipe" },
);
} catch (error: unknown) {
const execError = error as { stdout?: Buffer; stderr?: Buffer };
const output =
(execError.stdout?.toString() || "") +
(execError.stderr?.toString() || "");
expect(output).toContain("Manifest file not found");
}
} finally {
fs.rmSync(projectDir, { recursive: true, force: true });
}
});
it("should preserve executable file permissions after packing and unpacking", () => { it("should preserve executable file permissions after packing and unpacking", () => {
// Skip this test on Windows since it doesn't support Unix permissions // Skip this test on Windows since it doesn't support Unix permissions
if (process.platform === "win32") { if (process.platform === "win32") {