AS AgentScopeAPI 手册 / API Reference

核心精讲Core deep dives

24 个核心类型的中文解读、设计要点与可独立运行的示例。每个条目也会以相同锚点呈现在对应的 API 页面中。Twenty-four core types with Chinese explanations, design notes and standalone examples. Every entry is also rendered on its API page under the same anchor.

PythonPython

Agentagentscope.agent
Agent:状态化的 ReAct 运行时Agent: the stateful ReAct runtime

Agent 是 AgentScope 2.x 的核心智能体实现,内部完成推理—行动循环、工具调用、上下文压缩与事件流。它把模型、工具、记忆和中间件组装成一个可持久化的会话执行器。

Agent is the core agent implementation in AgentScope 2.x. It owns the reasoning-acting loop, tool execution, context compression and event stream, assembling a model, tools, memory and middleware into a persistable session runtime.

ReActConfigagentscope.agent
ReActConfig:循环策略ReActConfig: loop policy

ReActConfig 描述一次回复内推理—行动循环的策略:最多迭代多少次、结构化输出失败后给多少宽限、被拒绝或中断时如何结束。

ReActConfig describes the policy of the reasoning-acting loop: how many iterations to allow, how much grace to give structured output, and how to end when a tool call is rejected or the reply is interrupted.

Msgagentscope.message
Msg:统一消息模型Msg: the unified message model

Msg 是智能体之间传递信息的统一载体。内容由 TextBlock、ThinkingBlock、ToolCallBlock、ToolResultBlock 等内容块组成,既表达纯文本,也表达多模态与工具调用。

Msg is the unified carrier exchanged between agents. Its content is a list of blocks such as TextBlock, ThinkingBlock, ToolCallBlock and ToolResultBlock, covering plain text, multimodal data and tool interactions.

Toolkitagentscope.tool
Toolkit:工具注册与执行Toolkit: registration and execution

Toolkit 是工具、技能与 MCP 客户端的统一注册表。它把 Python 函数或 MCP 工具转换成模型可理解的 schema,并在调用时完成权限检查、并发调度与结果封装。

Toolkit is the unified registry for tools, skills and MCP clients. It turns Python functions or MCP tools into model-readable schemas and handles permission checks, concurrency and result wrapping at call time.

ChatModelBaseagentscope.model
ChatModelBase:模型抽象ChatModelBase: the model abstraction

ChatModelBase 是所有聊天模型的抽象基类,统一流式与非流式调用、token 计数、重试和结构化输出。上层代码只依赖这个基类即可切换供应商。

ChatModelBase is the abstraction shared by all chat models. It unifies streaming and non-streaming calls, token counting, retries and structured output so upper layers can switch providers without code changes.

OpenAIChatModelagentscope.model
OpenAIChatModel:OpenAI 兼容模型OpenAIChatModel: OpenAI-compatible models

OpenAIChatModel 通过 OpenAI Chat Completions 协议连接 OpenAI 及兼容端点,支持流式输出、工具调用、结构化输出与自定义 formatter。

OpenAIChatModel speaks the OpenAI Chat Completions protocol to OpenAI and compatible endpoints, with streaming, tool calls, structured output and a customizable formatter.

FormatterBaseagentscope.formatter
FormatterBase:消息与协议转换FormatterBase: message and protocol conversion

FormatterBase 负责把 Msg 列表转换成供应商请求,并把供应商响应解析回统一的 ChatResponse。它隔离了不同模型在工具、多模态和角色编码上的差异。

FormatterBase converts a list of Msg objects into provider requests and parses provider responses back into ChatResponse. It isolates differences in tool, multimodal and role encoding across models.

AgentStateagentscope.state
AgentState:可持久化的会话状态AgentState: persistable session state

AgentState 保存一次会话的全部可变状态:消息上下文、回复上下文、权限、工具与任务上下文。它让智能体可以在进程重启后恢复,也支持同一会话的并发控制。

