Metadata-Version: 2.4
Name: agentize-bridge
Version: 0.2.1
Summary: 一个轻量级框架，用于通过 HTTP、CLI 和 MCP 暴露现有的 Python 应用程序命令。
Author: ewoaoo
License-Expression: MIT
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.162.0
Requires-Dist: mcp>=2.3.0
Requires-Dist: pydantic>=2.7.4
Requires-Dist: uvicorn>=0.54.0
Dynamic: license-file

<div align="center">

# agentize-bridge

**让运行中的 Python 应用，成为 AI 代理可以调用的工具。**

一个装饰器注册命令，同时获得 HTTP、CLI、MCP 三个入口，并支持在宿主程序的主线程中执行命令。

[![Python](https://img.shields.io/badge/python-3.11%2B-3776AB?logo=python&logoColor=white)](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/getting-started.md#安装)
[![License](https://img.shields.io/badge/license-MIT-2ea44f)](https://github.com/ewoaoo/agentize-bridge/blob/main/LICENSE)

[**文档**](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/README.md) · [快速开始](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/getting-started.md) · [MCP 接入](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/mcp.md) · [安全](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/security.md)

</div>

---

桌面 GUI、DCC 软件等程序的状态只能在主线程中读写，无法直接接入 Web 框架，AI 代理因而无法调用它们。
使用 `agentize-bridge` 时，只需注册命令，参数校验、线程调度与对外暴露由框架负责。

命令也可以用 `BinaryResult` 返回文件或程序生成的二进制数据。HTTP 和 CLI 可以保存原始字节，MCP 也可将 PNG 和 JPEG 作为图像工具结果返回。 [返回二进制数据](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/commands.md#返回二进制数据)

CLI 和 MCP 都通过 HTTP 调用宿主程序里注册的命令。宿主程序可以在线程池中执行命令，也可以在主循环中调用 `run_pending()`，在主线程执行队列里的命令。 [架构图](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/README.md#它是怎么工作的)

## 快速开始

```bash
git clone https://github.com/ewoaoo/agentize-bridge.git
cd agentize-bridge
uv sync            # 或：pip install -e .
```

```python
import time

from agentize_bridge import AgentizeServer, command


@command()
def add(a: int, b: int = 2) -> int:
    """把两个数字相加。"""
    return a + b


if __name__ == "__main__":
    server = AgentizeServer("My App")
    server.start_http(host="127.0.0.1", port=8765)
    try:
        while True:
            server.run_pending()   # 在主线程中执行 thread="main" 的命令
            time.sleep(0.01)
    except KeyboardInterrupt:
        server.close()
```

```bash
curl -X POST http://127.0.0.1:8765/command/add \
  -H 'Content-Type: application/json' -d '{"a": 2, "b": 3}'      # 5

azb call add -a '{"a": 2, "b": 3}'                             # 5
```

控制台默认只显示失败请求的访问记录。设置 `AgentizeServer("My App", log_mode="full")` 可查看成功请求。文件输出配置见[访问日志](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/getting-started.md#访问日志)。

## 文档

[**完整文档 →**](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/README.md) 位于仓库的 `docs/` 目录，下面是常用入口：

| | 说明 |
| --- | --- |
| [定义命令](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/commands.md) | 参数、类型校验与二进制返回值 |
| [线程模型](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/threading.md) | `worker` 与 `main`，接入主循环 |
| [实例方法](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/instance-methods.md) | 暴露已有对象的方法 |
| [HTTP API](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/http-api.md) · [CLI](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/cli.md) · [MCP](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/mcp.md) | 三种调用方式 |
| [安全与限制](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/security.md) | 默认防护与已知缺口 |
| [开发](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/development.md) | 测试、构建与代码结构 |

> [!WARNING]
> **身份验证默认关闭。** 此时，任何能访问端口的人都能调用已注册的命令。本机使用建议保持默认的 `127.0.0.1`。跨机使用时，请开启鉴权并通过 HTTPS 或加密隧道连接。 [安全与限制](https://github.com/ewoaoo/agentize-bridge/blob/main/docs/security.md)

## License

[MIT](https://github.com/ewoaoo/agentize-bridge/blob/main/LICENSE)
