Documentation
¶
Index ¶
- Constants
- Variables
- func EnvStr(k, def string) string
- func MustJSON(v any) []byte
- type ClientHooks
- type DialOptions
- type MCPClient
- func (c *MCPClient) Call(ctx context.Context, method string, params any) (*RPCResponse, error)
- func (c *MCPClient) CallBatch(ctx context.Context, batch []RPCRequest) ([]RPCResponse, error)
- func (c *MCPClient) Close() error
- func (c *MCPClient) IsConnected() bool
- func (c *MCPClient) NextID() string
- func (c *MCPClient) Notify(ctx context.Context, method string, params any) error
- func (c *MCPClient) SetNotificationHandler(h NotificationHandler)
- func (c *MCPClient) ToolsCall(ctx context.Context, name string, params any) (json.RawMessage, error)
- func (c *MCPClient) ToolsList(ctx context.Context) (*ToolsListResult, error)
- type NotificationHandler
- type RPCError
- type RPCRequest
- type RPCResponse
- type Reconnector
- type SSETransport
- type StdioTransport
- type ToolInfo
- type ToolsListResult
- type Transport
- type TransportError
- type WebSocketTransport
Constants ¶
View Source
const (
ClientTransportClosedCode = -32099 // 客户端内部:传输关闭
)
View Source
const InternalDisconnectedMethod = "$transport/disconnected"
View Source
const JsonrpcVersion = "2.0"
Variables ¶
View Source
var ( // 可用于识别:传输层已关闭或不可用(可重试) ErrTransportClosed = errors.New("transport closed") )
Functions ¶
Types ¶
type ClientHooks ¶
type DialOptions ¶
type DialOptions struct {
// 通用
Headers http.Header
AuthToken string // 可选:Bearer
RequestTimeout time.Duration // 每次调用的默认超时
// 心跳(WS)
PingInterval time.Duration
PongWait time.Duration
// 回调(已有)
OnDisconnected func(error)
OnReconnected func()
// New hooks
OnMessage func([]byte) // 每条收到的消息(transport 层)
OnReconnectAttempt func(attempt int, backoff time.Duration) // 每次尝试重连时触发(用于 metrics / debug)
// TLS / Transport
InsecureSkipVerify bool
// SSE: 分离的发送与事件流地址
SSEEventsURL string // e.g. http(s)://host/mcp/events
// HTTPClient(可注入自定义传输/代理)
HTTPClient *http.Client
// 重试
MaxRetries int
// 重连策略
ReconnectInitialBackoff time.Duration // e.g. 500ms
ReconnectMaxBackoff time.Duration // e.g. 10s
// CancelCtx allows caller to cancel all reconnect attempts and in-flight connect requests.
CancelCtx context.Context
// PingFailureThreshold indicates how many consecutive ping write failures are tolerated before
// declaring the connection unhealthy and closing it to trigger a reconnect. Default: 3
PingFailureThreshold int
}
func (*DialOptions) WithDefaults ¶
func (o *DialOptions) WithDefaults() *DialOptions
type MCPClient ¶
type MCPClient struct {
// contains filtered or unexported fields
}
func NewMCPClient ¶
func NewMCPClient(t Transport, opts *DialOptions, hooks *ClientHooks) *MCPClient
func (*MCPClient) CallBatch ¶
func (c *MCPClient) CallBatch(ctx context.Context, batch []RPCRequest) ([]RPCResponse, error)
CallBatch:使用轮询等待响应,避免 goroutine 爆炸
func (*MCPClient) IsConnected ¶
func (*MCPClient) SetNotificationHandler ¶
func (c *MCPClient) SetNotificationHandler(h NotificationHandler)
type NotificationHandler ¶
type NotificationHandler func(method string, params json.RawMessage)
type RPCError ¶
type RPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data,omitempty"`
}
type RPCRequest ¶
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID *string `json:"id,omitempty"` // notification 时为 nil
Method string `json:"method"` // e.g. "tools/call", "tools/list"
Params *json.RawMessage `json:"params,omitempty"` // 任意 JSON
}
type RPCResponse ¶
type RPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID *string `json:"id,omitempty"`
Result *json.RawMessage `json:"result,omitempty"`
Error *RPCError `json:"error,omitempty"`
}
type Reconnector ¶
type Reconnector struct {
// contains filtered or unexported fields
}
func NewReconnector ¶
func NewReconnector(opts *DialOptions) *Reconnector
func (*Reconnector) Manage ¶
func (r *Reconnector) Manage(connectAndServe func(connected chan<- struct{}) error, recvC chan<- []byte, alive *atomic.Bool, stopCh <-chan struct{})
Manage runs a loop that calls connectAndServe; when connectAndServe signals 'connected' (by writing to the channel), Manage marks alive, calls OnReconnected, and waits until connectAndServe returns (connection closed). On errors it sends internal-notes and invokes OnDisconnected, then waits with backoff before retrying.
func (*Reconnector) WaitForReconnect ¶
func (r *Reconnector) WaitForReconnect(ctx context.Context) bool
WaitForReconnect waits until the next successful reconnect or ctx.Done(). Returns true if a reconnect happened.
type SSETransport ¶
type SSETransport struct {
// contains filtered or unexported fields
}
func NewSSETransport ¶
func NewSSETransport(postURL string, opts *DialOptions) (*SSETransport, error)
func (*SSETransport) Close ¶
func (t *SSETransport) Close() error
func (*SSETransport) IsConnected ¶
func (t *SSETransport) IsConnected() bool
func (*SSETransport) Recv ¶
func (t *SSETransport) Recv() <-chan []byte
type StdioTransport ¶
type StdioTransport struct {
// contains filtered or unexported fields
}
func NewStdioSubprocess ¶
func NewStdioSubprocess(serverPath string, args []string, opts *DialOptions) (*StdioTransport, error)
启动一个子进程,并使用其 stdio 作为传输层
func NewStdioTransport ¶
func NewStdioTransport(r io.Reader, w io.Writer, opts *DialOptions) *StdioTransport
func (*StdioTransport) Close ¶
func (t *StdioTransport) Close() error
func (*StdioTransport) IsConnected ¶
func (t *StdioTransport) IsConnected() bool
func (*StdioTransport) Recv ¶
func (t *StdioTransport) Recv() <-chan []byte
type ToolInfo ¶
type ToolInfo struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
InputSchema *json.RawMessage `json:"inputSchema,omitempty"`
}
type ToolsListResult ¶
type ToolsListResult struct {
Tools []ToolInfo `json:"tools"`
}
type TransportError ¶
type TransportError struct {
Op string // 读 / 写 / 连接
Err error // 原始错误
Temporary bool // 是否临时错误(建议重试)
}
TransportError 用于标识传输层错误(可能可重试)
func (*TransportError) Error ¶
func (e *TransportError) Error() string
func (*TransportError) Unwrap ¶
func (e *TransportError) Unwrap() error
type WebSocketTransport ¶
type WebSocketTransport struct {
// contains filtered or unexported fields
}
func NewWebSocketTransport ¶
func NewWebSocketTransport(urlStr string, opts *DialOptions) (*WebSocketTransport, error)
func (*WebSocketTransport) Close ¶
func (t *WebSocketTransport) Close() error
func (*WebSocketTransport) IsConnected ¶
func (t *WebSocketTransport) IsConnected() bool
func (*WebSocketTransport) Recv ¶
func (t *WebSocketTransport) Recv() <-chan []byte
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
mcpserver_for_test
command
|
|
|
sse_mcpc
command
|
|
|
stdio_mcpc
command
|
|
|
ws_mcpc
command
|
Click to show internal directories.
Click to hide internal directories.