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

Function unpackExtension

src/cli/unpack.ts:20–139  ·  view source on GitHub ↗
({
  mcpbPath,
  outputDir,
  silent,
}: UnpackOptions)

Source from the content-addressed store, hash-verified

18}
19
20export async function unpackExtension({
21 mcpbPath,
22 outputDir,
23 silent,
24}: UnpackOptions): Promise<boolean> {
25 const logger = getLogger({ silent });
26 const resolvedMcpbPath = resolve(mcpbPath);
27
28 if (!existsSync(resolvedMcpbPath)) {
29 logger.error(`ERROR: MCPB file not found: ${mcpbPath}`);
30 return false;
31 }
32
33 const finalOutputDir = outputDir ? resolve(outputDir) : process.cwd();
34
35 if (!existsSync(finalOutputDir)) {
36 mkdirSync(finalOutputDir, { recursive: true });
37 }
38
39 try {
40 const fileContent = readFileSync(resolvedMcpbPath);
41 const { originalContent } = extractSignatureBlock(fileContent);
42
43 // Parse file attributes from ZIP central directory
44 const fileAttributes = new Map<string, number>();
45 const isUnix = process.platform !== "win32";
46
47 if (isUnix) {
48 // Parse ZIP central directory to extract file attributes
49 const zipBuffer = originalContent;
50
51 // Find end of central directory record
52 let eocdOffset = -1;
53 for (let i = zipBuffer.length - 22; i >= 0; i--) {
54 if (zipBuffer.readUInt32LE(i) === 0x06054b50) {
55 eocdOffset = i;
56 break;
57 }
58 }
59
60 if (eocdOffset !== -1) {
61 const centralDirOffset = zipBuffer.readUInt32LE(eocdOffset + 16);
62 const centralDirEntries = zipBuffer.readUInt16LE(eocdOffset + 8);
63
64 let offset = centralDirOffset;
65
66 for (let i = 0; i < centralDirEntries; i++) {
67 if (zipBuffer.readUInt32LE(offset) === 0x02014b50) {
68 const externalAttrs = zipBuffer.readUInt32LE(offset + 38);
69 const filenameLength = zipBuffer.readUInt16LE(offset + 28);
70 const filename = zipBuffer.toString(
71 "utf8",
72 offset + 46,
73 offset + 46 + filenameLength,
74 );
75
76 // Extract Unix permissions from external attributes (upper 16 bits)
77 const mode = (externalAttrs >> 16) & 0o777;

Callers 2

cli.tsFile · 0.85
cleanMcpbFunction · 0.85

Calls 2

getLoggerFunction · 0.85
extractSignatureBlockFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…