upstream

package
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorised     = errors.New("unauthorised")
	ErrNotFound         = errors.New("not found (404)")
	ErrMethodNotAllowed = errors.New("method not allowed (405)")
	ErrClosed           = errors.New("transport closed")
)

Transport errors.

Functions

This section is empty.

Types

type AuthProvider

type AuthProvider interface {
	// GetAccessToken returns the current access token.
	GetAccessToken(ctx context.Context) (string, error)

	// RefreshToken refreshes the access token.
	RefreshToken(ctx context.Context) error
}

AuthProvider provides OAuth tokens for authenticated requests.

type Config

type Config struct {
	ServerURL    string
	Headers      map[string]string
	AuthProvider AuthProvider
	Strategy     Strategy
}

Config holds transport configuration.

type Connection

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

Connection represents a connection to a single upstream MCP server.

func NewConnection

func NewConnection(config *types.UpstreamConfig, cacheDir string, callbackPort int) (*Connection, error)

NewConnection creates a new upstream connection.

func (*Connection) Close

func (c *Connection) Close() error

Close closes the connection.

func (*Connection) Connect

func (c *Connection) Connect(ctx context.Context) error

Connect establishes the connection to the upstream server.

func (*Connection) ExecuteTool

func (c *Connection) ExecuteTool(ctx context.Context, toolName string, args map[string]any) (*Message, error)

ExecuteTool executes a tool on the upstream server.

func (*Connection) FetchTools

func (c *Connection) FetchTools(ctx context.Context) error

FetchTools fetches the list of tools from the upstream server.

func (*Connection) GetTools

func (c *Connection) GetTools() []ToolInfo

GetTools returns the list of tools from this upstream.

func (*Connection) Port

func (c *Connection) Port() int

Port returns the OAuth callback port (needed for auth provider access).

type HTTPTransport

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

HTTPTransport implements the Streamable HTTP transport for MCP.

func NewHTTPTransport

func NewHTTPTransport(cfg *Config) *HTTPTransport

NewHTTPTransport creates a new HTTP transport.

func (*HTTPTransport) Close

func (t *HTTPTransport) Close() error

Close closes the HTTP transport.

func (*HTTPTransport) SendReceive

func (t *HTTPTransport) SendReceive(ctx context.Context, msg *Message) (*Message, error)

SendReceive sends a JSON-RPC message via HTTP POST and returns the response.

func (*HTTPTransport) Start

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

Start initialises the HTTP transport by verifying connectivity.

type Manager

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

Manager manages connections to multiple upstream MCP servers.

func NewManager

func NewManager(config *types.ProxyConfig) *Manager

NewManager creates a new upstream manager.

func (*Manager) Close

func (m *Manager) Close() error

Close closes all upstream connections.

func (*Manager) Connect

func (m *Manager) Connect(ctx context.Context) error

Connect establishes connections to all configured upstreams.

func (*Manager) ExecuteTool

func (m *Manager) ExecuteTool(ctx context.Context, toolName string, args map[string]any) (*Message, error)

ExecuteTool executes a tool on the appropriate upstream. The toolName should be in the format "upstream:tool" or just "tool" for single upstream.

func (*Manager) GetAllTools

func (m *Manager) GetAllTools() map[string][]ToolInfo

GetAllTools returns all tools from all upstreams.

func (*Manager) GetConnection

func (m *Manager) GetConnection(name string) (*Connection, error)

GetConnection returns the connection for a specific upstream by name.

type Message

type Message struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      any             `json:"id,omitempty"`
	Method  string          `json:"method,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *RPCError       `json:"error,omitempty"`
}

Message represents a JSON-RPC 2.0 message.

func (*Message) IsNotification

func (m *Message) IsNotification() bool

IsNotification returns true if the message is a notification.

func (*Message) IsRequest

func (m *Message) IsRequest() bool

IsRequest returns true if the message is a request.

func (*Message) IsResponse

func (m *Message) IsResponse() bool

IsResponse returns true if the message is a response.

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

RPCError represents a JSON-RPC error.

type SSETransport

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

SSETransport implements the Server-Sent Events transport for MCP. MCP SSE protocol flow: 1. Client opens GET request to establish SSE stream 2. Server sends "endpoint" event with URL for POSTing messages 3. Client sends messages via POST to the endpoint URL 4. Server sends responses via the SSE stream

func NewSSETransport

func NewSSETransport(cfg *Config) *SSETransport

NewSSETransport creates a new SSE transport.

func (*SSETransport) Close

func (t *SSETransport) Close() error

Close closes the SSE transport.

func (*SSETransport) SendReceive

func (t *SSETransport) SendReceive(ctx context.Context, msg *Message) (*Message, error)

SendReceive sends a JSON-RPC message via HTTP POST and waits for the response via SSE.

func (*SSETransport) Start

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

Start establishes the SSE connection and waits for the endpoint event. The ctx parameter is used for initial setup timeout only. The SSE connection itself uses a long-lived context to stay alive across requests.

type Strategy

type Strategy string

Strategy defines the transport selection strategy.

const (
	StrategyHTTPFirst Strategy = "http-first"
	StrategySSEFirst  Strategy = "sse-first"
	StrategyHTTPOnly  Strategy = "http-only"
	StrategySSEOnly   Strategy = "sse-only"
)

Transport strategy constants.

func ParseStrategy

func ParseStrategy(s string) Strategy

ParseStrategy parses a strategy string.

type ToolInfo

type ToolInfo struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	InputSchema json.RawMessage `json:"inputSchema,omitempty"`
}

ToolInfo holds information about a tool from an upstream server.

type Transport

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

	// SendReceive sends a JSON-RPC message and waits for the response.
	// This is a synchronous request/response operation.
	SendReceive(ctx context.Context, msg *Message) (*Message, error)

	// Close closes the transport connection.
	Close() error
}

Transport defines the interface for MCP transports.

Jump to

Keyboard shortcuts

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