transport

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package transport provides transport implementations for MCP protocol.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildEnv

func BuildEnv(configEnv map[string]string) []string

BuildEnv creates an environment slice by merging the current environment with additional variables from configEnv.

func ExpandEnv

func ExpandEnv(s string) string

ExpandEnv expands environment variables in a string. Supports ${VAR} and ${VAR:-default} syntax.

func ExpandEnvMap

func ExpandEnvMap(m map[string]string) map[string]string

ExpandEnvMap expands environment variables in each value of a map.

func ExpandEnvSlice

func ExpandEnvSlice(s []string) []string

ExpandEnvSlice expands environment variables in each string of a slice.

func ParseAndDispatchNotification

func ParseAndDispatchNotification(data []byte, handler NotificationHandler) bool

ParseAndDispatchNotification parses a JSON message and dispatches it to the handler if it's a notification. Returns true if it was a notification, false otherwise.

Types

type HTTPConfig

type HTTPConfig struct {
	URL     string
	Headers map[string]string
}

HTTPConfig contains configuration for HTTP transport

type HTTPTransport

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

HTTPTransport implements Transport for HTTP-based MCP servers.

func NewHTTPTransport

func NewHTTPTransport(config HTTPConfig) *HTTPTransport

NewHTTPTransport creates a new HTTP transport

func (*HTTPTransport) Close

func (t *HTTPTransport) Close() error

Close closes the transport

func (*HTTPTransport) IsAlive

func (t *HTTPTransport) IsAlive() bool

IsAlive returns true if the transport is connected

func (*HTTPTransport) Send

Send sends a request and waits for response

func (*HTTPTransport) SendNotification

func (t *HTTPTransport) SendNotification(ctx context.Context, notif *JSONRPCNotification) error

SendNotification sends a notification (no response expected)

func (*HTTPTransport) SetNotificationHandler

func (t *HTTPTransport) SetNotificationHandler(_ NotificationHandler)

SetNotificationHandler is a no-op for HTTP transport (stateless, no server-push)

func (*HTTPTransport) Start

func (t *HTTPTransport) Start(ctx context.Context) error

Start initializes the HTTP transport

type JSONRPCError

type JSONRPCError struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Data    json.RawMessage `json:"data,omitempty"`
}

JSONRPCError represents a JSON-RPC 2.0 error

type JSONRPCNotification

type JSONRPCNotification struct {
	JSONRPC string      `json:"jsonrpc"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

JSONRPCNotification represents a JSON-RPC 2.0 notification (no ID)

type JSONRPCRequest

type JSONRPCRequest struct {
	JSONRPC string      `json:"jsonrpc"`
	ID      uint64      `json:"id"`
	Method  string      `json:"method"`
	Params  interface{} `json:"params,omitempty"`
}

JSONRPCRequest represents a JSON-RPC 2.0 request

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      uint64          `json:"id,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *JSONRPCError   `json:"error,omitempty"`
}

JSONRPCResponse represents a JSON-RPC 2.0 response

type NotificationHandler

type NotificationHandler func(method string, params []byte)

NotificationHandler handles incoming notifications from the MCP server

type SSEConfig

type SSEConfig struct {
	URL     string
	Headers map[string]string
}

SSEConfig contains configuration for SSE transport

type SSETransport

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

SSETransport implements Transport for SSE-based MCP servers. This implements the deprecated SSE transport for backward compatibility.

func NewSSETransport

func NewSSETransport(config SSEConfig) *SSETransport

NewSSETransport creates a new SSE transport

func (*SSETransport) Close

func (t *SSETransport) Close() error

Close closes the transport

func (*SSETransport) IsAlive

func (t *SSETransport) IsAlive() bool

IsAlive returns true if the transport is connected

func (*SSETransport) Send

Send sends a request and waits for response

func (*SSETransport) SendNotification

func (t *SSETransport) SendNotification(ctx context.Context, notif *JSONRPCNotification) error

SendNotification sends a notification (no response expected)

func (*SSETransport) SetNotificationHandler

func (t *SSETransport) SetNotificationHandler(handler NotificationHandler)

SetNotificationHandler sets the handler for incoming notifications

func (*SSETransport) Start

func (t *SSETransport) Start(ctx context.Context) error

Start initializes the SSE transport

type STDIOConfig

type STDIOConfig struct {
	Command string
	Args    []string
	Env     map[string]string
}

STDIOConfig contains configuration for STDIO transport

type STDIOTransport

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

STDIOTransport implements Transport for STDIO-based MCP servers. It spawns a subprocess and communicates via stdin/stdout.

func NewSTDIOTransport

func NewSTDIOTransport(config STDIOConfig) *STDIOTransport

NewSTDIOTransport creates a new STDIO transport

func (*STDIOTransport) Close

func (t *STDIOTransport) Close() error

Close terminates the subprocess and cleans up

func (*STDIOTransport) IsAlive

func (t *STDIOTransport) IsAlive() bool

IsAlive returns true if the transport is connected

func (*STDIOTransport) Send

Send sends a request and waits for response

func (*STDIOTransport) SendNotification

func (t *STDIOTransport) SendNotification(ctx context.Context, notif *JSONRPCNotification) error

SendNotification sends a notification (no response expected)

func (*STDIOTransport) SetNotificationHandler

func (t *STDIOTransport) SetNotificationHandler(handler NotificationHandler)

SetNotificationHandler sets the handler for incoming notifications

func (*STDIOTransport) Start

func (t *STDIOTransport) Start(ctx context.Context) error

Start spawns the subprocess and establishes communication

type Transport

type Transport interface {
	// Start initializes and starts the transport connection.
	Start(ctx context.Context) error

	// Send sends a JSON-RPC request and waits for a response.
	// The context can be used to cancel the request.
	Send(ctx context.Context, req *JSONRPCRequest) (*JSONRPCResponse, error)

	// SendNotification sends a JSON-RPC notification (no response expected).
	SendNotification(ctx context.Context, notif *JSONRPCNotification) error

	// Close closes the transport connection and releases resources.
	Close() error

	// IsAlive returns true if the transport connection is still active.
	IsAlive() bool

	// SetNotificationHandler sets a handler for incoming notifications from the server.
	SetNotificationHandler(handler NotificationHandler)
}

Transport defines the interface for MCP transport implementations. All transports must be able to send JSON-RPC requests and receive responses.

Jump to

Keyboard shortcuts

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