Skip to main content
Glama
handsomejustin

Xiaomi smart home MCP server

run_action

Execute device actions such as starting a sweep or playing music. Specify device ID and action name to trigger commands on Xiaomi smart home.

Instructions

执行设备动作,例如扫地机开始清扫、播放音乐等。

Args:
    did: 设备ID
    action_name: 动作名称,如 start-sweep、stop-sweeping
    value: 动作参数,可选

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
didYes
action_nameYes
valueNo

Implementation Reference

  • MCP tool handler for 'run_action'. Decorated with @mcp.tool(). Sends a POST request to /devices/{did}/actions/{action_name} with optional value body.
    @mcp.tool()
    async def run_action(did: str, action_name: str, value: dict | None = None) -> dict:
        """执行设备动作,例如扫地机开始清扫、播放音乐等。
    
        Args:
            did: 设备ID
            action_name: 动作名称,如 start-sweep、stop-sweeping
            value: 动作参数,可选
        """
        body = {"value": value} if value is not None else {}
        return await _request("POST", f"/devices/{quote(did)}/actions/{action_name}", json_data=body)
  • DeviceService.run_action - the core service logic that creates a mijiaDevice, calls device.run_action with timeout, emits an update event, and returns status.
    def run_action(user_id: int, did: str, action_name: str, value=None, timeout=MIJIA_CALL_TIMEOUT) -> dict:
        api = api_pool.get_api(user_id)
        device = mijiaDevice(api, did=did)
        kwargs = {"value": value} if value is not None else {}
        _call_with_timeout(device.run_action, action_name, timeout=timeout, **kwargs)
        _emit_device_update(user_id, did, "action_executed", {"action_name": action_name, "status": "executed"})
        return {"did": did, "action_name": action_name, "status": "executed"}
  • Flask REST API handler for POST /devices/<did>/actions/<action_name>. Validates input via RunActionSchema, calls DeviceService.run_action, and returns success/error responses.
    @devices_ns.route("/<did>/actions/<action_name>", methods=["POST"])
    @auth_required
    @limiter.limit("30 per minute")
    def run_action(did, action_name):
        """执行设备动作
        ---
        tags:
          - 设备
        security:
          - cookieAuth: []
          - bearerAuth: []
        parameters:
          - in: path
            name: did
            type: string
            required: true
          - in: path
            name: action_name
            type: string
            required: true
          - in: body
            name: body
            schema:
              type: object
              properties:
                value:
                  description: 动作参数(可选)
        responses:
          200:
            description: 执行成功
          400:
            description: 参数验证失败
        """
        try:
            data = run_action_schema.load(request.get_json(silent=True) or {})
        except Exception as e:
            return error(f"输入验证失败: {e}", 400)
        try:
            result = DeviceService.run_action(get_current_user_id(), did, action_name, data.get("value"))
            return success(result)
        except DeviceActionError as e:
            return _mijia_error_response(e)
        except TimeoutError as e:
            return error(str(e), 504)
        except Exception as e:
            return error(str(e), 500)
  • RunActionSchema - Marshmallow schema defining input validation for run_action: optional 'value' field of any type.
    class RunActionSchema(Schema):
        value = fields.Raw(load_default=None)
  • _call_with_timeout helper function used by DeviceService.run_action to execute mijiaDevice calls with a timeout and retry mechanism.
    def _call_with_timeout(fn, *args, timeout=MIJIA_CALL_TIMEOUT, retries=MIJIA_CALL_RETRIES, **kwargs):
        last_exc = None
        for attempt in range(1 + retries):
            executor = ThreadPoolExecutor(max_workers=1)
            future = executor.submit(fn, *args, **kwargs)
            try:
                return future.result(timeout=timeout)
            except FuturesTimeoutError:
                last_exc = TimeoutError(f"小米云端响应超时 ({timeout}s)")
                executor.shutdown(wait=False, cancel_futures=True)
            except Exception:
                executor.shutdown(wait=False, cancel_futures=True)
                raise
            else:
                executor.shutdown(wait=False, cancel_futures=True)
        raise last_exc
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations provided. Description only implies mutation (execute actions) but does not disclose side effects, permissions needed, error handling, or return behavior. For a mutation tool, more behavioral context is necessary.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Extremely concise and well-structured: one-line purpose followed by parameter list. No wasted words.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

No output schema exists. Description lacks information about return values, success/failure indications, or idempotency. Incomplete for a command execution tool.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description compensates partially. did and action_name are well-described with examples. value is only described as 'optional parameters' with no structure, leaving ambiguity.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states verb 'execute' and resource 'device actions', with examples like start-sweep and playing music. Differentiates from read-oriented siblings like get_property. However, could be more specific about the exact scope of actions.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No explicit guidance on when to use this tool versus alternatives like run_scene. No mention of prerequisites or when not to use it.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/handsomejustin/mijia-control'

If you have feedback or need assistance with the MCP directory API, please join our Discord server