AppleScript API Python封装工具

一个将macOS应用AppleScript接口自动封装为Python类和FastMCP服务器的工具,用于与Claude Code等MCP客户端交互。

  • by aredigg
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 aredigg/MCP-server-AppleScript-wrapper

使用说明

用于Python的AppleScript API到MCP服务器包装器

引言

这是一个简单的Python程序,可以扫描macOS应用程序的暴露接口,并创建一个在Python中使用的包装器,以及一个创建为FastMCP服务器的包装器。 它将尝试查找应用程序,如果找到,它将创建三个文件、两个Python类和一个JSON文件,其中包含它收集到的相同信息。 它有望“开箱即用”,但由于MCP的描述可能太短,因此额外的信息将有助于LLM选择正确的工具。

需求

  • macOS(使用 osascript 和 sdef)
  • Python 3.10+
  • FastMCP(pip install fastmcp)--只需要运行MCP服务器

它使用了Python 3.10+中的新类型功能,因此如果你想尝试适应早期版本,如果你想删除它们。

安装

git clone https://github.com/aredigg/MCP-server-AppleScript-wrapper.git mcp-osacreate
cd mcp-osacreate
chmod +x bin/mcp-osacreate

python文件位于 bin 文件夹,可以保留在本地运行,也可以复制到系统路径,如 /usr/local/bin 像这样(需要管理员访问权限)

sudo cp bin/mcp-osacreate /usr/local/bin/

可选,仅适用于MCP服务器

pip3 install fastmcp

用法

> mcp_osacreate <ApplicationName>

macOS将请求访问以控制有问题的应用程序,因为它使用AppleScript来询问应用程序。这是一个一次性问题,如果您以后决定使用,也将适用于MCP。

示例

mcp_osacreate Finder

这将创建三个文件:

文件 描述
finder.py Python类直接使用
finder_mcp.py FastMCP服务器
finder.json JSON API文档

使用JSON文件

JSON文件包含完整的API文档,可用于参考、构建自定义工具、向LLM提供上下文或睡前阅读

更好的LLM集成技巧

增强工具描述

生成的MCP服务器使用应用程序SDEF字典中的描述,这些描述可能很稀疏。在生成的文档中编辑文档字符串 *_mcp.py 文件以添加更多上下文:

# Before (auto-generated)
@mcp.tool()
def finder_open(direct: str) -> str:
    """Open an object."""
    ...

# After
@mcp.tool()
def finder_open(direct: str) -> str:
    """
    Open a file, folder, or application in Finder.

    Args:
        direct: Path to the item to open. Can be:
            - A file path: "/Users/me/document.pdf"
            - A folder path: "/Users/me/Documents"
            - An application: "/Applications/Safari.app"

    Returns:
        Empty string on success

    Examples:
        finder_open("/Users/me/Documents")
        finder_open("/Applications/Calculator.app")
    """
    ...

[!警告] 这可能是一个强大的工具,AppleScript可能会暴露出可能会执行不必要或破坏性操作的接口,所以在应用之前一定要谨慎。我建议删除所有你不需要或认为有风险的工具或方法。

许可证

此项目根据Mozilla公共许可证2.0获得许可-请参阅 许可证 文件以获取详细信息。