Metadata-Version: 2.5
Name: alibabacloud-agentcore-sdk
Version: 0.1.2
Summary: Python runtime SDK for Alibaba Cloud AgentCore high-code agents
License: 
                                         Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: alibabacloud-agentcore20260804==1.2.1
Requires-Dist: anthropic<2,>=1
Requires-Dist: anyio<5,>=4.4
Requires-Dist: httpx<1,>=0.27
Requires-Dist: litellm<2,>=1.95.0; python_version >= '3.11'
Requires-Dist: litellm==1.95.0; python_version < '3.11'
Requires-Dist: openai<3,>=2
Requires-Dist: pyyaml<7,>=6.0
Provides-Extra: agentscope
Requires-Dist: agentscope<3,>=2; (python_version >= '3.11') and extra == 'agentscope'
Provides-Extra: collaboration
Requires-Dist: alibabacloud-agentcore-collaboration<1,>=0.1.0; extra == 'collaboration'
Provides-Extra: credentials
Requires-Dist: alibabacloud-agentidentitydata20251127<3,>=2.0; extra == 'credentials'
Requires-Dist: alibabacloud-tea-openapi>=0.3; extra == 'credentials'
Provides-Extra: crewai
Requires-Dist: crewai>=1.15.20; extra == 'crewai'
Requires-Dist: onnxruntime==1.23.2; (python_version == '3.10') and extra == 'crewai'
Provides-Extra: dev
Requires-Dist: jsonschema<5,>=4.23; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
Requires-Dist: pytest>=8.3; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: types-pyyaml>=6.0; extra == 'dev'
Provides-Extra: google-adk
Requires-Dist: google-adk[extensions]>=1.18; extra == 'google-adk'
Provides-Extra: langchain
Requires-Dist: langchain-anthropic>=1.0; extra == 'langchain'
Requires-Dist: langchain-core>=1.0; extra == 'langchain'
Requires-Dist: langchain-openai>=1.0; extra == 'langchain'
Requires-Dist: langchain<2,>=1; extra == 'langchain'
Provides-Extra: langgraph
Requires-Dist: langchain-anthropic>=1.0; extra == 'langgraph'
Requires-Dist: langchain-openai>=1.0; extra == 'langgraph'
Requires-Dist: langgraph>=1.0; extra == 'langgraph'
Provides-Extra: mcp
Requires-Dist: mcp<2,>=1.21.2; extra == 'mcp'
Provides-Extra: pydantic-ai
Requires-Dist: pydantic-ai>=1.22; extra == 'pydantic-ai'
Provides-Extra: server
Requires-Dist: ag-ui-protocol<1,>=0.1.10; extra == 'server'
Requires-Dist: fastapi<1,>=0.104; extra == 'server'
Requires-Dist: uvicorn>=0.24; extra == 'server'
Description-Content-Type: text/markdown

# AgentCore SDK for Python

使用 Python 构建 Agent，连接 AgentCore 云端资源或自有服务，并与常用 Agent 框架集成。

