penpot/mcp/packages/server/src/dispatch/TaskDispatcher.ts
Dr. Dominik Jain 8efbd9b5d3
🎉 Allow MCP to work with multiple files in parallel (#11927)
* 🎉 Add multi-file session routing to the MCP server

Session routing:
- Track each user's Penpot connections by session ID.
- Dispatch explicit sessions directly; otherwise discover the sole session.
- Return a retryable error when discovery fails or is incomplete.

Plugin and tools:
- Initialize connections with a fresh session ID and file metadata.
- Share the optional sessionId schema and place it last in tool inputs.

Validation:
- 84 tests, server type checking, and MCP formatting checks pass.
- Disable test logging to avoid the logging worker shutdown hang.

AI-assisted-by: gpt-6

* 🎉 Support independent MCP connections

- Require explicit connection intent for each integrated workspace.
- Derive short session IDs from the Penpot app instance and file.
- Show and copy session IDs in the MCP menu and standalone plugin UI.
- Stop stale callbacks and reconnect attempts after disconnect.

Validate with frontend and plugin tests, type checking, live multi-tab
checks, and standalone UI checks with a simulated connection. Server
tests pass with --test-force-exit; the normal runner can hang on shutdown.

AI-assisted-by: gpt-6

* 📚 Document multi-file MCP sessions

- Explain independent connections, session selection, and copying IDs.
- Describe session ID lifetimes for integrated and standalone plugins.
- Correct browser focus, connection, and recovery guidance.

Validate with the documentation site build and diff checks.

AI-assisted-by: gpt-6

* ✨ Change behavior on session ID duplication

* ✨ Open MCP toolbar menu on hover and match menu style

---------

Co-authored-by: alonso.torres <alonso.torres@kaleidos.net>
Co-authored-by: elhombretecla <delacruzgarciajuan@gmail.com>
2026-10-02 14:22:52 +02:00

50 lines
2.3 KiB
TypeScript

import type { PenpotSession } from "@penpot/mcp-common";
import type { PenpotConnection } from "../PenpotConnection";
import type { AbstractPluginTask } from "../PluginTask";
import type { UserPenpotConnections } from "../UserPenpotConnections";
/** Local connection access and tracked task execution provided by PluginBridge. */
export interface TaskDispatchHost {
getUserConnections(userToken: string | null): UserPenpotConnections | undefined;
sendLocalTask(task: AbstractPluginTask, connection: PenpotConnection): void;
sendRemoteTask(task: AbstractPluginTask, userToken: string, sessionId: string): void;
displaceConnection(connection: PenpotConnection): void;
}
/** Session discovery, task routing, and connection subscription lifecycle. */
export abstract class TaskDispatcher {
abstract discoverSessions(userToken: string | null): Promise<PenpotSession[]>;
abstract dispatch(task: AbstractPluginTask, userToken: string | null, sessionId?: string): Promise<void>;
abstract onNewConnection(connection: PenpotConnection): Promise<void>;
abstract onConnectionClosed(connection: PenpotConnection): Promise<void>;
abstract close(): Promise<void>;
/** Selects the sole session from a complete discovery result. */
protected selectOnlySession(sessions: PenpotSession[]): string {
if (sessions.length === 0) {
throw new Error("No Penpot sessions are connected. Please connect the Penpot MCP plugin and retry.");
}
if (sessions.length !== 1) {
throw new Error(
"Multiple Penpot sessions are connected. Ask the user to select one of the active sessions, " +
"then retry with its sessionId:\n" +
sessions
.map(
(session) =>
`${session.sessionId}: ${JSON.stringify(session.fileName)} (file ${session.fileId})`
)
.join("\n")
);
}
return sessions[0].sessionId;
}
}
/** Error returned when discovery cannot establish the complete set of sessions. */
export class SessionDiscoveryError extends Error {
constructor() {
super("Penpot session discovery failed. This may be due to temporary server load. You can retry the request.");
this.name = "SessionDiscoveryError";
}
}