diff --git a/docs/package-contract.md b/docs/package-contract.md index 56bdd04b..47e46ee5 100644 --- a/docs/package-contract.md +++ b/docs/package-contract.md @@ -1,6 +1,7 @@ # Mini App package contract -Verified against MiniMax Code 3.1.21. +Verified against MiniMax Code 3.1.21. The `mcpEndpoints` rule was read off the 3.1.14 reader; +the 3.1.4 reader is identical. A Mini App is a MiniMax Plugin whose `package.json` declares a Mini App payload. MiniMax Code enforces the manifest, payload, and path rules below when the package is installed. `npm run check` @@ -53,7 +54,7 @@ committed file. | `category` | One of `Office`, `Studio`, `Design & Sites`, `Code`, `Business`, `Sales`, `Productivity`, `Science & Healthcare`, `Education`, `Other`. | | `exampleQueries` | Array of non-empty strings. Provide at least one; the Agent uses them to open the Mini App by name. | | `apps` | Array of `*.app.json` paths; each file must exist and be `{ "schemaVersion": 1, "provider": "" }` (plus an optional `$schema`), with `provider` matching `^[a-z0-9_-]{1,64}$` and unique across the files. Each names a Connector app the plugin works with. From MiniMax Code 3.1.9 the plugin details list them with their connection status, and the client skips an entry that is missing, invalid, or repeats a provider instead of refusing the package; earlier clients ignore the field. | -| `mcpServers` | Array of `*.mcp.json` paths; each file must exist. These are MCP servers the plugin offers to the Agent, distinct from `mcpEndpoints` in `miniapp.json`. | +| `mcpServers` | Array of `*.mcp.json` paths; each file must exist. These are MCP servers the plugin offers to the Agent, distinct from `mcpEndpoints` in `miniapp.json`. The server names come from each file's `mcpServers` map. | | `skills` | Array of `skills//SKILL.md` paths; each file must exist. | | `hooks` | Optional array of `*.json` paths; each file must exist. Contents are validated by MiniMax Code at install time. | | `hostBindings` | Optional array of `bindings/.binding.json` paths; each file must exist. Contents are validated at install time. | @@ -115,9 +116,10 @@ No other keys are allowed inside `mcode`. Other top-level keys (`name`, `type`, Code 3.1.15, though the desktop Mini App Pad currently opens `surface.path` directly and renders no compact widget view, so new packages should omit it. - `mcpEndpoints` is an array of `{ "server": string, "path": string }`. `server` matches - `^[a-zA-Z0-9_-]{1,128}$` and must name a server declared through `plugin.json.mcpServers`; MiniMax - Code checks that reference at install time. `server` and `path` are each unique. Use `[]` when - there are none. + `^[a-zA-Z0-9_-]{1,128}$` and must name a server declared through `plugin.json.mcpServers`; + `npm run check` reports a `server` that no `*.mcp.json` declares as + `MINIAPP_MCP_ENDPOINT_UNDECLARED`, and MiniMax Code checks the same reference at install + time. `server` and `path` are each unique. Use `[]` when there are none. - `fileHandlers` is optional (MiniMax Code 3.1.18 or later); see [`fileHandlers`](#filehandlers). - No route (`surface.path`, an MCP endpoint `path`, or a `fileHandlers` `path`) may be `/__mavis` or start with `/__mavis/`. The Host owns that prefix and answers some of it itself. diff --git a/scripts/lib/package.mjs b/scripts/lib/package.mjs index 155c17a5..a462b501 100644 --- a/scripts/lib/package.mjs +++ b/scripts/lib/package.mjs @@ -1,11 +1,11 @@ import path from 'node:path'; -import { readJsonFile } from './fs.mjs'; +import { readJsonFile, isRecord } from './fs.mjs'; import { createReport } from './report.mjs'; import { checkClient } from './rules/client.mjs'; import { checkFiles } from './rules/files.mjs'; import { checkImageFile, checkLayout, checkNameMatchesDirectory } from './rules/layout.mjs'; -import { checkManifest } from './rules/manifest.mjs'; +import { checkManifest, readMcpServerNames } from './rules/manifest.mjs'; import { checkMcode } from './rules/mcode.mjs'; import { checkMiniApp, emptyMiniApp } from './rules/miniapp.mjs'; import { checkNode } from './rules/node.mjs'; @@ -21,12 +21,19 @@ export async function validatePackage(packageDir, { requireLowercaseAuthor = tru const manifest = await readJson(report, dir, '.minimax-plugin/plugin.json', 'MANIFEST_UNREADABLE'); const features = []; + let mcpServerNames; if (manifest.present) { const identity = await checkManifest(report, { packageDir: dir, manifest: manifest.value }); features.push(...(identity.features ?? [])); await checkNameMatchesDirectory(report, { packageDir: dir, name: identity.name }); if (identity.icon) await checkImageFile(report, { packageDir: dir, relativePath: identity.icon, label: 'icon' }); if (identity.darkIcon) await checkImageFile(report, { packageDir: dir, relativePath: identity.darkIcon, label: 'darkIcon' }); + // The declared server names decide which MCP endpoints may exist, so they are read here and + // handed to the Mini App rule. + mcpServerNames = await readMcpServerNames( + dir, + isRecord(manifest.value) ? manifest.value.mcpServers : undefined, + ); } const packageJson = await readJson(report, dir, 'package.json', 'MCODE_UNREADABLE'); @@ -34,7 +41,9 @@ export async function validatePackage(packageDir, { requireLowercaseAuthor = tru const miniapp = await readJson(report, dir, 'miniapp/miniapp.json', 'MINIAPP_UNREADABLE'); let resolved = emptyMiniApp(); - if (miniapp.present) resolved = await checkMiniApp(report, { packageDir: dir, manifest: miniapp.value }); + if (miniapp.present) { + resolved = await checkMiniApp(report, { packageDir: dir, manifest: miniapp.value, mcpServerNames }); + } features.push(...resolved.features); await checkFiles(report, { packageDir: dir }); diff --git a/scripts/lib/rules/manifest.mjs b/scripts/lib/rules/manifest.mjs index a54b810f..49458560 100644 --- a/scripts/lib/rules/manifest.mjs +++ b/scripts/lib/rules/manifest.mjs @@ -23,6 +23,37 @@ import { normalizePluginPath } from '../paths.mjs'; const FILE = '.minimax-plugin/plugin.json'; +/** + * The MCP server names a plugin declares. + * + * `plugin.json.mcpServers` names the files; the server names live inside them, under each file's + * `mcpServers` map. The Host resolves `mcpEndpoints[].server` against exactly those names and + * refuses an endpoint that names a server the plugin does not declare, so the checker needs the + * names too. + * + * `undefined` means "not known": a file is missing, unreadable, or shaped differently. The + * reference error is already reported in that case, and guessing an empty list here would turn + * every endpoint of that package into a false positive. + * + * @param {string} packageDir + * @param {string[] | undefined} files + * @returns {Promise} + */ +export async function readMcpServerNames(packageDir, files) { + if (files === undefined) return undefined; + const names = new Set(); + for (const item of new Set(files)) { + const portable = normalizePluginPath(item); + if (!portable.ok || !MCP_PATH.test(portable.value)) return undefined; + const absolutePath = path.join(packageDir, ...portable.value.split('/')); + if (!(await pathExists(absolutePath, 'file'))) return undefined; + const file = await readJsonFile(absolutePath); + if (!file.ok || !isRecord(file.value) || !isRecord(file.value.mcpServers)) return undefined; + for (const name of Object.keys(file.value.mcpServers)) names.add(name); + } + return [...names]; +} + export async function checkManifest(report, { packageDir, manifest }) { if (!isRecord(manifest)) { report.error('MANIFEST_NOT_OBJECT', 'plugin.json must be a JSON object', FILE); diff --git a/scripts/lib/rules/miniapp.mjs b/scripts/lib/rules/miniapp.mjs index c6573ab1..cb6a0f2c 100644 --- a/scripts/lib/rules/miniapp.mjs +++ b/scripts/lib/rules/miniapp.mjs @@ -53,7 +53,7 @@ export function emptyMiniApp() { return { nodeRoots: [], clientRoots: [], features: [], access: emptyAccess() }; } -export async function checkMiniApp(report, { packageDir, manifest }) { +export async function checkMiniApp(report, { packageDir, manifest, mcpServerNames }) { const resolved = emptyMiniApp(); if (!isRecord(manifest)) { report.error('MINIAPP_NOT_OBJECT', 'miniapp.json must be a JSON object', FILE); @@ -70,7 +70,7 @@ export async function checkMiniApp(report, { packageDir, manifest }) { const entry = await checkRuntime(report, packageDir, manifest.runtime, artifacts.node); if (entry) resolved.entry = entry; checkSurface(report, manifest.surface); - checkMcpEndpoints(report, manifest.mcpEndpoints); + checkMcpEndpoints(report, manifest.mcpEndpoints, mcpServerNames); resolved.access = manifest.permissions === undefined ? checkLegacyAccess(report, manifest, resolved.features) @@ -223,12 +223,16 @@ function checkRouteNotReserved(report, route, label) { } } -function checkMcpEndpoints(report, value) { +function checkMcpEndpoints(report, value, mcpServerNames) { if (value === undefined) return; if (!Array.isArray(value)) { report.error('MINIAPP_MCP_ENDPOINT_INVALID', 'mcpEndpoints must be an array (use [] when empty)', FILE); return; } + // The Host resolves an endpoint against the servers the plugin declares and refuses one that + // names a server nobody declared. `undefined` means the declared names are not known, and then + // there is nothing to compare against. + const declared = mcpServerNames === undefined ? undefined : new Set(mcpServerNames); const servers = new Set(); const paths = new Set(); for (const item of value) { @@ -245,6 +249,10 @@ function checkMcpEndpoints(report, value) { report.error('MINIAPP_MCP_ENDPOINT_INVALID', `MCP endpoint path ${route.reason}`, FILE); continue; } + if (declared && !declared.has(item.server)) { + report.error('MINIAPP_MCP_ENDPOINT_UNDECLARED', `MCP endpoint server "${item.server}" is not declared by the plugin's mcpServers`, FILE); + continue; + } if (servers.has(item.server) || paths.has(route.value)) { report.error('MINIAPP_MCP_ENDPOINT_INVALID', 'MCP endpoint server and path must each be unique', FILE); } diff --git a/test/miniapp.test.mjs b/test/miniapp.test.mjs index 82017d2d..5da96459 100644 --- a/test/miniapp.test.mjs +++ b/test/miniapp.test.mjs @@ -6,7 +6,7 @@ import { checkMiniApp } from '../scripts/lib/rules/miniapp.mjs'; import { createReport } from '../scripts/lib/report.mjs'; import { codes, copyExample, makeTmpRoot, readJson } from './helpers.mjs'; -async function run(mutate, prepare) { +async function run(mutate, prepare, options = {}) { const root = await makeTmpRoot(); const { dir } = await copyExample(root); const manifest = await readJson(path.join(dir, 'miniapp', 'miniapp.json')); @@ -14,7 +14,7 @@ async function run(mutate, prepare) { const next = result === undefined ? manifest : result; if (prepare) await prepare(dir); const report = createReport(); - const resolved = await checkMiniApp(report, { packageDir: dir, manifest: next }); + const resolved = await checkMiniApp(report, { packageDir: dir, manifest: next, mcpServerNames: options.mcpServerNames }); return { report, resolved }; } @@ -80,6 +80,30 @@ test('miniapp: mcpEndpoints shape and uniqueness', async () => { assert.deepEqual(codes(badServer.report.diagnostics), ['MINIAPP_MCP_ENDPOINT_INVALID']); }); +test('miniapp: an MCP endpoint must name a server the plugin declares', async () => { + const declared = await run( + (m) => { m.mcpEndpoints = [{ server: 'a', path: '/mcp/a' }]; return m; }, + undefined, + { mcpServerNames: ['a'] }, + ); + assert.deepEqual(declared.report.diagnostics, []); + const ghost = await run( + (m) => { m.mcpEndpoints = [{ server: 'ghost', path: '/mcp/ghost' }]; return m; }, + undefined, + { mcpServerNames: ['a'] }, + ); + assert.deepEqual(codes(ghost.report.diagnostics), ['MINIAPP_MCP_ENDPOINT_UNDECLARED']); + const noneDeclared = await run( + (m) => { m.mcpEndpoints = [{ server: 'a', path: '/mcp/a' }]; return m; }, + undefined, + { mcpServerNames: [] }, + ); + assert.deepEqual(codes(noneDeclared.report.diagnostics), ['MINIAPP_MCP_ENDPOINT_UNDECLARED']); + // The declared names are not known: no verdict, and no false positive either. + const unknown = await run((m) => { m.mcpEndpoints = [{ server: 'ghost', path: '/mcp/ghost' }]; return m; }); + assert.deepEqual(unknown.report.diagnostics, []); +}); + test('miniapp: hostConnectorAccess is validated for shape and reported as unverified when non-empty', async () => { const ok = await run((m) => { m.hostConnectorAccess = { providers: ['notion'] }; return m; }); assert.deepEqual(codes(ok.report.diagnostics), ['HOST_CONNECTOR_UNVERIFIED']); diff --git a/test/package.test.mjs b/test/package.test.mjs index 36e2d918..5b867d65 100644 --- a/test/package.test.mjs +++ b/test/package.test.mjs @@ -52,6 +52,21 @@ test('package: an example is not subject to the lowercase-author rule', async () assert.deepEqual(await validatePackage(dir, { requireLowercaseAuthor: false }), []); }); +test('package: an MCP endpoint that names no declared server is an error', async () => { + const root = await makeTmpRoot(); + const { dir } = await copyExample(root); + const mcpFile = path.join(dir, 'servers.mcp.json'); + await writeFile(mcpFile, `${JSON.stringify({ + schemaVersion: 1, + mcpServers: { real: { type: 'streamable-http', url: 'http://127.0.0.1:9/mcp/real' } }, + })}\n`); + await editJson(path.join(dir, '.minimax-plugin', 'plugin.json'), (m) => ({ ...m, mcpServers: ['servers.mcp.json'] })); + await editJson(path.join(dir, 'miniapp', 'miniapp.json'), (m) => ({ ...m, mcpEndpoints: [{ server: 'ghost', path: '/mcp/ghost' }] })); + assert.deepEqual(codes(await validatePackage(dir)), ['MINIAPP_MCP_ENDPOINT_UNDECLARED']); + await editJson(path.join(dir, 'miniapp', 'miniapp.json'), (m) => ({ ...m, mcpEndpoints: [{ server: 'real', path: '/mcp/real' }] })); + assert.deepEqual(await validatePackage(dir), []); +}); + test('package: features from plugin.json and miniapp.json reach the README minimum-version check', async () => { const root = await makeTmpRoot(); const { dir } = await copyExample(root);