Help us shape the future of Go! Take the 2026 Go Developer Survey

mcp

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 21 Imported by: 0

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

type Connectors struct {
	Stdio   Connector // stdio 传输连接器
	HTTPSSE Connector // http_sse 传输连接器
}

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) Connect

func (m *Manager) Connect(ctx context.Context) error

Connect 从每个已配置的服务器发现工具。 失败被跟踪为降级状态,不会导致整个管理器失败。

func (*Manager) RegisterTools

func (m *Manager) RegisterTools(reg *tools.Registry) error

RegisterTools 将发现的 MCP 工具注册到给定的注册表中。

func (*Manager) Statuses

func (m *Manager) Statuses() []ServerStatus

Statuses 返回每个服务器的连接/发现状态。

type ServerStatus

type ServerStatus struct {
	Name      string // 服务器名称
	Transport string // 使用的传输协议
	Connected bool   // 是否已成功连接
	Degraded  bool   // 是否处于降级(异常)状态
	ToolCount int    // 发现的工具数量
	Message   string // 状态描述消息或错误信息
}

ServerStatus 表示 MCP 服务器在管理器中的当前运行状态。

type ToolDefinition

type ToolDefinition struct {
	Name        string // 工具名称
	Description string // 工具功能描述
}

ToolDefinition 描述从 MCP 服务器发现的工具元数据。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL