docs(mcp): 存量英文注释中文化(仅注释,代码零字节改动)
- capability/mcp 整包 17 个文件核查:仅 config.go / registry.go 含英文注释(38 行)→ 全部中文化; 其余 15 个文件本就全中文,零改动。 - 保留 YAML/Go/JSON 示例行与字符串字面量(非注释,禁止改动)。 - 验收:机械校验为空;gofmt -l 干净;go build/vet/test ./internal/capability/mcp/... 全绿。
This commit is contained in:
1 parent
2593765add
commit
5c2189efb0
2 files changed
+31
-38
No files matched your search
@@ -9,17 +9,16 @@ import (
|
|||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load reads MCP server configuration from the process environment. It returns
|
// Load 从进程环境读取 MCP server 配置,返回所有已配置的服务器;
|
||||||
// every configured server; callers filter on Enabled.
|
// 由调用方按 Enabled 过滤。
|
||||||
func Load() (*Config, error) {
|
func Load() (*Config, error) {
|
||||||
return FromEnv(os.Environ())
|
return FromEnv(os.Environ())
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromEnv builds a Config from a KEY=value pair slice such as os.Environ() or a
|
// FromEnv 从形如 os.Environ() 或测试夹具的 KEY=value 键值对切片构建 Config。
|
||||||
// test fixture. Named servers are read from MCP_<NAME>_* for indexes; the
|
// 带编号的服务器按索引从 MCP_<NAME>_* 读取;不带编号的 MCP_* 变量描述
|
||||||
// unnumbered MCP_* variables describe a single default server. When N is a
|
// 单台默认服务器。逐组读取(每组一台服务器),扫描在遇到第一组
|
||||||
// positive index, one server is read per group and scanning stops at the first
|
// NAME/COMMAND/URL 全空的配置时停止。
|
||||||
// group with no NAME/COMMAND/URL set.
|
|
||||||
func FromEnv(pairs []string) (*Config, error) {
|
func FromEnv(pairs []string) (*Config, error) {
|
||||||
env := make(map[string]string, len(pairs))
|
env := make(map[string]string, len(pairs))
|
||||||
for _, p := range pairs {
|
for _, p := range pairs {
|
||||||
@@ -31,7 +30,7 @@ func FromEnv(pairs []string) (*Config, error) {
|
|||||||
return &Config{Servers: fromEnvMap(env)}, nil
|
return &Config{Servers: fromEnvMap(env)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FromYAML parses a YAML document describing MCP servers, for example:
|
// FromYAML 解析描述 MCP server 的 YAML 文档,例如:
|
||||||
//
|
//
|
||||||
// servers:
|
// servers:
|
||||||
// - name: calc
|
// - name: calc
|
||||||
@@ -48,7 +47,7 @@ func FromYAML(data []byte) (*Config, error) {
|
|||||||
return &Config{Servers: doc.Servers}, nil
|
return &Config{Servers: doc.Servers}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// fromEnvMap builds the server list from a resolved environment map.
|
// fromEnvMap 从已解析的环境变量 map 构建服务器列表。
|
||||||
func fromEnvMap(env map[string]string) []MCPConfig {
|
func fromEnvMap(env map[string]string) []MCPConfig {
|
||||||
var servers []MCPConfig
|
var servers []MCPConfig
|
||||||
if cfg, ok := readOne(env, "MCP"); ok {
|
if cfg, ok := readOne(env, "MCP"); ok {
|
||||||
@@ -64,8 +63,8 @@ func fromEnvMap(env map[string]string) []MCPConfig {
|
|||||||
return servers
|
return servers
|
||||||
}
|
}
|
||||||
|
|
||||||
// readOne reads a single MCPConfig from env using the given variable prefix
|
// readOne 用给定的变量前缀(如 "MCP" 或 "MCP_0")从环境读取一台 MCPConfig;
|
||||||
// (e.g. "MCP" or "MCP_0"). It reports ok=false when no identifying field is set.
|
// 没有任何标识字段时返回 ok=false。
|
||||||
func readOne(env map[string]string, prefix string) (MCPConfig, bool) {
|
func readOne(env map[string]string, prefix string) (MCPConfig, bool) {
|
||||||
p := prefix
|
p := prefix
|
||||||
if p == "" {
|
if p == "" {
|
||||||
@@ -88,8 +87,7 @@ func readOne(env map[string]string, prefix string) (MCPConfig, bool) {
|
|||||||
}, true
|
}, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// splitArgs splits a comma-separated args value, trimming spaces and empty
|
// splitArgs 拆分逗号分隔的 args 值,去掉空白与空项。
|
||||||
// entries.
|
|
||||||
func splitArgs(raw string) []string {
|
func splitArgs(raw string) []string {
|
||||||
raw = strings.TrimSpace(raw)
|
raw = strings.TrimSpace(raw)
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
@@ -105,7 +103,7 @@ func splitArgs(raw string) []string {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseBool parses a boolean value, defaulting to def on empty or invalid input.
|
// parseBool 解析布尔值;输入为空或非法时返回默认值 def。
|
||||||
func parseBool(raw string, def bool) bool {
|
func parseBool(raw string, def bool) bool {
|
||||||
raw = strings.TrimSpace(raw)
|
raw = strings.TrimSpace(raw)
|
||||||
if raw == "" {
|
if raw == "" {
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ import (
|
|||||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Registry connects an MCP client and auto-registers its discovered tools into
|
// Registry 连接一台 MCP 客户端,把自动发现的工具注册进共享的 capability
|
||||||
// the shared capability registry, making them available to every character.
|
// registry,使所有角色都能使用。
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
reg *registry.Registry
|
reg *registry.Registry
|
||||||
mcp *Client
|
mcp *Client
|
||||||
@@ -21,11 +21,10 @@ type Registry struct {
|
|||||||
closed bool
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRegistry builds a Registry that registers cfg's tools into reg. Options
|
// NewRegistry 构建一个 Registry,把 cfg 的工具注册进 reg。opts 作用在底层
|
||||||
// apply to the underlying Client (logger, injected transport). It is named
|
// Client 上(日志器、注入的传输)。之所以叫 NewRegistry 而不是 New,是因为
|
||||||
// NewRegistry rather than New because this package already exports New for
|
// 本包已为 Client 导出了 New。这里会自动接上工具清单变更回调,
|
||||||
// Client. A tool-list-changed handler is wired automatically so the registry
|
// 服务器发 tools/list_changed 时 registry 会重新同步。
|
||||||
// re-syncs when the server announces tools/list_changed.
|
|
||||||
func NewRegistry(reg *registry.Registry, cfg MCPConfig, opts ...Option) *Registry {
|
func NewRegistry(reg *registry.Registry, cfg MCPConfig, opts ...Option) *Registry {
|
||||||
r := &Registry{reg: reg}
|
r := &Registry{reg: reg}
|
||||||
opts = append(opts, WithToolListChangedHandler(r.refresh))
|
opts = append(opts, WithToolListChangedHandler(r.refresh))
|
||||||
@@ -35,8 +34,7 @@ func NewRegistry(reg *registry.Registry, cfg MCPConfig, opts ...Option) *Registr
|
|||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register connects the MCP server, discovers its tools, and registers each one
|
// Register 连接 MCP 服务器、发现其工具,并把每把工具注册进底层 registry。
|
||||||
// into the underlying registry.
|
|
||||||
func (r *Registry) Register(ctx context.Context) error {
|
func (r *Registry) Register(ctx context.Context) error {
|
||||||
if r.reg == nil {
|
if r.reg == nil {
|
||||||
return fmt.Errorf("mcp registry: nil registry")
|
return fmt.Errorf("mcp registry: nil registry")
|
||||||
@@ -64,12 +62,10 @@ func (r *Registry) Register(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// refresh re-syncs the registered tool set with the server after a
|
// refresh 在收到 notifications/tools/list_changed 通知后,把已注册工具集与
|
||||||
// notifications/tools/list_changed notification. It re-discovers the current
|
// 服务器重新对齐。它重新发现当前工具并只应用差异:移除已消失的名字、
|
||||||
// tools and applies only the diff: it removes names that are gone and registers
|
// 注册新出现的名字,因此其它提供者注册的同名工具(名字冲突)会被保留。
|
||||||
// newly appeared ones, so tools registered by other providers (which collide by
|
// 它由客户端的工具清单变更回调调用(不在传输读路径上),并由客户端合并触发。
|
||||||
// name) are preserved. It is invoked (off the transport read path) by the
|
|
||||||
// client's tool-list-changed handler and coalesced by the client.
|
|
||||||
func (r *Registry) refresh() {
|
func (r *Registry) refresh() {
|
||||||
if r.reg == nil {
|
if r.reg == nil {
|
||||||
return
|
return
|
||||||
@@ -91,9 +87,8 @@ func (r *Registry) refresh() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drop names the server no longer exposes. Tools registered by other
|
// 丢弃服务器不再暴露的名字。其它提供者注册的工具在注册时就被跳过、
|
||||||
// providers were skipped at registration time and are not in r.names, so they
|
// 不在 r.names 里,因此这里永远不会把它们删掉。
|
||||||
// are never removed here.
|
|
||||||
kept := r.names[:0]
|
kept := r.names[:0]
|
||||||
for _, name := range r.names {
|
for _, name := range r.names {
|
||||||
if !incoming[name] {
|
if !incoming[name] {
|
||||||
@@ -106,8 +101,8 @@ func (r *Registry) refresh() {
|
|||||||
}
|
}
|
||||||
r.names = kept
|
r.names = kept
|
||||||
|
|
||||||
// Register newly appeared tools. Skips names already present (own or
|
// 注册新出现的工具。跳过已存在的名字(无论是自己的还是外来的),
|
||||||
// foreign) so a name collision with another provider is not doubled.
|
// 避免与其它提供者同名时重复注册。
|
||||||
existing := make(map[string]bool, len(r.names))
|
existing := make(map[string]bool, len(r.names))
|
||||||
for _, name := range r.names {
|
for _, name := range r.names {
|
||||||
existing[name] = true
|
existing[name] = true
|
||||||
@@ -127,16 +122,16 @@ func (r *Registry) refresh() {
|
|||||||
r.log.Info("mcp tools refreshed", "name", r.mcp.cfg.Name, "count", len(r.names))
|
r.log.Info("mcp tools refreshed", "name", r.mcp.cfg.Name, "count", len(r.names))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToolNames returns a copy of the tool names this registry registered, in
|
// ToolNames 返回本 registry 注册过的工具名副本(按注册顺序)。
|
||||||
// registration order. It is empty before Register succeeds.
|
// Register 成功之前为空。
|
||||||
func (r *Registry) ToolNames() []string {
|
func (r *Registry) ToolNames() []string {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
return append([]string(nil), r.names...)
|
return append([]string(nil), r.names...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unregister removes every tool this registry previously registered and closes
|
// Unregister 移除本 registry 之前注册的全部工具,并关闭底层客户端。
|
||||||
// the underlying client. It is safe to call multiple times.
|
// 可安全重复调用。
|
||||||
func (r *Registry) Unregister(ctx context.Context) error {
|
func (r *Registry) Unregister(ctx context.Context) error {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
names := r.names
|
names := r.names
|
||||||
|
|||||||
Reference in new issue
Block a user