mcp

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseGlobalMCPPool

func CloseGlobalMCPPool(ctx context.Context) error

CloseGlobalMCPPool 关闭全局MCP客户端池

func GetAllToolsGlobal

func GetAllToolsGlobal() (map[string][]*ToolSignature, error)

GetAllToolsGlobal 获取全局池中所有服务器的工具

func GetHealthyServersGlobal

func GetHealthyServersGlobal() ([]string, error)

GetHealthyServersGlobal 获取全局池中健康的服务器列表

func InitGlobalMCPPool

func InitGlobalMCPPool(ctx context.Context) error

InitGlobalMCPPool 从配置中初始化全局MCP客户端池

func IsGlobalMCPPoolInitialized

func IsGlobalMCPPoolInitialized() bool

IsGlobalMCPPoolInitialized 检查全局MCP客户端池是否已初始化

func ReloadGlobalMCPPool

func ReloadGlobalMCPPool(ctx context.Context) error

ReloadGlobalMCPPool 重新加载全局MCP客户端池配置

Types

type ClientPool

type ClientPool interface {
	// Server management
	RegisterServer(ctx context.Context, name string, config ServerDefinition) error
	UnregisterServer(name string) error
	GetServerNames() []string

	// Client access
	GetClientSession(ctx context.Context, server string) (*mcpSdk.ClientSession, error)
	GetClientWrapper(server string) (ClientWrapper, error)

	// Health management
	StartHealthCheck(ctx context.Context, interval time.Duration) error
	StopHealthCheck() error
	PerformHealthCheck(ctx context.Context) error

	// Lifecycle
	Close(ctx context.Context) error

	// Statistics and monitoring
	GetPoolStats() PoolStats
	GetHealthyServers() []string
	GetUnhealthyServers() []string
}

ClientPool defines the interface for MCP client pools

type ClientProvider

type ClientProvider interface {
	GetClientSession(server string) (*mcpSdk.ClientSession, error)
	RegisterServer(name string, config ServerDefinition) error
	UnregisterServer(name string) error
	GetServerNames() []string
	StartHealthCheck(interval time.Duration)
	StopHealthCheck()
	Close() error
}

ClientProvider interface for getting MCP clients

type ClientWrapper

type ClientWrapper interface {
	// Connection management
	GetSession() *mcpSdk.ClientSession
	IsHealthy() bool
	GetLastHealthyTime() time.Time
	Reconnect(ctx context.Context) error
	Close() error

	// Configuration
	GetConfig() ServerDefinition
	GetRetryCount() int

	// Tool management
	CacheAllTools(ctx context.Context) error
	GetToolSignatures() map[string]*ToolSignature
	GetToolSignature(toolName string) (*ToolSignature, bool)

	// Health status
	SetHealthy(healthy bool)
	IncrementRetryCount() int
	ResetRetryCount()
}

ClientWrapper defines the interface for MCP client wrappers

type DefaultToolManager

type DefaultToolManager struct {
	// contains filtered or unexported fields
}

DefaultToolManager implements ToolManager interface

func NewDefaultToolManager

func NewDefaultToolManager(pool ClientPool) *DefaultToolManager

NewDefaultToolManager creates a new tool manager

func (*DefaultToolManager) CallTool

