|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { createHash } from 'node:crypto'; |
| 4 | +import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs'; |
| 5 | +import { tmpdir } from 'node:os'; |
| 6 | +import path from 'node:path'; |
| 7 | +import { pathToFileURL } from 'node:url'; |
| 8 | + |
| 9 | +const project = 'https://github.com/jiangxingfan1-coder/Open-Agent-API'; |
| 10 | +const bundleName = 'open-agent-api-mcp'; |
| 11 | +const publisher = 'io.modelcontextprotocol.registry/publisher-provided'; |
| 12 | +const maxBundleBytes = 32 * 1024 * 1024; |
| 13 | + |
| 14 | +export function registryRecord(manifest, sha256) { |
| 15 | + const { version, name, server, user_config: config } = manifest; |
| 16 | + assert.ok(name === bundleName, 'Unexpected MCPB name.'); |
| 17 | + assert.ok( |
| 18 | + typeof version === 'string' && /^\d+\.\d+\.\d+(?:-[a-z0-9.-]+)?$/.test(version), |
| 19 | + 'Invalid MCPB version.', |
| 20 | + ); |
| 21 | + assert.ok(typeof sha256 === 'string' && /^[a-f0-9]{64}$/.test(sha256), 'Invalid MCPB digest.'); |
| 22 | + assert.ok( |
| 23 | + server?.type === 'node' && |
| 24 | + server.entry_point === 'server/index.mjs' && |
| 25 | + server.mcp_config?.command === 'node', |
| 26 | + 'Unexpected MCPB launcher.', |
| 27 | + ); |
| 28 | + assert.ok( |
| 29 | + Object.keys(server.mcp_config.env ?? {}).length === 0, |
| 30 | + 'The MCPB contract no longer uses only arguments.', |
| 31 | + ); |
| 32 | + |
| 33 | + const inputs = [ |
| 34 | + { name: 'gateway_url', type: 'string' }, |
| 35 | + { name: 'token_file', type: 'file' }, |
| 36 | + ]; |
| 37 | + assert.ok(Object.keys(config ?? {}).length === inputs.length, 'Unexpected MCPB inputs.'); |
| 38 | + for (const input of inputs) { |
| 39 | + assert.ok(config[input.name]?.type === input.type, `Unexpected MCPB input type: ${input.name}`); |
| 40 | + assert.ok(config[input.name]?.required === true, `Missing required MCPB input: ${input.name}`); |
| 41 | + } |
| 42 | + assert.ok( |
| 43 | + config.token_file.default === undefined, |
| 44 | + 'The token path must be supplied by the user.', |
| 45 | + ); |
| 46 | + const expected = [ |
| 47 | + '${__dirname}/server/index.mjs', |
| 48 | + ...inputs.map((input) => `\${user_config.${input.name}}`), |
| 49 | + ]; |
| 50 | + const args = server.mcp_config.args; |
| 51 | + assert.ok( |
| 52 | + Array.isArray(args) && |
| 53 | + args.length === expected.length && |
| 54 | + expected.every((arg, i) => arg === args[i]), |
| 55 | + 'Unexpected MCPB launch argument order or source.', |
| 56 | + ); |
| 57 | + |
| 58 | + return { |
| 59 | + $schema: 'https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json', |
| 60 | + name: `io.github.jiangxingfan1-coder/${bundleName}`, |
| 61 | + description: |
| 62 | + 'Connect MCP clients to Open Agent API. Requires a self-hosted gateway URL and scoped token file.', |
| 63 | + title: 'Open Agent API', |
| 64 | + repository: { url: project, source: 'github' }, |
| 65 | + websiteUrl: `${project}/blob/main/integrations/mcpb/README.md`, |
| 66 | + version, |
| 67 | + packages: [ |
| 68 | + { |
| 69 | + registryType: 'mcpb', |
| 70 | + identifier: `${project}/releases/download/mcp-open-agent-api-v${version}/${bundleName}-${version}.mcpb`, |
| 71 | + fileSha256: sha256, |
| 72 | + transport: { type: 'stdio' }, |
| 73 | + }, |
| 74 | + ], |
| 75 | + _meta: { [publisher]: { setup: { kind: 'mcpb-user-config', requiredInputs: inputs } } }, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +function fromBundle(file) { |
| 80 | + const stat = statSync(file); |
| 81 | + if (!stat.isFile() || stat.size < 1 || stat.size > maxBundleBytes) |
| 82 | + throw new Error('Expected a regular MCPB file no larger than 32 MiB.'); |
| 83 | + const bytes = readFileSync(file); |
| 84 | + // Read the manifest and hash the same snapshot; never extract or execute the entry point. |
| 85 | + const dir = mkdtempSync(path.join(tmpdir(), 'oaa-mcpb-registry-')); |
| 86 | + try { |
| 87 | + const snapshot = path.join(dir, 'bundle.mcpb'); |
| 88 | + writeFileSync(snapshot, bytes, { mode: 0o600 }); |
| 89 | + let manifest; |
| 90 | + try { |
| 91 | + manifest = JSON.parse( |
| 92 | + execFileSync('unzip', ['-p', snapshot, 'manifest.json'], { |
| 93 | + encoding: 'utf8', |
| 94 | + timeout: 10_000, |
| 95 | + maxBuffer: 128 * 1024, |
| 96 | + }), |
| 97 | + ); |
| 98 | + } catch { |
| 99 | + throw new Error( |
| 100 | + 'Cannot read MCPB manifest.json. Check the bundle and local unzip installation.', |
| 101 | + ); |
| 102 | + } |
| 103 | + return registryRecord(manifest, createHash('sha256').update(bytes).digest('hex')); |
| 104 | + } finally { |
| 105 | + rmSync(dir, { recursive: true, force: true }); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +if (process.argv[1] && import.meta.url === pathToFileURL(path.resolve(process.argv[1])).href) { |
| 110 | + if (process.argv.length !== 3) |
| 111 | + throw new Error('Usage: node scripts/mcpb-registry.mjs RELEASE.mcpb'); |
| 112 | + process.stdout.write(JSON.stringify(fromBundle(process.argv[2]), null, 2) + '\n'); |
| 113 | +} |
0 commit comments