set_pwm
Adjust PWM frequency (0-100) for IoT devices via MCP server, enabling precise control and integration with AI models for natural language commands.
Instructions
设置PWM频率,范围0-100
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| frequency | Yes | PWM频率值(0-100) |
Implementation Reference
- src/mcp2mqtt/server.py:281-289 (handler)Handler logic for the set_pwm MCP tool. Validates the frequency argument (0-100) and constructs the MQTT message payload 'PWM {frequency}' for publishing.if name == "set_pwm": frequency = arguments.get("frequency", 0) if not (0 <= frequency <= 100): return [types.TextContent( type="text", text="Error: Frequency must be between 0 and 100" )] message = f"PWM {frequency}"
- src/mcp2mqtt/server.py:215-242 (registration)Registers the set_pwm tool dynamically from config.tools by providing MCP Tool object with name, description, and inputSchema built from parameters in config.@server.list_tools() async def handle_list_tools() -> List[types.Tool]: """List available tools.""" tools = [] for tool_name, tool_config in config.tools.items(): tools.append( types.Tool( name=tool_config.name, description=tool_config.description, inputSchema={ "type": "object", "properties": { param["name"]: { "type": param["type"], "description": param["description"], **({"enum": param["enum"]} if "enum" in param else {}) } for param in tool_config.parameters }, "required": [ param["name"] for param in tool_config.parameters if param.get("required", False) ] } ) ) return tools
- src/mcp2mqtt/server.py:309-319 (handler)Shared handler logic that sends the prepared message via MQTT to the configured topic and returns the response.mqtt_connection = MQTTConnection(config) response = await mqtt_connection.connect_and_send( topic=tool_config.mqtt_topic, message=message, response_topic=tool_config.response_topic ) return [types.TextContent( type="text", text=response if response else f"{config.mqtt_response_start_string} {message} OK" )]
- src/mcp2mqtt/server.py:263-278 (schema)Validates input arguments against the schema defined in tool_config.parameters (required fields and enum values) before execution.# 检查必需参数 for param in tool_config.parameters: if param.get('required', False) and param['name'] not in arguments: return [types.TextContent( type="text", text=f"Error: Missing required parameter {param['name']}" )] # 验证枚举值 if 'enum' in param and param['name'] in arguments: if arguments[param['name']] not in param['enum']: return [types.TextContent( type="text", text=f"Error: Invalid value for {param['name']}" )]