From 0deccd5a8317b47bffa26bdbf693c01c95fa91e4 Mon Sep 17 00:00:00 2001 From: Zhengxi Zhou Date: Thu, 22 May 2025 17:19:36 +0800 Subject: [PATCH] feat(server): Add task status retrieval and update dependencies This commit introduces a new tool `get_browser_use_task_result` to retrieve the status and result of a browser task. The version in README.md is updated to v0.0.3, and the mcp dependency in pyproject.toml is upgraded to >=1.9.0. These changes enhance the server's functionality by allowing users to monitor task progress and ensure compatibility with the latest mcp features. --- .../mcp_server_vefaas_browser_use/README.md | 23 +++++- .../pyproject.toml | 2 +- .../mcp_server_vefaas_browser_use/server.py | 70 ++++++++++++++----- 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/server/mcp_server_vefaas_browser_use/README.md b/server/mcp_server_vefaas_browser_use/README.md index ed190a45..a1592c03 100644 --- a/server/mcp_server_vefaas_browser_use/README.md +++ b/server/mcp_server_vefaas_browser_use/README.md @@ -4,7 +4,7 @@ veFaaS Browser-Use MCP server 可以让用户仅输入检索任务,就可以 | | | |------|------| -| 版本 | v0.0.2 | +| 版本 | v0.0.3 | | 描述 | veFaaS Browser-Use MCP server 自动化你的浏览器操作任务 | | 分类 | 容器与中间件 | | 标签 | veFaaS,函数服务,Browser-Use,浏览器 | @@ -33,6 +33,27 @@ veFaaS Browser-Use MCP server 可以让用户仅输入检索任务,就可以 查看今日北京天气 ``` +### Tool 2: get_browser_use_task_status + +#### 类型 + +查询 + +#### 详细描述 + +查询浏览器操作任务的执行状态和结果。 + +输出: + +- 返回任务当前状态(如进行中、已完成、失败等) +- 如已完成,返回任务结果 + +#### 最容易被唤起的 Prompt示例 + +``` +查询刚才浏览器任务的执行状态 +``` + ## 可适配平台 Python, Cursor, Claude macOS App, Cline diff --git a/server/mcp_server_vefaas_browser_use/pyproject.toml b/server/mcp_server_vefaas_browser_use/pyproject.toml index a26158b2..2cc32441 100644 --- a/server/mcp_server_vefaas_browser_use/pyproject.toml +++ b/server/mcp_server_vefaas_browser_use/pyproject.toml @@ -5,7 +5,7 @@ description = "MCP server for automating browser operations" readme = "README.md" requires-python = ">=3.12" dependencies = [ - "mcp==1.3.0", + "mcp>=1.9.0", "requests==2.32.3" ] diff --git a/server/mcp_server_vefaas_browser_use/src/mcp_server_vefaas_browser_use/server.py b/server/mcp_server_vefaas_browser_use/src/mcp_server_vefaas_browser_use/server.py index a4648d8f..a7b05bd4 100644 --- a/server/mcp_server_vefaas_browser_use/src/mcp_server_vefaas_browser_use/server.py +++ b/server/mcp_server_vefaas_browser_use/src/mcp_server_vefaas_browser_use/server.py @@ -11,12 +11,25 @@ mcp = FastMCP("VeFaaS Browser Use") +HEADERS = { + "X-Faas-Event-Type": "http", + "Content-Type": "application/json" + } + @mcp.tool(description="""Creates a browser use task which can automatically browse the web. Use this when you need to create a browser use task with specific messages. The endpoint is read from the environment variable BROWSER_USE_ENDPOINT. -The tool will display progress updates during task execution and return the final result. +After this tool is called, automatically call tool get_browser_use_task_result to get the result. """) -def create_browser_use_task(task: str): +def create_browser_use_task(task: str) -> str: + """ + Args: + task (str): The task description for the browser to execute. For example: "check the weather in beijing." + The task should be a clear instruction of what you want the browser to do. + + Returns: + str: The task ID that can be used to retrieve results later. + """ # check required environment variables and parameters endpoint = os.getenv("BROWSER_USE_ENDPOINT") @@ -40,15 +53,10 @@ def create_browser_use_task(task: str): ] } - headers = { - "X-Faas-Event-Type": "http", - "Content-Type": "application/json" - } - try: # 1. create a new task - response = requests.post(url, headers=headers, json=payload) - + response = requests.post(url, headers=HEADERS, json=payload) + response.raise_for_status() response_json = response.json() if response.content else None @@ -56,18 +64,42 @@ def create_browser_use_task(task: str): task_id = response_json.get("task_id") if response_json else None print(f"Task ID: {task_id}") + + return task_id - # Return early if task creation failed or no task_id was returned - if not task_id: - return { - "status_code": response.status_code, - "response": response_json, - "error": "No task_id returned from task creation" - } + except requests.exceptions.RequestException as e: + error_message = f"Error in browser use task: {str(e)}" + raise ValueError(error_message) + +@mcp.tool(description="""Retrieves the result of a browser use task. +The endpoint is read from the environment variable BROWSER_USE_ENDPOINT. +""") +def get_browser_use_task_result(task_id: str): + """ + Args: + task_id (str): The ID of the browser use task to retrieve results for. + This is the ID returned by create_browser_use_task. + + Returns: + str: The final result from the browser task, including any choices or completion data. + """ + # check required environment variables and parameters + endpoint = os.getenv("BROWSER_USE_ENDPOINT") + + if not endpoint: + raise ValueError("BROWSER_USE_ENDPOINT is not set") + + if not task_id: + raise ValueError("Task ID is required") + + if not endpoint.startswith("http://") and not endpoint.startswith("https://"): + endpoint = f"http://{endpoint}" + + try: - # 2. stream the response + # stream the response url = f"{endpoint}/tasks/{task_id}/stream" - response = requests.get(url, stream=True) + response = requests.get(url, headers=HEADERS, stream=True) response.raise_for_status() @@ -77,12 +109,12 @@ def create_browser_use_task(task: str): if line: if "choices" in line: return line - break except requests.exceptions.RequestException as e: error_message = f"Error in browser use task: {str(e)}" raise ValueError(error_message) + def main(): logger.info("Starting veFaaS browser use MCP Server")