Documentation
¶
Overview ¶
Package agentplugin 实现 OpenAI Agent Plugins 1.0.0 规范的可移植插件加载器。
规范要点:
- 一个插件是一个目录,根目录必须含 plugin.json 清单(闭包 schema)。
- 组件固定位置:skills/<name>/SKILL.md 与根目录 mcp.json。
- 失败隔离:单个 MCP server 失败不阻塞 skills;不合规 skill 跳过;顶层 mcp.json 无效仅禁用 MCP;manifest 致命违反才拒绝整个插件。
- 两个运行时变量 ${PLUGIN_ROOT} / ${PLUGIN_DATA} 仅在 stdio 的 args/env 值/cwd 展开。
本包只负责「加载、校验、发现、连接」,不规定分发、安装与权限模型(规范明确排除)。
Index ¶
- Constants
- func DefaultScanDir() string
- func EnsureDefaultScanDir() error
- func InstallFromZip(zipPath, name string) (string, error)
- func RegisterAllTools(plugins map[string]*ManagedPlugin, reg ToolRegistrar)
- func StartAll(plugins map[string]*ManagedPlugin)
- func StopAll(plugins map[string]*ManagedPlugin)
- func Summary(plugins map[string]*ManagedPlugin) []map[string]any
- func Uninstall(name string) (string, error)
- type Author
- type MCPConfig
- type MCPEntryStatus
- type MCPPluginTool
- type MCPServerSpec
- type ManagedPlugin
- type Manager
- type Manifest
- type Plugin
- type Runtime
- type SkillRef
- type SkillsGroup
- type ToolRegistrar
Constants ¶
const ( // SpecVersion 当前支持的 Agent Plugins 规范版本。 SpecVersion = "1.0.0" // PluginSchemaURL plugin.json 的规范 schema 标识。 PluginSchemaURL = "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json" // MCPSchemaURL mcp.json 的规范 schema 标识。 MCPSchemaURL = "https://agent-plugins.org/schemas/1.0.0/mcp.schema.json" )
规范常量
const ( TransportStdio = "stdio" TransportStreamableHTTP = "streamable-http" TransportSSE = "sse" )
Transport 类型枚举。
const SkillSourceAgentPlugin skills.SkillSource = "agentplugin"
SkillSourceAgentPlugin 标记来自 Agent Plugin 的 skill,便于集成层区分来源。
Variables ¶
This section is empty.
Functions ¶
func InstallFromZip ¶
InstallFromZip 将一个插件 zip 包解压到扫描目录下。
zip 包结构要求:顶层可含一个根目录(即插件目录),该目录必须含 plugin.json; 或 zip 直接以 plugin.json 为顶层条目(此时用 name 作为目录名)。 已存在同名插件目录时返回错误(调用方可先 Uninstall)。
返回插件目录的绝对路径。
func RegisterAllTools ¶
func RegisterAllTools(plugins map[string]*ManagedPlugin, reg ToolRegistrar)
RegisterAllTools 将所有受管插件的 MCP 工具注册到工具注册表(跳过被禁用的插件)。
func StartAll ¶
func StartAll(plugins map[string]*ManagedPlugin)
StartAll 启动所有受管插件的 MCP 运行时(跳过被禁用的插件)。
Types ¶
type Author ¶
type Author struct {
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
URL string `json:"url,omitempty"`
}
Author 清单中的作者信息。
type MCPConfig ¶
type MCPConfig struct {
Schema string `json:"$schema"`
MCPServers map[string]MCPServerSpec `json:"mcpServers"`
}
MCPConfig 描述 mcp.json 文档。
type MCPEntryStatus ¶
type MCPEntryStatus struct {
Name string // server 条目名
Spec MCPServerSpec // 原始配置(占位符已展开)
Connected bool // 是否已连接
ToolCount int // 已发现的工具数
Error string // 连接/校验失败原因(空表示成功)
}
MCPEntryStatus 描述一个 MCP server 条目的连接状态。
type MCPPluginTool ¶
type MCPPluginTool struct {
// contains filtered or unexported fields
}
MCPPluginTool 将插件 MCP 工具适配为 tool registry 可注册的形态。
func (*MCPPluginTool) Description ¶
func (t *MCPPluginTool) Description() string
Description 返回工具描述。
func (*MCPPluginTool) Schema ¶
func (t *MCPPluginTool) Schema() map[string]any
Schema 返回工具输入 schema(与 tool.Tool 接口对接)。
type MCPServerSpec ¶
type MCPServerSpec struct {
Type string `json:"type"` // stdio | streamable-http | sse
Command string `json:"command,omitempty"` // stdio:单个可执行 token(不展开占位符)
Args []string `json:"args,omitempty"` // stdio:展开占位符
Env map[string]string `json:"env,omitempty"` // stdio:env 值展开占位符,key 不展开
Cwd string `json:"cwd,omitempty"` // stdio:展开占位符,须在 PLUGIN_ROOT/PLUGIN_DATA 内
URL string `json:"url,omitempty"` // http:绝对 URL(不展开占位符)
Headers map[string]string `json:"headers,omitempty"` // http:字面 headers,不含凭证
}
MCPServerSpec 描述 mcp.json 中单个 MCP server 条目。
type ManagedPlugin ¶
type ManagedPlugin struct {
Plugin *Plugin
Runtime *Runtime // MCP 运行时;无 MCP 或被禁用时为 nil
Disabled bool // 是否被显式禁用(禁用时不启动 MCP、不注入 skills/tools)
}
ManagedPlugin 封装一个已加载插件及其运行时(若有 MCP)。
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 管理多个已加载的 Agent Plugin。
func NewManager ¶
NewManager 创建一个扫描指定根目录列表的 Manager。 disabled 为被显式禁用的插件名集合,这些插件不会被加载也不会启动 MCP。
func (*Manager) LoadAll ¶
func (m *Manager) LoadAll() map[string]*ManagedPlugin
LoadAll 加载所有扫描根目录下的插件,返回按 name 索引的结果。
发现规则:每个扫描根的直接子目录,若含常规 plugin.json 则视为一个插件。 被禁用的插件仍会被加载(以便在 UI 中展示),但其 Runtime 为 nil, 调用方应据此跳过 StartAll / skills 注入 / tools 注入。 单个插件致命失败不阻塞其他插件(插件级失败隔离)。
type Manifest ¶
type Manifest struct {
Schema string `json:"$schema"` // 必须,选择规范校验契约
Name string `json:"name"` // 必须,1-64 字符,小写字母/数字/连字符/点
Version string `json:"version,omitempty"` // 可选,推荐 SemVer
Description string `json:"description,omitempty"`
Author *Author `json:"author,omitempty"`
Homepage string `json:"homepage,omitempty"`
Repository string `json:"repository,omitempty"`
License string `json:"license,omitempty"`
Keywords []string `json:"keywords,omitempty"`
Extensions map[string]any `json:"extensions,omitempty"` // 反向域名命名空间,客户端按需消费
Unknown []string `json:"-"` // 加载时检测到的未知顶层字段(报告用)
}
Manifest 描述 plugin.json 清单。 schema 为闭包:未知顶层字段须报告并忽略(非致命),其他 schema 违反为致命。
type Plugin ¶
type Plugin struct {
Root string // 插件根目录(文件系统解析后的绝对路径)
DataDir string // PLUGIN_DATA 可写数据目录(跨更新持久)
Manifest *Manifest // 清单
Skills []SkillRef // 发现的 skills
MCPServers []MCPEntryStatus // MCP 连接状态
Extensions map[string]any // 已识别的扩展命名空间数据(未实现的命名空间已忽略)
// 加载报告
MCPDisabled bool // 顶层 mcp.json 无效时整个 MCP 被禁用
ManifestValid bool // 清单是否通过校验
FatalError string // 致命错误(非空时插件被拒绝,仅 Root/Manifest 可用)
Warnings []string // 非致命报告(unknown 字段、非 object extensions、跳过的 skill 等)
}
Plugin 表示一个已加载的 Agent Plugin。
func Load ¶
Load 从 pluginRoot 加载一个 Agent Plugin,执行全部静态校验与组件发现,但不连接 MCP。
失败边界(按规范最窄原则):
- plugin.json 解析在根外 / 缺失 / 致命 schema 违反 → 拒绝整个插件(FatalError 非空)。
- 组件类型固定位置类型错误(如 skills/ 不是目录)→ 仅该组件类型无效。
- 单个 skill 的 SKILL.md 逃逸根或不合规 → 跳过该 skill。
- 单个 MCP entry 配置逃逸或不合规 → 该条目 Error,其他继续。
- 顶层 mcp.json 无效 → 整个 MCP 禁用(MCPDisabled=true),skills 不受影响。
dataDir 为 PLUGIN_DATA 可写目录(跨更新持久),由调用方提供;为空时自动推导。 MCP 连接在 runtime.Start 中进行,Load 保持幂等且无副作用。
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime 管理单个 Agent Plugin 的 MCP 连接生命周期。
规范要求:单个 MCP server 启动失败仅禁用该条目,其余 server/skills/扩展继续可用。 Start 对每个静态校验通过的条目尝试连接,失败记入 entry.Error,不传播异常。
func (*Runtime) CallTool ¶
func (r *Runtime) CallTool(ctx context.Context, serverName, toolName string, args map[string]any) (any, error)
CallTool 调用指定 server 的工具。
func (*Runtime) RegisterTools ¶
func (r *Runtime) RegisterTools(reg ToolRegistrar, pluginName string)
RegisterTools 将所有已连接 server 的工具注册到工具注册表。
type SkillRef ¶
type SkillRef struct {
Name string // skill 子目录名
Dir string // skill 目录绝对路径(已解析符号链接)
Skill *skills.Skill // 解析后的 skill(失败时为 nil)
Error string // 该 skill 加载失败原因(空表示成功)
}
SkillRef 描述插件内发现的一个 skill。
type SkillsGroup ¶
SkillsGroup 按插件分组的 skills。
func AllSkills ¶
func AllSkills(plugins map[string]*ManagedPlugin) []*SkillsGroup
AllSkills 收集所有受管插件中成功加载的 skills,按插件名分组返回。 被禁用的插件的 skills 不计入(禁用即不注入)。 集成层(如 skills.Manager)可据此注入插件来源的 skills。
type ToolRegistrar ¶
ToolRegistrar 是工具注册表的最小接口(由 *tool.Registry 实现)。 使用 tool.Tool 命名接口类型,确保 *tool.Registry.Register(tool.Tool) 签名精确匹配。