T5 SMP版本(3.13.1)上使用自定义MCP工具

更新时间:2026-02-06 01:13:43下载pdf

1.添加工具信息与描述

// robot move forward/backward tool
TUYA_CALL_ERR_GOTO(WUKONG_MCP_TOOL_ADD(
"device.robot.move",
"Controls the robot to move forward or backward. "
"This tool can be triggered by natural language commands such as: "
"'往前走' (move forward), '往后退' (move backward), "
"'向前' (forward), '向后' (backward), '前进' (advance), '后退' (go back), "
"'往前' (go forward), '往后' (go backward).\n"
"Parameters:\n"
"- direction (string): The direction to move. "
"Accepts 'forward' or 'backward' (English), or natural language commands like "
"'往前走', '往后退', '向前', '向后', '前进', '后退', '往前', '往后'. "
"The tool will automatically map natural language to 'forward' or 'backward'.\n"
"Response:\n"
"- Returns true if the robot successfully moved in the requested direction.",
swimming_robot_move,
NULL,
MCP_PROP_STR("direction", "The direction to move. Accepts 'forward'/'backward' or natural language like '往前走'/'往后退'/'向前'/'向后'/'前进'/'后退'.")), err)

2.编写回调函数

STATIC OPERATE_RET swimming_robot_move(CONST MCP_PROPERTY_LIST_T *properties, MCP_RETURN_VALUE_T *ret_val, VOID *user_data)
{
    CHAR_T *direction = NULL;
    BOOL_T is_forward = FALSE;
    BOOL_T is_backward = FALSE;
    OPERATE_RET rt = OPRT_OK;

    TAL_PR_DEBUG("swimming robot move start \r\n");

    // Parse properties to get direction
    for (INT_T i = 0; i < properties->count; i++) 
	{
        MCP_PROPERTY_T *prop = properties->properties[i];
		TAL_PR_DEBUG("swimming move[%d]: name = %s,type = %d, description = %s, has_default = %d\r\n",i,prop->name,prop->type,prop->description,prop->has_default);
    }

    return OPRT_OK;
}

3.对开发板说:往前走,然后看回调有没有打印LOG

[12-17 17:39:48 ty D][449][tuya_ai_toy_mcp.c:149] swimming robot move start

[12-17 17:39:48 ty D][449][tuya_ai_toy_mcp.c:155] swimming move[0]: name = direction,type = 2, description = The direction to move. Accepts ‘forward’/‘backward’ or natural language like ‘往前走’/‘往后退’/‘向前’/‘向后’/‘前进’/‘后退’., has_default = 1

4. MCP工具注册丢失,需要本地提前注册工具

//client run
STATIC OPERATE_RET __on_ai_toy_ai_client_run(VOID_T *data)
{
    TAL_PR_NOTICE("ai toy -> connected to server");
    wukong_ai_handle_client(NULL, 0);
    
    // 提前创建Session,让云端提前加载MCP工具列表
    // 这样第一次对话时MCP工具就可以立即使用
    CHAR_T * scode = NULL;
    scode = tuya_ai_agent_get_active_scode();
    if (scode == NULL || scode[0] == '\0') {
        // 如果scode为空,使用默认的scode
        scode = "";  // 默认使用的scode
    }
    
    // 检查session是否已存在
    AI_AGENT_SESSION_T *session = tuya_ai_agent_get_session(scode);
    if (session == NULL) {
        // Session不存在,创建新的session,这样MCP工具列表会发送给云端
        OPERATE_RET ret = tuya_ai_agent_crt_session(scode, 0, 0, NULL, 0);
        if (ret == OPRT_OK) {
            TAL_PR_NOTICE("ai toy -> pre-created session for MCP tools, scode: %s", scode);
        } else {
            TAL_PR_WARN("ai toy -> failed to pre-create session: %d, scode: %s", ret, scode);
        }
    } else {
        TAL_PR_DEBUG("ai toy -> session already exists for scode: %s, sid: %s", scode, session->sid);
    }
    
    return OPRT_OK;
}