Documentation
¶
Overview ¶
Package mcp 实现模型上下文协议 (Model Context Protocol) 的客户端管理。 它负责连接远程 MCP 服务器,发现并注册其提供的工具,并处理工具调用请求。
Index ¶
Constants ¶
View Source
const ( TransportStdio = "stdio" // 标准输入输出传输协议 TransportHTTPSSE = "http_sse" // HTTP SSE 传输协议 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client interface {
// ListTools 获取服务器提供的所有工具列表。
ListTools(ctx context.Context) ([]ToolDefinition, error)
// CallTool 调用指定的工具并返回结果。
CallTool(ctx context.Context, toolName, argsJSON string) (any, error)
}
Client 定义了与 MCP 服务器交互的客户端接口。
type Connector ¶
type Connector interface {
// Connect 根据配置连接到指定的 MCP 服务器。
Connect(ctx context.Context, serverName string, cfg config.MCPServerConfig) (Client, error)
}
Connector 定义了建立 MCP 连接并返回客户端实例的接口。
type Connectors ¶
Connectors 包含了支持的所有传输协议的连接器实现。
func DefaultConnectors ¶
func DefaultConnectors() Connectors
DefaultConnectors 返回用于 stdio 和 HTTP/SSE 传输的生产连接器。
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 统一管理所有已配置的 MCP 服务器及其动态工具。
func NewManager ¶
func NewManager(servers map[string]config.MCPServerConfig, connectors Connectors) *Manager
NewManager 根据配置和连接器集合创建一个新的 MCP 管理器。
func (*Manager) CallTool ¶
func (m *Manager) CallTool(ctx context.Context, serverName, toolName, argsJSON string) (string, error)
CallTool 将原始工具调用路由到选定的 MCP 服务器客户端。 当服务器降级或工具调用失败时,CallTool 会尝试带退避的有界重连。
func (*Manager) RegisterTools ¶
RegisterTools 将发现的 MCP 工具注册到给定的注册表中。
type ServerStatus ¶
type ServerStatus struct {
Name string // 服务器名称
Transport string // 使用的传输协议
Connected bool // 是否已成功连接
Degraded bool // 是否处于降级(异常)状态
ToolCount int // 发现的工具数量
Message string // 状态描述消息或错误信息
}
ServerStatus 表示 MCP 服务器在管理器中的当前运行状态。
type ToolDefinition ¶
ToolDefinition 描述从 MCP 服务器发现的工具元数据。
Click to show internal directories.
Click to hide internal directories.