Skip to main content
Glama
pholex

Qinglong MCP Server

by pholex

run_task

Execute scheduled tasks in Qinglong Panel and retrieve execution logs by providing a task ID, with synchronous operation that waits up to 30 seconds for completion.

Instructions

执行任务并等待完成,返回执行日志(最多等待30秒)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
task_idYes任务 ID

Implementation Reference

  • Handler for the 'run_task' tool: starts the specified task via Qinglong API, polls status every 5s for up to 30s until completion, then returns the execution log. Times out with suggestion to use get_task_logs.
    elif tool_name == "run_task":
        task_id = arguments.get("task_id")
        headers = {"Authorization": f"Bearer {token}"}
        
        try:
            url = f"{QINGLONG_URL}/open/crons/run"
            resp = requests.put(url, headers=headers, json=[task_id], timeout=10)
            result = resp.json()
            if result.get("code") != 200:
                response = {
                    "jsonrpc": "2.0",
                    "id": request["id"],
                    "error": {"code": -32603, "message": f"启动任务失败: {result}"}
                }
                print(json.dumps(response), flush=True)
                continue
        except Exception as e:
            response = {
                "jsonrpc": "2.0",
                "id": request["id"],
                "error": {"code": -32603, "message": f"启动任务失败: {str(e)}"}
            }
            print(json.dumps(response), flush=True)
            continue
        
        time.sleep(2)
        response = None
        task_started = False
        
        for _ in range(6):
            time.sleep(5)
            try:
                status_url = f"{QINGLONG_URL}/open/crons/{task_id}"
                status_resp = requests.get(status_url, headers=headers, timeout=10)
                status_result = status_resp.json()
                
                if status_result.get("code") == 200:
                    cron = status_result["data"]
                    task_status = cron.get("status")
                    
                    # status: 0=运行中, 1=空闲
                    if task_status == 0:
                        task_started = True
                    elif task_status == 1 and task_started:
                        log_url = f"{QINGLONG_URL}/open/crons/{task_id}/log"
                        log_resp = requests.get(log_url, headers=headers, timeout=10)
                        log_result = log_resp.json()
                        
                        if log_result.get("code") == 200:
                            response = {
                                "jsonrpc": "2.0",
                                "id": request["id"],
                                "result": {"content": [{"type": "text", "text": log_result["data"]}]}
                            }
                        else:
                            response = {
                                "jsonrpc": "2.0",
                                "id": request["id"],
                                "error": {"code": -32603, "message": f"获取日志失败: {log_result}"}
                            }
                        break
            except Exception as e:
                response = {
                    "jsonrpc": "2.0",
                    "id": request["id"],
                    "error": {"code": -32603, "message": f"检查任务失败: {str(e)}"}
                }
                break
        
        if response is None:
            response = {
                "jsonrpc": "2.0",
                "id": request["id"],
                "result": {"content": [{"type": "text", "text": f"任务 {task_id} 超时(30秒),请使用 get_task_logs 查看日志"}]}
            }
  • Schema definition for the 'run_task' tool, specifying input as an object with required 'task_id' integer.
    {
        "name": "run_task",
        "description": "执行任务并等待完成,返回执行日志(最多等待30秒)",
        "inputSchema": {
            "type": "object",
            "properties": {
                "task_id": {"type": "integer", "description": "任务 ID"}
            },
            "required": ["task_id"]
        }
    },
  • server.py:77-156 (registration)
    Registration of all tools including 'run_task' in the tools/list MCP method response.
    response = {
        "jsonrpc": "2.0",
        "id": request["id"],
        "result": {
            "tools": [
                {
                    "name": "list_qinglong_tasks",
                    "description": "查询青龙面板中的所有定时任务列表",
                    "inputSchema": {
                        "type": "object",
                        "properties": {}
                    }
                },
                {
                    "name": "run_task",
                    "description": "执行任务并等待完成,返回执行日志(最多等待30秒)",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "task_id": {"type": "integer", "description": "任务 ID"}
                        },
                        "required": ["task_id"]
                    }
                },
                {
                    "name": "run_task_async",
                    "description": "异步启动任务,不等待执行完成",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "task_id": {"type": "integer", "description": "任务 ID"}
                        },
                        "required": ["task_id"]
                    }
                },
                {
                    "name": "get_task_logs",
                    "description": "获取青龙面板中指定任务的执行日志",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "task_id": {"type": "integer", "description": "任务 ID"}
                        },
                        "required": ["task_id"]
                    }
                },
                {
                    "name": "get_task_status",
                    "description": "获取青龙面板中指定任务的执行状态",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "task_id": {"type": "integer", "description": "任务 ID"}
                        },
                        "required": ["task_id"]
                    }
                },
                {
                    "name": "list_subscriptions",
                    "description": "查询青龙面板中的所有订阅列表",
                    "inputSchema": {
                        "type": "object",
                        "properties": {}
                    }
                },
                {
                    "name": "run_subscription",
                    "description": "运行指定的订阅",
                    "inputSchema": {
                        "type": "object",
                        "properties": {
                            "subscription_id": {"type": "integer", "description": "订阅 ID"}
                        },
                        "required": ["subscription_id"]
                    }
                }
            ]
        }
    }
    print(json.dumps(response), flush=True)

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/pholex/qinglong-mcp-server'

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