diff --git a/apps/medusa/integration-tests/http/mcp-write.spec.ts b/apps/medusa/integration-tests/http/mcp-write.spec.ts index 8a97b7b55..718e4ac34 100644 --- a/apps/medusa/integration-tests/http/mcp-write.spec.ts +++ b/apps/medusa/integration-tests/http/mcp-write.spec.ts @@ -232,7 +232,7 @@ medusaIntegrationTestRunner({ limits: expect.stringContaining('the real call can still be rejected'), proposed: draft, current: null, - note: expect.stringContaining('no current values'), + note: '"Create Product": current values are shown only for a write to a path that ends in a resource id, so only the proposal is shown.', }); const rejected = await callTool(api, fullToken, 'write_api', { operation: 'POST /admin/products', body: draft }); @@ -280,6 +280,14 @@ medusaIntegrationTestRunner({ proposed: null, }); expect(preview.structuredContent?.limits).not.toContain('matches the published schema'); + + const action = await callTool(api, fullToken, 'write_api', { + operation: 'POST /admin/products/export', + dry_run: true, + }); + expect(action.structuredContent?.note).toBe( + '"Export Products": current values are shown only for a write to a path that ends in a resource id, so only the proposal is shown.', + ); }); it('does not show current values in a dry run for a token without mcp:read', async () => { diff --git a/packages/medusa-mcp/README.md b/packages/medusa-mcp/README.md index 6f5a1d639..23f768de9 100644 --- a/packages/medusa-mcp/README.md +++ b/packages/medusa-mcp/README.md @@ -28,8 +28,17 @@ Status: in development on `feat/medusa-mcp`. Design and plan: Arbor thread DEM-1 A dry run checks the body against the published spec only. Medusa enforces business rules in its workflows, which no spec describes: creating a product without `options` passes the schema and fails in Medusa's create workflow. So every preview says it was not executed and whether the body was checked (`schema_check`), and a failed call returns an `error` object with the phase (`schema` or `medusa`), -Medusa's `type`, `code`, and `message`, a hint for the status, and the spec revision, so an agent can correct the body -and call again. +Medusa's `type`, `code`, and `message` when Medusa sends them (`code` is often absent), and a hint for the status, so +an agent can correct the body and call again. `spec_revision` sits beside `error`, at the top of the result. + +A failed call is an MCP tool result with `isError: true`, as the MCP spec asks for execution errors, not a JSON-RPC +error. ChatGPT presents such a result as a tool exception carrying the text. The result takes one of three shapes: + +| Failure | Text content | `structuredContent` | +| --- | --- | --- | +| Medusa rejected the call | The same JSON as `structuredContent` | `operation`, `status`, `body`, `error` (`phase: 'medusa'`), `spec_revision` | +| The body failed the schema | A readable list of problems | `operation`, `error` (`phase: 'schema'`, `problems`, `hint`), `spec_revision` | +| Refused before any call: unknown operation, wrong tool, blocked by policy, missing scope, rate limit, a body that is not JSON, bad path parameters | A readable message | None | Calls run against Medusa's own API as the admin who connected, with a short-lived admin JWT, so Medusa's permissions and validators apply. A built-in blocklist (`src/mcp/policy.ts`) keeps agents away from auth, diff --git a/packages/medusa-mcp/src/mcp/server.ts b/packages/medusa-mcp/src/mcp/server.ts index 50422d942..6a820525b 100644 --- a/packages/medusa-mcp/src/mcp/server.ts +++ b/packages/medusa-mcp/src/mcp/server.ts @@ -389,9 +389,16 @@ export function buildMcpServer(registry: ApiRegistry, target: ExecutionTarget): const proposalOnly = (note: string) => ok({ ...preview, proposed: args.body ?? null, current: null, note }); const read = catalog.byKey.get(`GET ${operation.path}`); - if (!/\{[^}]+\}$/.test(operation.path) || !read || read.blocked_by) { - return proposalOnly('This operation creates or acts on a resource, so there are no current values to compare.'); + if (!/\{[^}]+\}$/.test(operation.path)) { + // The spec does not say whether a POST creates, updates, or acts, so the note names the operation and states what + // the preview does rather than guessing what the call will do. + const name = operation.summary ? `"${operation.summary}"` : 'This operation'; + return proposalOnly( + `${name}: current values are shown only for a write to a path that ends in a resource id, so only the proposal is shown.`, + ); } + if (!read || read.blocked_by) + return proposalOnly('This resource cannot be read here, so current values are not shown.'); if (!target.scopes.includes('mcp:read')) return proposalOnly('Current values need the mcp:read scope.'); const query = declaredFor(read).query.has('fields') && fields.length ? { fields: fields.join(',') } : undefined;