AgentState stores all mutable state of a session: message context, reply context, permissions, tool and task context. It lets agents resume after a restart and supports concurrency control for the same session.

MiddlewareBaseagentscope.middleware
MiddlewareBase:无侵入钩子MiddlewareBase: non-invasive hooks

MiddlewareBase 提供回复、推理、行动、模型调用、权限检查、上下文压缩和系统提示词等钩子。中间件按“环绕”方式包装下一段逻辑,可在不修改智能体源码的情况下加入日志、审计、缓存或策略。

MiddlewareBase exposes hooks for reply, reasoning, acting, model calls, permission checks, context compression and system prompts. Middleware wraps the next handler, adding logging, auditing, caching or policy without modifying agent source.

KnowledgeBaseagentscope.rag
KnowledgeBase:文档检索KnowledgeBase: document retrieval

KnowledgeBase 把嵌入模型、向量库和集合名绑定在一起,提供文档写入、向量检索、文档与分块管理。它既可以作为工具暴露给模型,也可以由 RAGMiddleware 自动注入检索结果。

KnowledgeBase binds an embedding model, a vector store and a collection name, providing document ingestion, vector search and document/chunk management. It can be exposed as a tool or used by RAGMiddleware to inject retrieved context.

MCPClientagentscope.mcp
MCPClient:接入 MCP 服务MCPClient: connect MCP servers

MCPClient 管理一个 MCP 服务连接,支持 STDIO 与 HTTP 传输。它把远端工具发现为 ToolBase,可一次性注册进 Toolkit,并负责生命周期与超时控制。

MCPClient manages a connection to an MCP server over STDIO or HTTP. It discovers remote tools as ToolBase objects, registers them into a Toolkit in one step, and owns lifecycle and timeout handling.

FunctionToolagentscope.tool
FunctionTool:把函数变成工具FunctionTool: turn functions into tools

FunctionTool 从普通 Python 函数的签名、类型注解和 docstring 生成工具 schema,并把调用结果封装成 ToolResponse。它是最常用的自定义工具方式。

FunctionTool derives a tool schema from a Python function's signature, type hints and docstring, and wraps results into ToolResponse. It is the most common way to add custom tools.

JavaJava

ReActAgentio.agentscope.core
ReActAgent:响应式 ReAct 智能体ReActAgent: the reactive ReAct agent

ReActAgent 是 Java 版的核心智能体。Builder 组装模型、工具、权限、状态存储与中间件,call 返回 Mono,streamEvents 返回 AgentEvent 流。

ReActAgent is the core agent in Java. Its builder assembles the model, tools, permissions, state store and middleware; call returns Mono and streamEvents returns a stream of AgentEvent.

AgentBaseio.agentscope.core.agent
AgentBase:智能体生命周期AgentBase: the agent lifecycle

AgentBase 实现所有智能体共享的生命周期:运行互斥、拦截器、状态恢复、结构化输出分派与协作式中断。自定义智能体通常继承它并实现 doCall。

AgentBase implements the lifecycle shared by all agents: run mutual exclusion, hooks, state restoration, structured output dispatch and cooperative interruption. Custom agents typically extend it and implement doCall.

RuntimeContextio.agentscope.core.agent
RuntimeContext:单次调用上下文RuntimeContext: per-call context

RuntimeContext 承载单次调用所需的会话/用户标识、AgentState、工具执行配置与自定义属性。它不持久化,跨调用状态应放在 AgentState 中。

RuntimeContext carries per-call session/user identity, AgentState, tool execution configuration and custom attributes. It is not persisted; cross-call state belongs in AgentState.

Msgio.agentscope.core.message
Msg:Java 消息模型Msg: the Java message model

Msg 是 Java 版的统一消息对象,由 MsgRole 与 ContentBlock 列表组成。Builder 负责构造,getTextContent 与 getStructuredData 提供常用读取方式。

Msg is the unified message object in Java, composed of a MsgRole and a list of ContentBlock. The builder constructs messages, while getTextContent and getStructuredData provide common accessors.

