toggle_power
Turn LIFX smart lights on or off using a selector and duration control. Manage lighting power states through the LIFX MCP Server.
Instructions
Toggle power state of lights
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| token | Yes | LIFX API token | |
| selector | No | Selector for filtering lights (default: 'all') | |
| duration | No | Duration in seconds |
Implementation Reference
- src/index.ts:331-353 (handler)The handler function for the 'toggle_power' tool. It extracts parameters from the request, constructs the request body, calls the LIFX API toggle endpoint via makeLIFXRequest, and returns a success message with the API response.case "toggle_power": { const { token, selector = "all", duration } = args as { token: string; selector?: string; duration?: number; }; const body = duration !== undefined ? { duration } : {}; const result = await makeLIFXRequest(`/lights/${selector}/toggle`, { method: "POST", body, token, }); return { content: [ { type: "text", text: `Power toggled successfully for selector "${selector}". ${JSON.stringify(result, null, 2)}`, }, ], }; }
- src/index.ts:172-184 (registration)The registration of the 'toggle_power' tool in the ListTools response, including its name, description, and input schema for validation.{ name: "toggle_power", description: "Toggle power state of lights", inputSchema: { type: "object", properties: { token: { type: "string", description: "LIFX API token" }, selector: { type: "string", description: "Selector for filtering lights (default: 'all')" }, duration: { type: "number", minimum: 0, description: "Duration in seconds" }, }, required: ["token"], }, },
- src/index.ts:72-114 (helper)Shared helper function used by the toggle_power handler (and others) to make authenticated HTTP requests to the LIFX API.async function makeLIFXRequest( endpoint: string, options: { method?: string; body?: any; token: string; } ): Promise<any> { const { method = "GET", body, token } = options; const url = `${LIFX_API_BASE}${endpoint}`; const headers: Record<string, string> = { "Authorization": `Bearer ${token}`, "User-Agent": USER_AGENT, }; if (body && (method === "POST" || method === "PUT")) { headers["Content-Type"] = "application/json"; } try { const response = await fetch(url, { method, headers, body: body ? JSON.stringify(body) : undefined, }); if (!response.ok) { const errorText = await response.text(); throw new Error(`LIFX API error: ${response.status} ${response.statusText} - ${errorText}`); } // Some endpoints return empty responses const contentType = response.headers.get("content-type"); if (contentType?.includes("application/json")) { return await response.json(); } return await response.text(); } catch (error) { throw new Error(`Failed to make LIFX API request: ${error instanceof Error ? error.message : String(error)}`); } }