Thanks to visit codestin.com
Credit goes to pkg.go.dev

transport

package
v0.27.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2025 License: MIT Imports: 17 Imported by: 20

Documentation

Index

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

func (*InProcessTransport) Start added in v0.23.0

func (c *InProcessTransport) Start(ctx context.Context) error

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 JSONRPCRequest struct {
	JSONRPC string `json:"jsonrpc"`
	ID      int64  `json:"id"`
	Method  string `json:"method"`
	Params  any    `json:"params,omitempty"`
}

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

func (c *SSE) Close() error

Close shuts down the SSE client connection and cleans up any pending responses. Returns an error if the shutdown process fails.

func (*SSE) GetBaseURL

func (c *SSE) GetBaseURL() *url.URL

GetBaseURL returns the base URL set in the SSE constructor.

func (*SSE) GetEndpoint

func (c *SSE) GetEndpoint() *url.URL

GetEndpoint returns the current endpoint URL for the SSE connection.

func (*SSE) SendNotification

func (c *SSE) SendNotification(ctx context.Context, notification mcp.JSONRPCNotification) error

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

func (*SSE) Start

func (c *SSE) Start(ctx context.Context) error

Start initiates the SSE connection to the server and waits for the endpoint information. Returns an error if the connection fails or times out waiting for the endpoint.

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

func NewStdio(
	command string,
	env []string,
	args ...string,
) *Stdio

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

func (c *Stdio) Close() error

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.

func (*Stdio) Start

func (c *Stdio) Start(ctx context.Context) error

func (*Stdio) Stderr

func (c *Stdio) Stderr() io.Reader

Stderr returns a reader for the stderr output of the subprocess. This can be used to capture error messages or logs from the subprocess.

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:

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

func (*StreamableHTTP) Start added in v0.22.0

func (c *StreamableHTTP) Start(ctx context.Context) error

Start initiates the HTTP connection to the server.

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.

Jump to

Keyboard shortcuts

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