Skip to content

Commit f600fc4

Browse files
authored
Feat/a2a call oauth (#635)
* feat:支持调用oauth鉴权类型agent * feat:固定oauth访问id-api的version * fix:修复github格式检查错误
1 parent b488b75 commit f600fc4

4 files changed

Lines changed: 369 additions & 18 deletions

File tree

‎docs/content/docs/cli/harness-cli.en.mdx‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ configures the runtime's gateway authorizer.
178178
in `harness.json` (no key). Calling such a runtime requires your own
179179
`Authorization: Bearer <user-pool JWT>` header — the CLI does not mint it.
180180

181+
### Calling OAuth remote agents from A2A Registry
182+
183+
When a harness enables the `agentkit_a2a` registry, the runtime dynamically
184+
discovers agents through `SearchAgentCards` / `GetA2aAgent` and mounts them as
185+
per-turn `remote_a2a_*` tools. If a matched remote AgentCard uses `apiKey`
186+
auth, the runtime injects the declared header from `securitySchemes`. If it
187+
uses `oauth2` with `clientCredentials.tokenUrl`, the runtime parses the
188+
Volcengine Identity UserPool from the token URL, finds its
189+
`MACHINE_TO_MACHINE` client, exchanges `client_credentials` for an access token,
190+
and calls the remote agent's `message/send` / `tasks/get` with
191+
`Authorization: Bearer <access_token>`.
192+
193+
Normally this only requires the harness runtime to have permission to call the
194+
AgentKit A2A Registry and Identity OpenAPI. If the registry endpoint is custom
195+
and cannot also serve Identity OpenAPI requests, set `REGISTRY_ID_ENDPOINT` or
196+
`AGENTKIT_ID_ENDPOINT` to the Identity OpenAPI endpoint.
197+
181198
## `veadk harness invoke`
182199

183200
Invoke a **deployed** harness and print its output. The `url` and `key` are resolved from `harness.json` (written by `deploy`) by `--name`, so you need not pass them explicitly.

‎docs/content/docs/cli/harness-cli.mdx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ auth:
169169
- 用户池、客户端、外部身份提供商(如飞书)在 **Identity 控制台**一次性建好,CLI 只**引用** `discovery_url` + `allowed_ids`,**不涉及任何 secret**。
170170
- custom_jwt 部署后 `harness.json` 记录 `{url, runtime_id, auth_type, discovery_url, allowed_ids}`(无 key)。调用该 runtime 需自带 `Authorization: Bearer <用户池签发的 JWT>`,CLI 不代为获取。
171171

172+
### A2A Registry 调用 OAuth remote-agent
173+
174+
当 Harness 启用 `agentkit_a2a` registry 后,运行时会通过 `SearchAgentCards` / `GetA2aAgent` 动态发现并挂载 `remote_a2a_*` 工具。命中的远程 Agent 如果是 `apiKey` 鉴权,会按 AgentCard 的 `securitySchemes` 注入对应 header;如果是 `oauth2` 且声明了 `clientCredentials.tokenUrl`,运行时会自动从 token URL 解析火山引擎 Identity UserPool,查询其中的 `MACHINE_TO_MACHINE` client,使用 `client_credentials` 换取 access token,并以 `Authorization: Bearer <access_token>` 调用远程 Agent 的 `message/send` / `tasks/get`。
175+
176+
通常只需要确保 Harness Runtime 有权限调用 AgentKit A2A Registry 和 Identity OpenAPI。若 registry endpoint 使用了自定义域名且不能同时访问 Identity OpenAPI,可通过 `REGISTRY_ID_ENDPOINT` 或 `AGENTKIT_ID_ENDPOINT` 指定 Identity OpenAPI 地址。
177+
172178
## `veadk harness invoke`
173179

174180
调用一个**已部署**的 harness 并打印输出。`url` 与 `key` 默认按 `--name` 从 `harness.json`(由 `deploy` 写入)解析,无需显式传入。

‎tests/a2a/test_registry_client.py‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from veadk.a2a.registry_client import (
2323
AgentKitA2ARegistryConfig,
2424
RegistryError,
25+
_OAUTH_TOKEN_CACHE,
2526
_agent_auth_headers,
2627
_volc_sign_v4,
2728
create_task,
@@ -68,6 +69,41 @@ def _agent_card() -> dict:
6869
}
6970

7071

72+
def _oauth_agent_card() -> dict:
73+
token_url = (
74+
"https://userpool-61597ac7-4bcb-4acf-a1d8-fdbfb95333ad."
75+
"userpool.auth.id.cn-beijing.volces.com/oauth/token"
76+
)
77+
return {
78+
"name": "Finance Policy Remote Agent",
79+
"description": "Finance policy agent",
80+
"version": "1.0.0",
81+
"url": " https://oauth-agent.test/a2a/ ",
82+
"security": [{"oauth2": []}],
83+
"securitySchemes": {
84+
"oauth2": {
85+
"type": "oauth2",
86+
"description": "OAuth2 client credentials flow",
87+
"flows": {
88+
"clientCredentials": {
89+
"tokenUrl": f" `{token_url}` ",
90+
"refreshUrl": f" `{token_url}` ",
91+
"scopes": {},
92+
}
93+
},
94+
}
95+
},
96+
"skills": [
97+
{
98+
"id": "finance-policy",
99+
"name": "Finance policy",
100+
"description": "Answer finance policy questions",
101+
"tags": ["finance", "policy"],
102+
}
103+
],
104+
}
105+
106+
71107
@patch.dict(
72108
"os.environ",
73109
{
@@ -190,6 +226,84 @@ def test_create_task_gets_agent_and_sends_message(post: Mock):
190226
assert "Authorization" not in serialized
191227

192228

229+
@patch.dict(
230+
"os.environ",
231+
{
232+
"AGENTKIT_ACCESS_KEY": "ak-test",
233+
"AGENTKIT_SECRET_KEY": "sk-test",
234+
},
235+
clear=False,
236+
)
237+
@patch("veadk.a2a.registry_client.requests.post")
238+
def test_create_task_gets_oauth_agent_token_and_sends_message(post: Mock):
239+
_OAUTH_TOKEN_CACHE.clear()
240+
card = _oauth_agent_card()
241+
post.side_effect = [
242+
_mock_response(
243+
{
244+
"ResponseMetadata": {"RequestId": "get-req"},
245+
"Result": {
246+
"Id": "agent-id",
247+
"Status": "running",
248+
"AgentCard": json.dumps(card),
249+
},
250+
}
251+
),
252+
_mock_response(
253+
{
254+
"ResponseMetadata": {"RequestId": "list-client-req"},
255+
"Result": {"Data": [{"Uid": "m2m-client-id"}]},
256+
}
257+
),
258+
_mock_response(
259+
{
260+
"ResponseMetadata": {"RequestId": "get-client-req"},
261+
"Result": {"ClientSecret": "m2m-client-secret"},
262+
}
263+
),
264+
_mock_response({"access_token": "oauth-access-token", "expires_in": 3600}),
265+
_mock_response(
266+
{
267+
"result": {
268+
"kind": "message",
269+
"parts": [{"kind": "text", "text": "需要财务审批。"}],
270+
}
271+
}
272+
),
273+
]
274+
275+
result = create_task(
276+
"Finance Policy Remote Agent",
277+
"这笔支出是否需要审批?",
278+
config=AgentKitA2ARegistryConfig(
279+
space_id="space-test",
280+
endpoint="https://open.volcengineapi.com/",
281+
),
282+
)
283+
284+
assert result["outcome"] == "success"
285+
assert result["selected_agent"]["name"] == "Finance Policy Remote Agent"
286+
assert result["response"]["text"] == "需要财务审批。"
287+
288+
assert post.call_args_list[0].kwargs["params"]["Action"] == "GetA2aAgent"
289+
assert post.call_args_list[1].kwargs["params"]["Action"] == "ListUserPoolClients"
290+
assert post.call_args_list[1].kwargs["params"]["Version"] == "2025-10-30"
291+
assert post.call_args_list[2].kwargs["params"]["Action"] == "GetUserPoolClient"
292+
assert post.call_args_list[2].kwargs["params"]["Version"] == "2025-10-30"
293+
assert post.call_args_list[3].args[0].endswith("/oauth/token")
294+
assert (
295+
post.call_args_list[3].kwargs["headers"]["Authorization"].startswith("Basic ")
296+
)
297+
assert post.call_args_list[4].args[0] == "https://oauth-agent.test/a2a/"
298+
assert post.call_args_list[4].kwargs["headers"]["Authorization"] == (
299+
"Bearer oauth-access-token"
300+
)
301+
302+
serialized = json.dumps(result, ensure_ascii=False)
303+
assert "oauth-access-token" not in serialized
304+
assert "m2m-client-secret" not in serialized
305+
306+
193307
@patch.dict(
194308
"os.environ",
195309
{

0 commit comments

Comments
 (0)