Upload images through the API, and from the MCP server
POST /api/v1/media stores an image where the panel keeps a page's and answers with the path to send as `featured_image`. Without it the MCP server could create a page but never give it a picture. The file goes through Media::for(), so it is processed, served and pruned like one uploaded in the panel. Two ways in, one per request: a multipart `file`, or JSON with a `url` the API downloads itself. JPEG, PNG, WebP, GIF or AVIF only, decided from the bytes; no SVG, which is a document that can carry script. The URL is fetched from inside the stack's network, so RemoteImage guards it: HTTPS only, every resolved address must be public, the connection is pinned to the checked address with CURLOPT_RESOLVE so a changing DNS answer cannot swap in a private one, redirects are not followed, and the body is read to a ceiling whatever its headers say. Uploading needs permission to write pages. MediaPolicy::create stays false, so the panel gains no free-standing "new file" button. In the MCP server a multipart binary field becomes `<field>_path` over stdio: the server reads the file from the caller's disk and sends it as multipart, instead of the model pushing base64 through its context. Over HTTP the server's disk is not the caller's, so only `body.url` is offered. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -30,6 +30,7 @@ const DROPPED_KEYWORDS = new Set([
|
||||
* @property {string} path Path as documented, e.g. /v1/pages/{page}.
|
||||
* @property {{name: string, in: string}[]} params
|
||||
* @property {boolean} hasBody
|
||||
* @property {string[]} files Multipart fields filled from a local file, read from `<name>_path`.
|
||||
* @property {boolean} requiresAuth
|
||||
*
|
||||
* @typedef {object} Tool
|
||||
@@ -42,9 +43,13 @@ const DROPPED_KEYWORDS = new Set([
|
||||
|
||||
/**
|
||||
* @param {object} spec The parsed OpenAPI document.
|
||||
* @param {object} [options]
|
||||
* @param {boolean} [options.localFiles] Offer uploads from a path on this
|
||||
* machine. Only true over stdio, where this machine is the caller's own; a
|
||||
* deployed server would be reading its own disk on a stranger's say-so.
|
||||
* @returns {Tool[]}
|
||||
*/
|
||||
export function buildTools(spec) {
|
||||
export function buildTools(spec, { localFiles = false } = {}) {
|
||||
const paths = Object.keys(spec.paths ?? {});
|
||||
const tools = [];
|
||||
|
||||
@@ -58,7 +63,9 @@ export function buildTools(spec) {
|
||||
if (EXCLUDED_OPERATIONS.has(`${method.toUpperCase()} ${path}`)) continue;
|
||||
if ((op.tags ?? []).some((tag) => EXCLUDED_TAGS.has(tag))) continue;
|
||||
|
||||
tools.push(buildTool(spec, paths, path, method, item, op));
|
||||
const tool = buildTool(spec, paths, path, method, item, op, localFiles);
|
||||
|
||||
if (tool) tools.push(tool);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,7 +86,7 @@ export function buildTools(spec) {
|
||||
return tools;
|
||||
}
|
||||
|
||||
function buildTool(spec, paths, path, method, item, op) {
|
||||
function buildTool(spec, paths, path, method, item, op, localFiles) {
|
||||
const params = mergeParameters(spec, item.parameters, op.parameters)
|
||||
// Headers are credentials here (preview and ingest secrets), which come
|
||||
// from the server's configuration, never from the model.
|
||||
@@ -99,6 +106,32 @@ function buildTool(spec, paths, path, method, item, op) {
|
||||
}
|
||||
|
||||
const bodySchema = op.requestBody?.content?.['application/json']?.schema;
|
||||
const multipartSchema = op.requestBody?.content?.['multipart/form-data']?.schema;
|
||||
|
||||
// A file in a multipart body cannot travel as JSON — as base64 an ordinary
|
||||
// photo costs hundreds of thousands of tokens — so the model names a path
|
||||
// and the server reads it. Only where that path is on the caller's machine.
|
||||
const files = localFiles && multipartSchema
|
||||
? Object.entries(toJsonSchema(spec, multipartSchema).properties ?? {})
|
||||
.filter(([, property]) => property.format === 'binary')
|
||||
.map(([name]) => name)
|
||||
: [];
|
||||
|
||||
// Multipart only, and no way to send a file from here: nothing to offer.
|
||||
if (!bodySchema && multipartSchema && files.length === 0) return null;
|
||||
|
||||
for (const field of files) {
|
||||
const name = `${field}_path`;
|
||||
|
||||
if (name in properties) {
|
||||
throw new Error(`${method.toUpperCase()} ${path} has a parameter named "${name}", which the file upload needs`);
|
||||
}
|
||||
|
||||
properties[name] = {
|
||||
type: 'string',
|
||||
description: `Absolute path to a file on this machine, uploaded as \`${field}\`. Read by the MCP server; use instead of \`body\`.`,
|
||||
};
|
||||
}
|
||||
|
||||
if (bodySchema) {
|
||||
if ('body' in properties) {
|
||||
@@ -114,7 +147,9 @@ function buildTool(spec, paths, path, method, item, op) {
|
||||
|
||||
properties.body = { ...body, description: 'JSON request body.' };
|
||||
|
||||
if (op.requestBody.required) required.push('body');
|
||||
// With a file to send instead, the JSON body is the other way in rather
|
||||
// than a requirement; the API says which one it missed if neither comes.
|
||||
if (op.requestBody.required && files.length === 0) required.push('body');
|
||||
}
|
||||
|
||||
const security = op.security ?? spec.security ?? [];
|
||||
@@ -139,6 +174,7 @@ function buildTool(spec, paths, path, method, item, op) {
|
||||
path,
|
||||
params: params.map(({ name, in: location }) => ({ name, in: location })),
|
||||
hasBody: Boolean(bodySchema),
|
||||
files,
|
||||
requiresAuth: security.length > 0,
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user