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>
97 lines
3.9 KiB
JavaScript
97 lines
3.9 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* MCP server for the landing API.
|
|
*
|
|
* Reads the OpenAPI document the running application serves and offers one
|
|
* tool per documented operation, so it always describes the API it is talking
|
|
* to — local, staging or production — rather than the one in this checkout.
|
|
*
|
|
* node server.mjs stdio, for a client that launches it locally
|
|
* node server.mjs --http Streamable HTTP on /mcp, for deploying
|
|
*
|
|
* Configuration, all through the environment:
|
|
*
|
|
* LANDING_URL Application root. Default http://localhost:8000
|
|
* LANDING_OPENAPI Path or URL of the spec, if not LANDING_URL/openapi.yaml.
|
|
* LANDING_PREVIEW_TOKEN Lets the public tools see unpublished content. Over
|
|
* HTTP, only for signed-in callers.
|
|
*
|
|
* stdio only — the one person running it is the one it signs in as:
|
|
*
|
|
* LANDING_USERNAME Pireon credentials for the admin API (/v1). The
|
|
* LANDING_PASSWORD token is obtained and refreshed automatically.
|
|
* LANDING_TOKEN Or a fixed bearer token instead.
|
|
*
|
|
* HTTP only — every request brings its own credentials (see src/http.mjs):
|
|
*
|
|
* LANDING_MCP_PORT Default 3000.
|
|
* LANDING_MCP_HOST Default 127.0.0.1; the image sets 0.0.0.0.
|
|
* LANDING_MCP_REQUIRE_AUTH "true" refuses anonymous callers instead of
|
|
* offering them the public API.
|
|
*
|
|
* stdout is the stdio protocol channel: anything for a human goes to stderr.
|
|
*/
|
|
|
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
import { Catalogue } from './src/catalogue.mjs';
|
|
import { LandingClient } from './src/client.mjs';
|
|
import { startHttpServer } from './src/http.mjs';
|
|
import { createMcpServer } from './src/mcp.mjs';
|
|
|
|
const env = (name) => process.env[name]?.trim() || undefined;
|
|
|
|
const baseUrl = (env('LANDING_URL') ?? 'http://localhost:8000').replace(/\/+$/, '');
|
|
const http = process.argv.includes('--http');
|
|
|
|
const catalogue = new Catalogue({
|
|
source: env('LANDING_OPENAPI') ?? `${baseUrl}/openapi.yaml`,
|
|
baseUrl,
|
|
// A deployment outlives many deploys of the application; a stdio process
|
|
// lives for one editor session and describes its tools once anyway.
|
|
ttlMs: http ? 60_000 : Infinity,
|
|
// Over stdio the disk the server reads is the caller's own.
|
|
localFiles: !http,
|
|
});
|
|
|
|
if (http) {
|
|
// Credentials in the environment would sign every anonymous caller in as
|
|
// whoever they belong to.
|
|
for (const name of ['LANDING_USERNAME', 'LANDING_PASSWORD', 'LANDING_TOKEN']) {
|
|
if (env(name)) console.error(`landing-mcp: ${name} is ignored over HTTP; each request brings its own credentials`);
|
|
}
|
|
|
|
// Tried now only to report a misconfiguration early. The application may
|
|
// well still be starting, so a failure here is not fatal: the catalogue
|
|
// retries on the first request.
|
|
catalogue.get().catch((error) => console.error(`landing-mcp: OpenAPI document not available yet: ${error.message}`));
|
|
|
|
startHttpServer({
|
|
catalogue,
|
|
port: Number(env('LANDING_MCP_PORT') ?? 3000),
|
|
host: env('LANDING_MCP_HOST') ?? '127.0.0.1',
|
|
previewToken: env('LANDING_PREVIEW_TOKEN'),
|
|
requireAuth: env('LANDING_MCP_REQUIRE_AUTH') === 'true',
|
|
});
|
|
} else {
|
|
let current;
|
|
|
|
try {
|
|
current = await catalogue.get();
|
|
} catch (error) {
|
|
console.error(`landing-mcp: could not load the OpenAPI document from ${catalogue.source}: ${error.message}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const client = new LandingClient({
|
|
apiBase: current.apiBase,
|
|
token: env('LANDING_TOKEN'),
|
|
username: env('LANDING_USERNAME'),
|
|
password: env('LANDING_PASSWORD'),
|
|
previewToken: env('LANDING_PREVIEW_TOKEN'),
|
|
});
|
|
|
|
await createMcpServer({ spec: current.spec, tools: current.tools, client }).connect(new StdioServerTransport());
|
|
|
|
console.error(`landing-mcp: stdio against ${current.apiBase}${client.canAuthenticate() ? '' : ' (public API only: no credentials)'}`);
|
|
}
|