Track handler responses being sent

This commit is contained in:
Dominik Jain 2025-09-20 19:19:38 +02:00 committed by Dominik Jain
parent bc28e116c8
commit c3ae05c8fc
2 changed files with 15 additions and 0 deletions

View File

@ -2,6 +2,8 @@
* Represents a task received from the MCP server in the Penpot MCP plugin
*/
export class Task<TParams = any> {
public isResponseSent: boolean = false;
/**
* @param requestId Unique identifier for the task request
* @param taskType The type of the task to execute
@ -13,6 +15,11 @@ export class Task<TParams = any> {
* Sends a task response back to the MCP server.
*/
protected sendResponse(success: boolean, data: any = undefined, error: any = undefined): void {
if (this.isResponseSent) {
console.error("Response already sent for task:", this.requestId);
return;
}
const response = {
type: "task-response",
response: {
@ -26,6 +33,7 @@ export class Task<TParams = any> {
// Send to main.ts which will forward to MCP server via WebSocket
penpot.ui.sendMessage(response);
console.log("Sent task response:", response);
this.isResponseSent = true;
}
public sendSuccess(data: any = undefined): void {

View File

@ -52,6 +52,13 @@ function handlePluginTaskRequest(request: { id: string; task: string; params: an
// Cast the params to the expected type and handle the task
console.log("Processing task with handler:", handler);
handler.handle(task);
// check whether a response was sent and send a generic success if not
if (!task.isResponseSent) {
console.warn("Handler did not send a response, sending generic success.");
task.sendSuccess("Task completed without a specific response.");
}
console.log("Task handled successfully:", task);
} catch (error) {
console.error("Error handling task:", error);