ATH SDK(Python)—— 智能体可信握手客户端
对接 ATH 1.0(智能体可信握手协议,Agent Trust Handshake Protocol,schema 0.1)网关的轻量客户端 SDK。
纯 Python 标准库,零第三方依赖,Python 3.8+,核心就一个文件——不想装包的话,把 ath_sdk/client.py 直接拷进你的项目也能用。
为什么需要它
智能体之间、智能体与服务之间通信前,先「验明身份、讲清权限、再放行」——这就是 ATH 九步可信握手:
发现 → 注册 → 握手互验 → 授权请求 → 三方权限交集确认 → 令牌 → 受保护访问 → 吊销
三方权限交集(Agent 请求 ∩ 用户同意 ∩ 服务策略)保证:客户端要得再多,用户没同意、服务方不允许的权限都拿不到。
安装
pip install ath-sdk-python # 从 PyPI 安装
# 或直接拷贝单文件:
cp ath_sdk/client.py your_project/ath_client.py
十分钟上手
from ath_sdk import ATHConnector
agent = ATHConnector("https://gateway.example.com")
# 1. 注册(首次):用网关运营方发放的注册凭证
agent.register("your-account", "<注册凭证>", scopes=["a2a:discover"])
# 返回 client_id / client_secret / did,请妥善保存(client_secret 仅注册时返回一次)
# 2. 完成握手与授权(每次会话)
agent.discover()
agent.handshake()
agent.request_scopes(["a2a:discover"], user_id="user-xxx")
# 3. 用户确认:确认凭证由网关运营方就本次请求动态签发(邮箱等带外通道获取)
agent.confirm(["a2a:discover"], confirm_credential="<确认凭证>")
# 4. 拿令牌、访问受保护资源
agent.get_token()
agent.protected("/ath/protected/ping")
# 5. 用完即吊销
agent.revoke()
或一步到位:
from ath_sdk import connect
agent = connect("https://gateway.example.com",
client_id="...", client_secret="...", client_did="did:ath:...",
user_id="user-xxx", confirm_credential="<确认凭证>")
agent.protected("/ath/protected/ping")
已注册的凭证也可走环境变量:ATH_CLIENT_ID / ATH_CLIENT_SECRET / ATH_CLIENT_DID。
命令行自检
python -m ath_sdk --gateway https://gateway.example.com --action discover # 只读发现
python -m ath_sdk --gateway https://gateway.example.com --action handshake # 握手互验(需凭证)
API 一览
| 方法 | 对应步骤 | 说明 |
|---|---|---|
discover() |
0 | 读取 /.well-known/ath.json,记录支持的权限 |
register(account, credential, scopes) |
— | 注册身份,取得 client_id / client_secret / DID |
handshake() |
1–4 | 握手发起 + HMAC-SHA256 身份互验 |
request_scopes(scopes, user_id) |
5–6 | 授权请求,返回 request_id |
confirm(scopes, confirm_credential) |
7–8 | 用户确认,返回三方交集后的最终权限 |
get_token() |
9 | 换取访问令牌(自动缓存,TTL 3600s) |
protected(path) |
— | 带令牌访问受保护端点 |
revoke() |
— | 吊销令牌,立即失效 |
ready(...) / connect(...) |
0–9 | 一步到位 |
LangChain 智能体集成(可选)
把已就绪的连接器包成 LangChain 工具,让 AI 智能体在可信握手治理下访问受保护资源:
pip install "ath-sdk-python[langchain]" # 可选增强;不装也不影响主包零依赖
from ath_sdk import connect
from ath_sdk.langchain_tool import make_ath_tools
agent = connect("https://gateway.example.com", client_id=..., client_secret=...)
tools = make_ath_tools(agent) # 默认仅放行 /ath/protected/ 前缀
# 传入 create_react_agent 等;或独立调用:tools[0].invoke({"path": "/ath/protected/ping"})
未安装 langchain-core 时工具以独立模式工作(逻辑完整,只是不能挂进智能体框架);
allowed_prefixes 参数可收窄智能体可访问的端点范围,防止越权。完整示例见 examples/langchain_agent.py。
MCP 服务(可选)
把 ATH 能力以标准 MCP 服务对外提供,一份服务覆盖所有支持 MCP 的客户端与平台 (DSH、华为九问、Dify、Coze、Claude Desktop、Cursor、阿里百炼、百度千帆等):
pip install "ath-sdk-python[mcp]" # 可选增强;不装也不影响主包零依赖
# stdio(桌面客户端 / DSH / 九问本地拉起)
ATH_GATEWAY=https://gateway.example.com \
ATH_CLIENT_ID=... ATH_CLIENT_SECRET=... ATH_CLIENT_DID=... \
python -m ath_sdk.mcp_server
# streamable-http(远程平台:百炼 / 千帆工作流 / Dify 等)
python -m ath_sdk.mcp_server --transport streamable-http --port 9131
四个工具:ath_handshake(九步到令牌一步就绪)、ath_request(带令牌访问受保护端点,
默认仅放行 /ath/protected/ 前缀)、ath_verify(令牌校验)、ath_status(配置与令牌状态,密钥零回显)。
配置走环境变量:ATH_GATEWAY、ATH_CLIENT_ID/SECRET/DID、ATH_USER_ID、
ATH_USER_CONFIRM_SECRET(设置后由服务就每次授权请求现算确认证明)、
ATH_MCP_ALLOWED_PREFIXES(路径白名单,逗号分隔)。
集成矩阵
| 接入面 | 方式 | 状态 |
|---|---|---|
| LangChain / LangGraph | athsdk[langchain] 工具集成 |
✅ 已验证(真实网关端到端) |
| DeepSeek Harness(DSH) | athsdk[mcp] MCP 服务(stdio) |
✅ 已验证(dsh-mcp-client 桥接实测 9/9,见 docs/dsh-integration.md) |
| 九问(openJiuwen / JiuwenSwarm) | athsdk[mcp] MCP 服务(stdio) |
✅ 已验证(openjiuwen StdioClient 实测 8/8,见 docs/jiuwen-integration.md) |
| MCP 客户端与平台(Dify / Coze / Claude Desktop / Cursor / 百炼 / 千帆等) | athsdk[mcp] MCP 服务(stdio + streamable-http) |
✅ 已验证(本地网关端到端 8/8;配置样例见 docs/clients-platforms.md) |
| LlamaIndex / AutoGen / OpenAI Agents SDK / CrewAI | 各框架 MCP 客户端接入本服务 | 按需薄适配 |
安全约定
- 凭证外置:
client_secret只在注册响应中返回一次,SDK 不落盘、不打印;建议用环境变量传入。 - 确认凭证动态签发:每次授权请求由网关运营方就
request_id + user_id现算现发,无法预置或重放——这是三方权限交集设计的一部分。 - 最小权限:只请求业务需要的 scope;越权项会被网关三方交集拦截。
配套工具
- 一致性测试套件:ath-conformance-suite —— 对任意 ATH 1.0 网关做端到端一致性验证(13 项检查,可接 CI)。
- 参考网关实现:agent-community-gateway —— ATH 1.0 网关的产品与评估文档集,含网关参考实现、演示接入指南与实测指标,本 SDK 的对接目标即此协议。
声明
本 SDK 是「对齐信通院 ATH 1.0 规范的实践者」实现的客户端库,非中国信通院官方组件;对接官方评估请以信通院发布为准。
License
Apache-2.0
Metadata
Release files for ath-sdk-python 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ath_sdk_python-0.1.0.tar.gz | 18.0 kB | Without provenance | Details for ath_sdk_python-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ath_sdk_python-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for ath_sdk_python-0.1.0-py3-none-any.whl |
Total release size: 34.1 kB
Release files / ath_sdk_python-0.1.0.tar.gz
| Download URL | ath_sdk_python-0.1.0.tar.gz |
|---|---|
| Size | 18.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
709bd41d69b95d859f84a5a685a3cdb9076dd851936b2ea63cb010f20da0e0bd
|
|
BLAKE2b-256 checksum How to use checksums |
6242a23b15d111f90835439cef9dc9a67c8d4ef7cc8c170067368658d1b2b51a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Release files / ath_sdk_python-0.1.0-py3-none-any.whl
| Download URL | ath_sdk_python-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86306457f199fade6663eb78cda9808ac19b4dee6a6974d62c3fc6cc2d90f7ba
|
|
BLAKE2b-256 checksum How to use checksums |
e44799c2307871a257891e35c3693c1ba22478e428e80b49f86d37e2fced99ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.3
|
Security for ath-sdk-python 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.