arcade-mcp/libs/arcade-core/arcade_core/parse.py
jottakka fe8ddfd500
[TOO-326] Windows papercuts (#768)
<!-- CURSOR_SUMMARY -->
> [!NOTE]
> **Medium Risk**
> Touches authentication/login flow, credentials-file permissions, and
subprocess lifecycle behavior across platforms; while mostly defensive,
regressions could impact login or process management on Windows/macOS
runners.
> 
> **Overview**
> Improves Windows/cross-platform reliability across the CLI and MCP
server: OAuth login now binds the callback server to `127.0.0.1`, avoids
slow loopback reverse-DNS, adds a configurable callback timeout
(`--timeout` + env default), and opens URLs via a Windows-friendly
`_open_browser` to avoid flashing console windows.
> 
> Centralizes CLI output via a shared `console` that forces UTF-8 on
Windows, standardizes UTF-8 file reads/writes throughout, tightens
credentials-file permissions on Windows using `icacls`, and adds shared
Windows subprocess helpers for **no-window** process creation and
graceful termination (used by `deploy`, MCP reload, and usage-tracking
worker).
> 
> Updates client configuration UX/robustness (Windows AppData resolution
via `platformdirs`, Cursor config path fallbacks + compatibility writes,
overwrite warnings, absolute `uv` path for GUI clients, safer path
display) and improves `deploy` child-process handling to avoid
pipe-buffer deadlocks while giving better debug-aware error messages.
> 
> Expands CI to run tests on Linux/Windows/macOS, adds a no-auth CLI
integration workflow, disables usage tracking in toolkits CI, and adds
extensive regression tests for Windows signals, subprocess cleanup,
UTF-8, and config-path edge cases; bumps `arcade-core` to `4.4.2` and
`arcade-mcp-server` to `1.17.2` (with updated dependency pin).
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
0fabd8ca1cd647039ba6ddbdf3f7809c330bab9e. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
2026-02-25 13:18:16 -03:00

74 lines
2.3 KiB
Python

import ast
from pathlib import Path
def load_ast_tree(filepath: str | Path) -> ast.AST:
"""
Load and parse the Abstract Syntax Tree (AST) from a Python file.
"""
try:
with open(filepath, encoding="utf-8") as file:
return ast.parse(file.read(), filename=filepath)
except FileNotFoundError:
raise FileNotFoundError(f"File {filepath} not found")
def get_function_name_if_decorated(
node: ast.FunctionDef | ast.AsyncFunctionDef,
) -> str | None:
"""
Check if a function has a decorator.
"""
decorator_ids = {"arc.tool", "tool"}
for decorator in node.decorator_list:
# if the function is decorated and the decorator is
# either called, or placed on the function
if (
(isinstance(decorator, ast.Name) and decorator.id in decorator_ids)
or (
isinstance(decorator, ast.Attribute)
and isinstance(decorator.value, ast.Name)
and f"{decorator.value.id}.{decorator.attr}" in decorator_ids
)
or (
isinstance(decorator, ast.Call)
and isinstance(decorator.func, ast.Name)
and decorator.func.id in decorator_ids
)
# Support MCPApp tools. e.g., @app.tool or @app.tool(...)
or (
isinstance(decorator, ast.Attribute)
and decorator.attr == "tool"
and isinstance(decorator.value, ast.Name)
)
or (
isinstance(decorator, ast.Call)
and isinstance(decorator.func, ast.Attribute)
and decorator.func.attr == "tool"
and isinstance(decorator.func.value, ast.Name)
)
):
return node.name
return None
def get_tools_from_file(filepath: str | Path) -> list[str]:
"""
Retrieve tools from a Python file.
"""
tree = load_ast_tree(filepath)
return get_tools_from_ast(tree)
def get_tools_from_ast(tree: ast.AST) -> list[str]:
"""
Retrieve tools from Python source code.
"""
tools = []
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
tool_name = get_function_name_if_decorated(node)
if tool_name:
tools.append(tool_name)
return tools