Skip to main content
Glama
ascript-cn
by ascript-cn

eval_python

Execute Python code directly on device's main process for instant automation, debugging, and composite decision-making. Returns results immediately.

Instructions

在设备主进程的 Python 上下文中直接 exec 任意代码,立即返回结果(Android + iOS)。 几百毫秒一轮,不需要 upload_file/run_project,适合:

  • 探索性调试:试 selector / 找图 / OCR / 找色 / 一次点击

  • 复合决策:把'看屏幕→判断→点击'打包一段 Python 一次执行

  • 自动裁模板、SoM 标注、智能 tap 路由

  • 任意一次性同步 API 调用:action.click / slide / Selector(任何 mode) / Permission / KeyValue / Sms / Clipboard 等

Android 主进程 client 已本地 stub 化(App.java onCreate 时 bindClient),所有原本走 :py 进程 IPC 的 API 现在 eval 里也能直接调。

⛔ 红线:eval 代码无法外部中断!HTTP 60s 超时只断客户端连接,服务端 Python 仍在跑直到自然返回;卡住 = 整个 App UI 冻住。所有循环必须 range/deadline 限定, sleep ≤ 5s,try/except 整段。超过 30s 的逻辑改用 upload + run_project(可被 stop_project kill)。

⚠ 仍需谨慎的场景:

  • 长循环 / 耗时 > 30s:用 upload+run_project

  • 回调注册(event.on / sensor.on):register 能调用,但 eval 返回时 _result 已定,回调触发的数据拿不回 — 持续监听必须用 upload+run_project

  • 长 session(cloud_control 连云、ESP32 BLE HID 持久会话):建议用工程模式

完整指南见 docs/AGENT_EVAL_GUIDE.md。

代码必须把结果赋给 _result 全局变量。返回值约定:

  • 简单字符串:_result = 'ok'

  • 结构化数据(推荐):_result = json.dumps({'found': True, 'x': 320})

  • 含截图返回:_result = json.dumps({'data': {...}, 'image_base64': '...'}) (MCP 自动识别 image_base64 字段并以图片形式返回给 AI 多模态查看)

image_path 非空时 App 会注入 _im_source 全局变量指向该图片路径; iOS 上若代码中引用 img 变量,会被预读为 cv2 ndarray。

跨平台:iOS 端会自动把 ascript.android. 替换为 ascript.ios., 并预加载 cv2 / np / Image (PIL) 到执行环境,常用片段几乎无需修改。 完整 API 参见 search_api / get_module_apis(按 platform 选择)。

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes要执行的 Python 代码。必须将结果赋给 _result 变量。
image_pathNo可选:传入已有图片路径,App 会注入为 _im_source 全局变量。
Behavior5/5

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

The description discloses all critical behaviors: code execution cannot be interrupted, HTTP 60s timeout only disconnects client but server continues, UI freezes if code hangs, platform-specific modifications (iOS replaces ascript.android), image_path injection, and return value conventions. Since no annotations are provided, the description fully covers the behavioral traits.

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

Conciseness4/5

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

The description is long but well-structured with clear sections (use cases, red lines, caution notes, return conventions, cross-platform). Every sentence adds value, though a bit verbose. It could be slightly more concise but remains effective.

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

Completeness5/5

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

Given the tool's complexity (arbitrary code execution, multiple platforms, safety constraints), the description is exhaustive. It covers security warnings, alternative tools for longer tasks, behavior under timeout, image handling, and platform-specific code replacement. No output schema exists, but return values are fully documented.

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

Parameters5/5

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

Schema coverage is 100% (both code and image_path described), but the description adds significant meaning: code must assign to _result, image_path injects _im_source (and on iOS img is preloaded as ndarray). It also explains the expected return formats (simple string, JSON, image base64) which the schema omits.

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

Purpose5/5

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

The description clearly states that the tool execs arbitrary Python code in the device's main process and returns results immediately. It distinguishes itself from siblings like upload_file and run_project by emphasizing fast execution (hundreds of milliseconds) and listing specific use cases such as exploratory debugging and composite decision-making.

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

Usage Guidelines5/5

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

The description explicitly provides when-to-use scenarios (exploratory debugging, composite decisions, etc.) and when-not-to-use scenarios (long loops >30s, callback registration, long sessions). It also names alternatives: upload+run_project for long tasks and engineering mode for persistent sessions.

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/ascript-cn/ascript-mcp'

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