[AgentCore 官网](https://www.aliyun.com/product/agentcore) · [官方文档](https://help.aliyun.com/zh/agentcore/) · [示例指南](examples/README.md)

## 从这里开始

| 你想做什么 | 入口 |
| --- | --- |
| 连接平台上的模型、MCP 和 Skill | [云端资源](#使用云端资源) |
| 使用自己的模型、MCP 或本地 Skill | [自定义资源](#使用自定义资源) |
| 为 Agent 添加长期记忆 | [Memory](#使用-memory) |
| 配置 MCP 凭证与固定 Header | [凭证与 Header](#凭证与-mcp-header) |
| 接入已有 Agent 框架 | [框架集成](#框架集成) |
| 将 Agent 发布为 HTTP 服务 | [服务协议](#提供-agent-服务) |

## 功能

- **模型**：访问托管模型，或连接自定义模型服务。
- **工具与 Skill**：接入 MCP 服务，使用云端或随应用分发的 Skill。
- **记忆**：检索和保存长期记忆，接入框架的 Agent 执行流程。
- **凭证**：按名称获取托管 API Key，或为 MCP 显式绑定 Header 凭证。
- **服务协议**：通过 AG-UI 或 OpenAI Chat Completions 提供 Agent 服务。

支持 LangChain、LangGraph、AgentScope、Google ADK、PydanticAI 和 CrewAI。

## 安装

需要 Python 3.10+；AgentScope 2.x 需要 Python 3.11+。

> 当前分支处于公开发布准备阶段。以下为公开发行版的安装方式，包的可用版本以正式发布为准。

```bash
pip install alibabacloud-agentcore-sdk

# 按需安装 MCP、服务端和框架支持
pip install "alibabacloud-agentcore-sdk[mcp,server,langchain]"
```

按使用场景选择 extras：`mcp`、`server`、`credentials`、`langchain`、`langgraph`、`agentscope`、`google-adk`、`pydantic-ai`、`crewai`。

## 快速开始

### 使用云端资源

先参考[官方文档](https://help.aliyun.com/zh/agentcore/)创建 Workspace，再按需准备模型连接、MCP 和 Skill，并为 Agent 授予访问权限。以下代码在部署到 AgentCore 的应用中运行；将资源名称替换为当前 Workspace 中自己的资源名称。只使用模型时，无需创建 MCP 或 Skill。

```python
import asyncio

from agentcore import AsyncAgentCore


async def main():
    async with AsyncAgentCore.auto() as core:
        model = await core.model("test-mc", model="qwen3.8-max")
        response = await model.invoke([{"role": "user", "content": "你好"}])
        print(response["choices"][0]["message"]["content"])

        mcp = await core.mcp("test-mcp")
        print([tool.name for tool in await mcp.list_tools()])

        skill = await core.skills.managed("test-skill")
        print(skill.name)


asyncio.run(main())
```

### 凭证与 MCP Header

在平台创建凭证并授权后，使用 `await core.credentials.get(name)` 按名称获取。API Key 凭证通过 `credential.value` 读取；MCP Header 凭证通过 `credential.as_headers()` 读取。不要将凭证值写入日志或提交到代码仓库。

托管 MCP 可在创建时显式绑定 MCP Header 凭证，并追加固定 Header。只有该 MCP 在凭证允许的应用范围内时才能使用：

```python
mcp = await core.mcp(
    "test-mcp",
    credential_name="my-mcp-credential",  # 可省略；须在平台创建并允许访问该 MCP
    headers={"x-business-id": "my-app"},  # 可省略
)
```

Header 作用于该客户端的握手、工具发现和工具调用，不是请求级身份上下文。
SDK 会复制配置，不同 Header 配置使用不同客户端和 Session；不传时保持原有行为。
合并顺序为平台 Header → 凭证 Header → 自定义 Header；名称大小写不敏感，冲突直接报错，不做覆盖。
`Authorization`、平台已配置的 Header，以及 `Host`、`Mcp-Session-Id`、`Mcp-Protocol-Version` 等协议维护字段不可覆盖。
凭证在首次建连和重连时查询与获取，复用 Session 期间不重复获取，也不后台刷新。
托管 MCP 使用 Streamable HTTP；直连 MCP 保留 `headers_provider`，stdio 不承载 HTTP Header。
不要修改共享客户端来切换用户身份。Header 的内容由业务明确选择，不自动透传入站请求头。
固定 Header 不等于可信身份。完整示例见 [MCP 凭证与 Header](examples/mcp_credentials.py)。

### 使用自定义资源

连接自有模型、MCP 服务，无需在 AgentCore 注册资源：

```python
import asyncio
import os

from agentcore import AsyncAgentCore


async def main():
    async with AsyncAgentCore() as core:
        model = core.direct_model(
            provider="openai",
            model=os.environ["CUSTOM_MODEL_NAME"],
            base_url=os.environ["CUSTOM_MODEL_BASE_URL"],
            api_key=os.environ["CUSTOM_MODEL_API_KEY"],
        )
        response = await model.invoke([{"role": "user", "content": "你好"}])
        print(response["choices"][0]["message"]["content"])

        mcp = core.direct_mcp(url=os.environ["CUSTOM_MCP_URL"])
        print([tool.name for tool in await mcp.list_tools()])


asyncio.run(main())
```

本地 Skill 加载方式及完整配置见 [自定义资源示例](examples/direct_resources.py)。

## 使用 Memory

Memory 用于保存和检索长期记忆，也支持查询、更新、删除记忆以及查看会话消息。使用前，在 AgentCore 中创建 MemoryStore 并授予应用访问权限。

```python
import asyncio

from agentcore import AsyncAgentCore
from agentcore.memory import MemoryScope


async def main():
    async with AsyncAgentCore.auto() as core:
        store = core.memory_store("test-memory")
        await store.add_memories(
            scope=MemoryScope(user_id="example-user", session_id="session-1"),
            text="用户喜欢简短的中文回答。",
        )

        # Newly added memories may take time to become searchable.
        result = await store.search_memories(
            "用户有哪些回答偏好？",
            scope=MemoryScope(user_id="example-user"),
            top_k=5,
        )
        for hit in result.memories:
            print(hit.memory.content.text)


if __name__ == "__main__":
    asyncio.run(main())
```

- `user_id`、`agent_id`、`session_id` 是彼此独立的记忆范围字段；写入时都可省略，服务端会将未传字段归入默认范围。
- 查询时只传需要精确匹配的字段，未传字段按通配范围查询；响应中的默认范围字段会表现为 `None`。
- `list_memory_session_messages` 要求 `session_id`，并且 `user_id`、`agent_id` 至少传一个。
- 分区标识由可信业务上下文提供，不替代访问权限控制。

示例会写入真实数据，建议使用测试记忆空间。新写入的记忆可能需要一定时间才能检索到，首次查询可能为空。

完整代码见 [Memory 基础示例](examples/memory.py)；让 Agent 自动检索并使用记忆，参见 [六框架 Memory 示例](examples/memory_frameworks/README.md)。

## 框架集成

将模型、MCP 和 Skill 接入现有框架，不必重写 Agent 的业务逻辑。

| 框架 | 示例 |
| --- | --- |
| LangChain | [Agent](examples/frameworks/langchain_agent.py) |
| LangGraph | [模型与工具循环](examples/frameworks/langgraph_agent.py) |
| AgentScope 2.x | [Agent](examples/frameworks/agentscope_agent.py) |
| Google ADK | [Agent 与 Runner](examples/frameworks/google_adk_agent.py) |
| PydanticAI | [Agent](examples/frameworks/pydantic_ai_agent.py) |
| CrewAI | [Agent、Task 与 Crew](examples/frameworks/crewai_agent.py) |

需要长期记忆时，参见 [Memory 框架示例](examples/memory_frameworks/README.md)。

需要展示工具执行过程时，使用 [框架执行事件转换器](examples/frameworks/execution-events.md)，保留工具调用、结果与消息边界。

## 提供 Agent 服务

`AgentCoreServer` 支持 AG-UI、OpenAI Chat Completions 和自定义 `ProtocolHandler`。

```bash
uvicorn examples.basic_agent:server --host 0.0.0.0 --port 8080
```

默认接口为 `POST /ag-ui/agent` 和 `POST /openai/v1/chat/completions`，支持流式响应。参见 [最小服务示例](examples/basic_agent.py)。

需要完整工具执行流程时，运行 [LangChain 服务示例](examples/langchain_server.py)：它将托管模型、MCP、Skill、框架事件转换器和两个协议入口串起来。此示例使用 Python 3.11+；安装 `alibabacloud-agentcore-sdk[mcp,server,langchain]` 后，在云端应用中执行：

```bash
uvicorn examples.langchain_server:server --host 0.0.0.0 --port 8080 --log-level info
```

示例使用 OpenAI/v1 模型连接，每次处理最新一条用户文本，不保存会话历史。AG-UI/OpenAI 请求及预期输出见 [调用 Agent 服务](examples/README.md#调用-agent-服务)。

框架执行流应使用对应的[事件转换器](examples/frameworks/execution-events.md)，不要只提取文本。AG-UI 可表达消息边界、工具调用和工具结果；OpenAI Chat Completions 按其标准表达文本和工具调用，不提供独立的工具结果流式事件。会话历史的保存与恢复仍由应用或所用框架负责。

## 使用提示

- 在应用生命周期内复用 Core；异步应用使用 `async with`，同步应用可使用 `with AgentCore.auto()`。
- 普通同步调用和异步文本流见 [模型调用示例](examples/model_calls.py)；Responses 用法见 [示例指南](examples/README.md#模型调用方式)。
- 模型的工具调用、Responses 和 Embedding 支持情况取决于所选模型。
- 仅加载可信 Skill；不需要执行命令时设置 `ALLOW_EXECUTE_COMMAND=false`。
- 使用 `logging.basicConfig(level=logging.INFO)` 开启日志，不要记录 API Key 等敏感信息。
- 排查模型、凭证或协作请求失败时，先查对应的 `*.failed` 日志中的地址、状态码、错误码和上游 RequestId，再结合脱敏后的错误说明定位。服务端未提供的字段显示为 `-`；日志不输出完整响应。`teams.update_ignored` 表示更新失败后沿用旧配置，`reason` 给出原因。
- Memory 失败日志包含操作名、记忆空间、状态码、服务端错误码、RequestId 和经过脱敏、截断的错误说明；服务端未提供的字段显示为 `-`。框架自动记忆降级时也会记录这些诊断信息。`MemoryAPIError` 同时通过异常文本和属性保留诊断字段，不输出请求正文或完整响应。

更多资源配置、框架依赖与运行命令见 [示例指南](examples/README.md)。
