import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { sendCommandToFigma } from "../communication";
import { logger } from "../logger";
/**
* Register selection and focus related MCP tools
*/
export function registerSelectionTools(server: McpServer): void {
// Set Focus
server.tool(
"set_focus",
"Focus on a specific node in Figma (scroll and zoom to it)",
{
nodeId: z.string().describe("The ID of the node to focus on"),
},
async ({ nodeId }) => {
try {
const result = await sendCommandToFigma<{ success: boolean }>(
"set_focus",
{ nodeId },
);
return {
content: [
{
type: "text",
text: result.success
? `Focused on node ${nodeId}`
: `Failed to focus on node ${nodeId}`,
},
],
};
} catch (error) {
logger.error("Error setting focus", error);
return {
content: [
{
type: "text",
text: `Error setting focus: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
},
);
// Set Selections
server.tool(
"set_selections",
"Select specific nodes in Figma",
{
nodeIds: z.array(z.string()).describe("Array of node IDs to select"),
},
async ({ nodeIds }) => {
try {
const result = await sendCommandToFigma<{
success: boolean;
selectedCount: number;
}>("set_selections", { nodeIds });
return {
content: [
{
type: "text",
text: `Selected ${result.selectedCount} nodes`,
},
],
};
} catch (error) {
logger.error("Error setting selections", error);
return {
content: [
{
type: "text",
text: `Error setting selections: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
},
);
}