func (tm *DefaultToolManager) CallTool(ctx context.Context, serverName, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallTool calls a tool on a specific server

func (*DefaultToolManager) CallToolAnyServer

func (tm *DefaultToolManager) CallToolAnyServer(ctx context.Context, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallToolAnyServer calls a tool on any available server that has it

func (*DefaultToolManager) CallToolWithRetry

func (tm *DefaultToolManager) CallToolWithRetry(ctx context.Context, serverName, toolName string, args map[string]interface{}, maxRetries int) (*ToolCallResult, error)

CallToolWithRetry calls a tool with retry logic

func (*DefaultToolManager) FindToolByName

func (tm *DefaultToolManager) FindToolByName(ctx context.Context, toolName string) ([]ServerToolInfo, error)

FindToolByName finds tools by name across all servers

func (*DefaultToolManager) GetAllTools

func (tm *DefaultToolManager) GetAllTools(ctx context.Context) (map[string][]*ToolSignature, error)

GetAllTools returns all tools from all servers

func (*DefaultToolManager) GetToolsByServer

func (tm *DefaultToolManager) GetToolsByServer(ctx context.Context, serverName string) ([]*ToolSignature, error)

GetToolsByServer returns tools from a specific server

type HealthChecker

type HealthChecker interface {
	CheckHealth(ctx context.Context, wrapper ClientWrapper) error
	GetCheckInterval() time.Duration
	GetCheckTimeout() time.Duration
}

HealthChecker defines the interface for health checking strategies

type MCPClientPool

type MCPClientPool struct {
	// contains filtered or unexported fields
}

MCPClientPool manages a pool of MCP clients Implements ClientPool interface

func EnsureGlobalMCPPool

func EnsureGlobalMCPPool(ctx context.Context) (*MCPClientPool, error)

EnsureGlobalMCPPool 确保全局MCP客户端池已初始化,如果未初始化则自动初始化

func GetGlobalMCPPool

func GetGlobalMCPPool() (*MCPClientPool, error)

GetGlobalMCPPool 获取全局MCP客户端池

func NewMCPClientPool

func NewMCPClientPool() *MCPClientPool

NewMCPClientPool creates a new MCP client pool

func (*MCPClientPool) CallTool

func (p *MCPClientPool) CallTool(ctx context.Context, serverName, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallTool calls a tool on a specific server

func (*MCPClientPool) CallToolAnyServer

func (p *MCPClientPool) CallToolAnyServer(ctx context.Context, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallToolAnyServer calls a tool on any available server that has it

func (*MCPClientPool) Close

func (p *MCPClientPool) Close(ctx context.Context) error

Close closes all client connections and stops health checks

func (*MCPClientPool) CloseCompat

func (p *MCPClientPool) CloseCompat() error

Close 的兼容版本(不支持context)

func (*MCPClientPool) GetAllTools

func (p *MCPClientPool) GetAllTools() (map[string][]*ToolSignature, error)

GetAllTools returns all tools from all servers

func (*MCPClientPool) GetClientSession

func (p *MCPClientPool) GetClientSession(ctx context.Context, server string) (*mcpSdk.ClientSession, error)

GetClientSession returns a client session for the specified server

func (*MCPClientPool) GetClientSessionCompat

func (p *MCPClientPool) GetClientSessionCompat(server string) (*mcpSdk.ClientSession, error)

GetClientSession 的兼容版本(不支持context)

func (*MCPClientPool) GetClientWrapper

func (p *MCPClientPool) GetClientWrapper(server string) (ClientWrapper, error)

GetClientWrapper returns the wrapper for a server

func (*MCPClientPool) GetHealthyServers

func (p *MCPClientPool) GetHealthyServers() []string

GetHealthyServers returns names of healthy servers

func (*MCPClientPool) GetPoolStats

func (p *MCPClientPool) GetPoolStats() PoolStats

GetPoolStats returns statistics about the client pool

func (*MCPClientPool) GetServerNames

func (p *MCPClientPool) GetServerNames() []string

GetServerNames returns all registered server names

func (*MCPClientPool) GetUnhealthyServers

func (p *MCPClientPool) GetUnhealthyServers() []string

GetUnhealthyServers returns names of unhealthy servers

func (*MCPClientPool) PerformHealthCheck

func (p *MCPClientPool) PerformHealthCheck(ctx context.Context) error

PerformHealthCheck checks the health of all registered clients

func (*MCPClientPool) RefreshAllTools

func (p *MCPClientPool) RefreshAllTools(ctx context.Context) error

RefreshAllTools refreshes tool cache for all servers

func (*MCPClientPool) RegisterServer

func (p *MCPClientPool) RegisterServer(ctx context.Context, name string, config ServerDefinition) error

RegisterServer registers a new MCP server configuration

func (*MCPClientPool) RegisterServerCompat

func (p *MCPClientPool) RegisterServerCompat(name string, config ServerDefinition) error

RegisterServer 的兼容版本(不支持context)

func (*MCPClientPool) StartHealthCheck

func (p *MCPClientPool) StartHealthCheck(ctx context.Context, interval time.Duration) error

StartHealthCheck starts periodic health checks if enabled in global config

func (*MCPClientPool) StartHealthCheckCompat

func (p *MCPClientPool) StartHealthCheckCompat(interval time.Duration)

StartHealthCheck 的兼容版本(不支持context)

func (*MCPClientPool) StopHealthCheck

func (p *MCPClientPool) StopHealthCheck() error

StopHealthCheck stops the health check routine

func (*MCPClientPool) StopHealthCheckCompat

func (p *MCPClientPool) StopHealthCheckCompat()

StopHealthCheck 的兼容版本(不支持context)

func (*MCPClientPool) UnregisterServer

func (p *MCPClientPool) UnregisterServer(name string) error

UnregisterServer removes a server from the pool

func (*MCPClientPool) UnregisterServerCompat

func (p *MCPClientPool) UnregisterServerCompat(name string) error

UnregisterServer 的兼容版本(不支持context)

type MCPClientWrapper

type MCPClientWrapper struct {
	ToolSignatures map[string]*ToolSignature
	// contains filtered or unexported fields
}

MCPClientWrapper wraps the MCP client with additional metadata Implements ClientWrapper interface

func NewMCPClientWrapper

func NewMCPClientWrapper(name string, config ServerDefinition) *MCPClientWrapper

NewMCPClientWrapper creates a new MCP client wrapper

func (*MCPClientWrapper) CacheAllTools

func (w *MCPClientWrapper) CacheAllTools(ctx context.Context) error

CacheAllTools caches all tools from the server with default timeout

func (*MCPClientWrapper) CallTool

func (w *MCPClientWrapper) CallTool(ctx context.Context, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallTool calls a tool on this server

func (*MCPClientWrapper) Close

func (w *MCPClientWrapper) Close() error

Close closes the client session

func (*MCPClientWrapper) GetConfig

func (w *MCPClientWrapper) GetConfig() ServerDefinition

GetConfig returns the server configuration

func (*MCPClientWrapper) GetLastError

func (w *MCPClientWrapper) GetLastError() error

GetLastError returns the last error that occurred

func (*MCPClientWrapper) GetLastHealthyTime

func (w *MCPClientWrapper) GetLastHealthyTime() time.Time

GetLastHealthyTime returns the last time the client was healthy

func (*MCPClientWrapper) GetName

func (w *MCPClientWrapper) GetName() string

GetName returns the server name

func (*MCPClientWrapper) GetRetryCount

func (w *MCPClientWrapper) GetRetryCount() int

GetRetryCount returns the current retry count

func (*MCPClientWrapper) GetSession

func (w *MCPClientWrapper) GetSession() *mcpSdk.ClientSession

GetSession returns the underlying MCP client session

func (*MCPClientWrapper) GetToolSignature

func (w *MCPClientWrapper) GetToolSignature(toolName string) (*ToolSignature, bool)

GetToolSignature returns a specific tool signature

func (*MCPClientWrapper) GetToolSignatures

func (w *MCPClientWrapper) GetToolSignatures() map[string]*ToolSignature

GetToolSignatures returns all cached tool signatures

func (*MCPClientWrapper) HasTool

func (w *MCPClientWrapper) HasTool(toolName string) bool

HasTool checks if the server has a specific tool

func (*MCPClientWrapper) IncrementRetryCount

func (w *MCPClientWrapper) IncrementRetryCount() int

IncrementRetryCount increments the retry count

func (*MCPClientWrapper) IsHealthy

func (w *MCPClientWrapper) IsHealthy() bool

IsHealthy returns the current health status

func (*MCPClientWrapper) Reconnect

func (w *MCPClientWrapper) Reconnect(ctx context.Context) error

Reconnect attempts to reconnect the client with retry logic

func (*MCPClientWrapper) ResetRetryCount

func (w *MCPClientWrapper) ResetRetryCount()

ResetRetryCount resets the retry count

func (*MCPClientWrapper) SetHealthy

func (w *MCPClientWrapper) SetHealthy(healthy bool)

SetHealthy updates the health status

type PoolConfig

type PoolConfig struct {
	MaxConcurrentHealthChecks int           `json:"max_concurrent_health_checks"`
	HealthCheckInterval       time.Duration `json:"health_check_interval"`
	HealthCheckTimeout        time.Duration `json:"health_check_timeout"`
}

PoolConfig represents pool configuration

type PoolStats

type PoolStats struct {
	TotalServers     int                   `json:"total_servers"`
	HealthyServers   int                   `json:"healthy_servers"`
	UnhealthyServers int                   `json:"unhealthy_servers"`
	ServerStats      map[string]ServerStat `json:"server_stats"`
	PoolConfig       PoolConfig            `json:"pool_config"`
}

PoolStats represents client pool statistics

func GetGlobalMCPPoolStats

func GetGlobalMCPPoolStats() (PoolStats, error)

GetGlobalMCPPoolStats 获取全局MCP客户端池统计信息

type ServerDefinition

type ServerDefinition types.MCPServerDefinition

func (*ServerDefinition) GetEffectiveTimeout

func (d *ServerDefinition) GetEffectiveTimeout() time.Duration

GetEffectiveTimeout returns the effective timeout considering global config

type ServerStat

type ServerStat struct {
	Name            string    `json:"name"`
	Healthy         bool      `json:"healthy"`
	LastHealthyTime time.Time `json:"last_healthy_time"`
	RetryCount      int       `json:"retry_count"`
	ToolCount       int       `json:"tool_count"`
}

ServerStat represents individual server statistics

type ServerToolInfo

type ServerToolInfo struct {
	ServerName string         `json:"server_name"`
	Tool       *ToolSignature `json:"tool"`
}

ServerToolInfo represents tool information with server context

type ToolCallResult

type ToolCallResult struct {
	ServerName string                 `json:"server_name"`
	ToolName   string                 `json:"tool_name"`
	Success    bool                   `json:"success"`
	Result     map[string]interface{} `json:"result,omitempty"`
	Error      string                 `json:"error,omitempty"`
	Duration   time.Duration          `json:"duration"`
	Timestamp  time.Time              `json:"timestamp"`
}

ToolCallResult represents the result of a tool call

func CallToolAnyServerGlobal

func CallToolAnyServerGlobal(ctx context.Context, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallToolAnyServerGlobal 使用全局池在任何可用服务器上调用工具

func CallToolGlobal

func CallToolGlobal(ctx context.Context, serverName, toolName string, args map[string]interface{}) (*ToolCallResult, error)

CallToolGlobal 使用全局池调用指定服务器上的工具

type ToolManager

type ToolManager interface {
	// Tool discovery
	GetAllTools(ctx context.Context) (map[string][]*ToolSignature, error)
	GetToolsByServer(ctx context.Context, serverName string) ([]*ToolSignature, error)
	FindToolByName(ctx context.Context, toolName string) ([]ServerToolInfo, error)

	// Tool calling
	CallTool(ctx context.Context, serverName, toolName string, args map[string]interface{}) (*ToolCallResult, error)
	CallToolAnyServer(ctx context.Context, toolName string, args map[string]interface{}) (*ToolCallResult, error)
	CallToolWithRetry(ctx context.Context, serverName, toolName string, args map[string]interface{}, maxRetries int) (*ToolCallResult, error)
}

ToolManager defines the interface for tool operations

type ToolParameter

type ToolParameter struct {
	Name        string      // Parameter name
	Type        string      // Parameter type (string, integer, number, boolean, array, object)
	Description string      // Parameter description
	Required    bool        // Whether required
	Default     interface{} // Default value
	Enum        []string    // Enum values
}

type ToolSignature

type ToolSignature struct {
	Name        string           // Tool name
	Description string           // Tool description
	Parameters  []*ToolParameter // Parameter list
	Required    []string         // Required parameter name list
}

func GetToolSignature

func GetToolSignature(tool *mcpSdk.Tool) *ToolSignature

GetToolSignature extracts the parameter signature from a tool

Jump to

Keyboard shortcuts

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