Documentation
¶
Index ¶
- type ClientOption
- type InProcessTransport
- func (*InProcessTransport) Close() error
- func (c *InProcessTransport) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
- func (c *InProcessTransport) SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error)
- func (c *InProcessTransport) SetNotificationHandler(handler func(notification mcp.JSONRPCNotification))
- func (c *InProcessTransport) Start(ctx context.Context) error
- type Interface
- type JSONRPCRequest
- type JSONRPCResponse
- type SSE
- func (c *SSE) Close() error
- func (c *SSE) GetBaseURL() *url.URL
- func (c *SSE) GetEndpoint() *url.URL
- func (c *SSE) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
- func (c *SSE) SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error)
- func (c *SSE) SetNotificationHandler(handler func(notification mcp.JSONRPCNotification))
- func (c *SSE) Start(ctx context.Context) error
- type Stdio
- func (c *Stdio) Close() error
- func (c *Stdio) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
- func (c *Stdio) SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error)
- func (c *Stdio) SetNotificationHandler(handler func(notification mcp.JSONRPCNotification))
- func (c *Stdio) Start(ctx context.Context) error
- func (c *Stdio) Stderr() io.Reader
- type StreamableHTTP
- func (c *StreamableHTTP) Close() error
- func (c *StreamableHTTP) GetSessionId() string
- func (c *StreamableHTTP) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
- func (c *StreamableHTTP) SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error)
- func (c *StreamableHTTP) SetNotificationHandler(handler func(mcp.JSONRPCNotification))
- func (c *StreamableHTTP) Start(ctx context.Context) error
- type StreamableHTTPCOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ClientOption ¶
type ClientOption func(*SSE)
func WithHTTPClient ¶ added in v0.25.0
func WithHTTPClient(httpClient *http.Client) ClientOption
func WithHeaders ¶
func WithHeaders(headers map[string]string) ClientOption
type InProcessTransport ¶ added in v0.23.0
type InProcessTransport struct {
// contains filtered or unexported fields
}
func NewInProcessTransport ¶ added in v0.23.0
func NewInProcessTransport(server *server.MCPServer) *InProcessTransport
func (*InProcessTransport) Close ¶ added in v0.23.0
func (*InProcessTransport) Close() error
func (*InProcessTransport) SendNotification ¶ added in v0.23.0
func (c *InProcessTransport) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
func (*InProcessTransport) SendRequest ¶ added in v0.23.0
func (c *InProcessTransport) SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error)
func (*InProcessTransport) SetNotificationHandler ¶ added in v0.23.0
func (c *InProcessTransport) SetNotificationHandler(handler func(notification mcp.JSONRPCNotification))
type Interface ¶
type Interface interface { // Start the connection. Start should only be called once. Start(ctx context.Context) error // SendRequest sends a json RPC request and returns the response synchronously. SendRequest(ctx context.Context, request JSONRPCRequest) (*JSONRPCResponse, error) // SendNotification sends a json RPC Notification to the server. SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error // SetNotificationHandler sets the handler for notifications. // Any notification before the handler is set will be discarded. SetNotificationHandler(handler func(notification mcp.JSONRPCNotification)) // Close the connection. Close() error }
Interface for the transport layer.
type JSONRPCRequest ¶
type JSONRPCResponse ¶
type JSONRPCResponse struct { JSONRPC string `json:"jsonrpc"` ID *int64 `json:"id"` Result json.RawMessage `json:"result"` Error *struct { Code int `json:"code"` Message string `json:"message"` Data json.RawMessage `json:"data"` } `json:"error"` }
type SSE ¶
type SSE struct {
// contains filtered or unexported fields
}
SSE implements the transport layer of the MCP protocol using Server-Sent Events (SSE). It maintains a persistent HTTP connection to receive server-pushed events while sending requests over regular HTTP POST calls. The client handles automatic reconnection and message routing between requests and responses.
func NewSSE ¶
func NewSSE(baseURL string, options ...ClientOption) (*SSE, error)
NewSSE creates a new SSE-based MCP client with the given base URL. Returns an error if the URL is invalid.
func (*SSE) Close ¶
Close shuts down the SSE client connection and cleans up any pending responses. Returns an error if the shutdown process fails.
func (*SSE) GetBaseURL ¶
GetBaseURL returns the base URL set in the SSE constructor.
func (*SSE) GetEndpoint ¶
GetEndpoint returns the current endpoint URL for the SSE connection.
func (*SSE) SendNotification ¶
SendNotification sends a JSON-RPC notification to the server without expecting a response.
func (*SSE) SendRequest ¶
func (c *SSE) SendRequest( ctx context.Context, request JSONRPCRequest, ) (*JSONRPCResponse, error)
SendRequest sends a JSON-RPC request to the server and waits for a response. Returns the raw JSON response message or an error if the request fails.
func (*SSE) SetNotificationHandler ¶
func (c *SSE) SetNotificationHandler(handler func(notification mcp.JSONRPCNotification))
type Stdio ¶
type Stdio struct {
// contains filtered or unexported fields
}
Stdio implements the transport layer of the MCP protocol using stdio communication. It launches a subprocess and communicates with it via standard input/output streams using JSON-RPC messages. The client handles message routing between requests and responses, and supports asynchronous notifications.
func NewIO ¶ added in v0.24.0
func NewIO(input io.Reader, output io.WriteCloser, logging io.ReadCloser) *Stdio
NewIO returns a new stdio-based transport using existing input, output, and logging streams instead of spawning a subprocess. This is useful for testing and simulating client behavior.
func NewStdio ¶
NewStdio creates a new stdio transport to communicate with a subprocess. It launches the specified command with given arguments and sets up stdin/stdout pipes for communication. Returns an error if the subprocess cannot be started or the pipes cannot be created.
func (*Stdio) Close ¶
Close shuts down the stdio client, closing the stdin pipe and waiting for the subprocess to exit. Returns an error if there are issues closing stdin or waiting for the subprocess to terminate.
func (*Stdio) SendNotification ¶
func (c *Stdio) SendNotification( ctx context.Context, notification mcp.JSONRPCNotification, ) error
SendNotification sends a json RPC Notification to the server.
func (*Stdio) SendRequest ¶
func (c *Stdio) SendRequest( ctx context.Context, request JSONRPCRequest, ) (*JSONRPCResponse, error)
SendRequest sends a JSON-RPC request to the server and waits for a response. It creates a unique request ID, sends the request over stdin, and waits for the corresponding response or context cancellation. Returns the raw JSON response message or an error if the request fails.
func (*Stdio) SetNotificationHandler ¶
func (c *Stdio) SetNotificationHandler( handler func(notification mcp.JSONRPCNotification), )
SetNotificationHandler sets the handler function to be called when a notification is received. Only one handler can be set at a time; setting a new one replaces the previous handler.
type StreamableHTTP ¶ added in v0.22.0
type StreamableHTTP struct {
// contains filtered or unexported fields
}
StreamableHTTP implements Streamable HTTP transport.
It transmits JSON-RPC messages over individual HTTP requests. One message per request. The HTTP response body can either be a single JSON-RPC response, or an upgraded SSE stream that concludes with a JSON-RPC response for the same request.
https://modelcontextprotocol.io/specification/2025-03-26/basic/transports
The current implementation does not support the following features:
- batching
- continuously listening for server notifications when no request is in flight (https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#listening-for-messages-from-the-server)
- resuming stream (https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#resumability-and-redelivery)
- server -> client request
func NewStreamableHTTP ¶ added in v0.22.0
func NewStreamableHTTP(baseURL string, options ...StreamableHTTPCOption) (*StreamableHTTP, error)
NewStreamableHTTP creates a new Streamable HTTP transport with the given base URL. Returns an error if the URL is invalid.
func (*StreamableHTTP) Close ¶ added in v0.22.0
func (c *StreamableHTTP) Close() error
Close closes the all the HTTP connections to the server.
func (*StreamableHTTP) GetSessionId ¶ added in v0.22.0
func (c *StreamableHTTP) GetSessionId() string
func (*StreamableHTTP) SendNotification ¶ added in v0.22.0
func (c *StreamableHTTP) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error
func (*StreamableHTTP) SendRequest ¶ added in v0.22.0
func (c *StreamableHTTP) SendRequest( ctx context.Context, request JSONRPCRequest, ) (*JSONRPCResponse, error)
SendRequest sends a JSON-RPC request to the server and waits for a response. Returns the raw JSON response message or an error if the request fails.
func (*StreamableHTTP) SetNotificationHandler ¶ added in v0.22.0
func (c *StreamableHTTP) SetNotificationHandler(handler func(mcp.JSONRPCNotification))
type StreamableHTTPCOption ¶ added in v0.22.0
type StreamableHTTPCOption func(*StreamableHTTP)
func WithHTTPHeaders ¶ added in v0.22.0
func WithHTTPHeaders(headers map[string]string) StreamableHTTPCOption
func WithHTTPTimeout ¶ added in v0.22.0
func WithHTTPTimeout(timeout time.Duration) StreamableHTTPCOption
WithHTTPTimeout sets the timeout for a HTTP request and stream.