Toolkitio.agentscope.core.tool
Toolkit:工具注册表Toolkit: the tool registry

Toolkit 扫描 @Tool 注解方法,把 Java 方法转换成模型工具 schema,并负责工具查找、分组、MCP 注册与权限元数据。

Toolkit scans @Tool-annotated methods, converts Java methods into model tool schemas, and handles lookup, grouping, MCP registration and permission metadata.

ChatModelBaseio.agentscope.core.model
ChatModelBase:响应式模型抽象ChatModelBase: the reactive model abstraction

ChatModelBase 定义 Java 模型的统一契约:stream 返回 Flux,子类实现 doStream。它同时暴露上下文窗口、原生结构化输出等能力位。

ChatModelBase defines the Java model contract: stream returns Flux and subclasses implement doStream. It also exposes capability flags such as context window and native structured output.

OpenAIChatModelio.agentscope.extensions.model.openai
OpenAIChatModel:OpenAI 协议实现OpenAIChatModel: the OpenAI protocol implementation

OpenAIChatModel 位于官方 openai 扩展中,通过 Builder 配置 API Key、模型名、端点、formatter 与上下文窗口,实现 ChatModelBase 的流式协议。

OpenAIChatModel lives in the official openai extension. Its builder configures API key, model name, endpoint, formatter and context window while implementing the ChatModelBase streaming protocol.

Formatterio.agentscope.core.formatter
Formatter:请求与响应转换Formatter: request and response conversion

Formatter 把 Msg 列表与 GenerateOptions 转换为供应商请求,并把响应解析为 ChatResponse。applyTools 与 applyOptions 负责工具和采样参数的协议映射。

Formatter converts Msg lists and GenerateOptions into provider requests and parses responses into ChatResponse. applyTools and applyOptions map tools and sampling parameters onto the provider protocol.

AgentStateio.agentscope.core.state
AgentState:会话状态AgentState: session state

AgentState 保存 Java 智能体的上下文、摘要、回复进度、权限、工具与任务状态。Builder 负责构造,AgentStateStore 负责跨进程持久化。

AgentState stores the Java agent's context, summary, reply progress, permissions, tool and task state. The builder constructs it, and AgentStateStore persists it across processes.

MiddlewareBaseio.agentscope.core.middleware
MiddlewareBase:响应式钩子MiddlewareBase: reactive hooks

MiddlewareBase 以 Flux 链的方式包装智能体执行,提供 onAgent、onReasoning、onActing、onModelCall 与 onSystemPrompt 等钩子。order 控制多个中间件的嵌套顺序。

MiddlewareBase wraps agent execution as a Flux chain, exposing hooks such as onAgent, onReasoning, onActing, onModelCall and onSystemPrompt. order controls how multiple middlewares nest.

SimpleKnowledgeio.agentscope.core.rag.knowledge
SimpleKnowledge:内存知识库SimpleKnowledge: an in-memory knowledge base

SimpleKnowledge 是 Knowledge 接口的开箱即用实现,组合 EmbeddingModel 与向量库,提供文档写入与相似度检索。它可以直接传给 ReActAgent.builder().knowledge(...)。

SimpleKnowledge is a ready-made implementation of the Knowledge interface, combining an EmbeddingModel with a vector store to provide document ingestion and similarity search. It can be passed to ReActAgent.builder().knowledge(...).

McpClientBuilderio.agentscope.core.tool.mcp
McpClientBuilder:接入 MCPMcpClientBuilder: connect MCP

McpClientBuilder 构造 MCP 客户端,支持 STDIO、SSE 与 Streamable HTTP 传输,并可统一设置请求头、查询参数与超时。构造出的客户端可直接注册进 Toolkit。

McpClientBuilder creates MCP clients over STDIO, SSE or Streamable HTTP, with common headers, query parameters and timeouts. The resulting client registers directly into a Toolkit.