penpot/mcp/packages/server/src/PenpotBackendClient.ts
Dr. Dominik Jain c0f0b14540
🎉 Add direct MCP feedback channel via dedicated tool (#12551)
* ♻️ Move devenv-only tools to a separate folder

* 🎉 Add an MCP feedback channel

Add a user-approved feedback tool and an MCP-token-authenticated backend
endpoint that queues plain-text email. Enable the tool through the
configured backend URI in both local and multi-user mode.

Validate subject and feedback limits, enforce per-profile rate limits,
and document configuration. Cover authentication, queueing, tool
availability, and failure handling with backend and MCP tests.

Closes #12490

AI-assisted-by: gpt-6

* ♻️ Move MCP feedback submission into RPC

Use the standard RPC wrappers for feedback and remove the dedicated
HTTP endpoint, route wiring, and custom rate limiter.

Restrict MCP tokens to opted-in routes and commands. Add a shared RPC
call helper to the MCP backend client and adapt the existing tests.

AI-assisted-by: gpt-6

* 🐛 Harden MCP feedback auth, validation and messages

Accept MCP tokens when either the mcp or the access-tokens flag is on, so
instances that enable MCP without personal access tokens can send
feedback. Personal tokens still need access-tokens. Keep the issuer in one
constant, decode tokens without raising to reject unknown issuers, and
name the issuer and type checks.

Reject control characters in the subject, and in the feedback all but
tabs and line breaks, in both the command and the tool, so a NUL answers
400 instead of failing when the mail job is stored. Answer 403 with
profile-blocked or profile-not-active for unusable profiles, record only
who sent feedback in the audit event, and add doc metadata.

Replace on OAuth approval an MCP token issued before MCP tokens had their
own issuer, or one this instance cannot decrypt, since the backend rejects
both.

In the MCP server, mention disabled access tokens on 401, drop the unused
503 case, and say that delivery happens in the background. Document the
flags in the README.

AI-assisted-by: claude-opus-5-5

* 📚 Document the MCP feedback tool and MCP token rules

List submit_feedback among the MCP tools and add an MCP feedback section to
the configuration guide covering PENPOT_MCP_BACKEND_URI,
PENPOT_MCP_FEEDBACK_EMAIL, the flags and the email settings. Add commented
examples of both variables to the compose file.

In the agent memories, record when OAuth approval replaces an MCP token,
how the MCP server reaches the backend, and which flags, routes and
commands accept MCP tokens.

AI-assisted-by: claude-opus-5-5
2026-10-29 10:35:46 +01:00

87 lines
3.3 KiB
TypeScript

import { createLogger } from "./logger";
/** A plain-text MCP feedback report. */
export interface Feedback {
subject: string;
feedback: string;
}
/** Backend operations available to MCP tools. */
export interface PenpotBackend {
submitFeedback(userToken: string, feedback: Feedback): Promise<void>;
}
/** Client for the configured Penpot instance's backend services. */
export class PenpotBackendClient implements PenpotBackend {
private readonly logger = createLogger("PenpotBackendClient");
private readonly baseUri: URL;
constructor(baseUri: string) {
try {
this.baseUri = new URL(baseUri);
} catch {
throw new Error("PENPOT_MCP_BACKEND_URI must be an absolute HTTP(S) instance URI.");
}
if (
!["http:", "https:"].includes(this.baseUri.protocol) ||
this.baseUri.username ||
this.baseUri.password ||
this.baseUri.search ||
this.baseUri.hash
) {
throw new Error(
"PENPOT_MCP_BACKEND_URI must be an HTTP(S) instance URI without credentials, query or fragment."
);
}
if (!this.baseUri.pathname.endsWith("/")) this.baseUri.pathname += "/";
}
public async submitFeedback(userToken: string, feedback: Feedback): Promise<void> {
await this.callRpc("send-mcp-feedback", userToken, feedback);
}
/** Calls a backend RPC method whose result body is not needed. */
private async callRpc(methodName: string, userToken: string, params: object): Promise<void> {
let response: Response;
try {
response = await fetch(new URL(`api/main/methods/${encodeURIComponent(methodName)}`, this.baseUri), {
method: "POST",
headers: {
Authorization: `Token ${userToken}`,
"X-Client": "penpot-mcp",
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(params),
redirect: "manual",
signal: AbortSignal.timeout(10_000),
});
// discard backend diagnostics, which may contain private data
await response.body?.cancel();
} catch (error) {
this.logger.error(error, "Penpot RPC request failed");
throw new Error("The backend operation could not be confirmed. Do not retry automatically.");
}
if (response.ok) return;
switch (response.status) {
case 401:
throw new Error(
"The backend did not accept the MCP user token. It may be outdated, " +
"or the Penpot instance may not have access tokens enabled."
);
case 403:
throw new Error("The MCP user token is not authorized for this backend operation.");
case 400:
throw new Error("The backend rejected the request parameters or the operation is disabled.");
case 429:
throw new Error("The backend request limit has been reached. Do not retry automatically.");
default:
throw new Error(
`The backend operation could not be confirmed (HTTP ${response.status}). Do not retry automatically.`
);
}
}
}