proxy

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HeaderMCPMethod mirrors the JSON-RPC `method` field. Required on all
	// requests from 2026-07-28 onwards.
	HeaderMCPMethod = "Mcp-Method"

	// HeaderMCPName mirrors `params.name` or `params.uri`. Required for
	// tools/call, prompts/get and resources/read.
	HeaderMCPName = "Mcp-Name"

	// MetaKeyProtocolVersion is the _meta key carrying the protocol version on
	// every request in the modern (per-request metadata) era.
	MetaKeyProtocolVersion = "io.modelcontextprotocol/protocolVersion"

	// ProtocolVersion20260728 is the first revision that drops the initialize
	// handshake and requires the standard request metadata headers.
	ProtocolVersion20260728 = "2026-07-28"
)

Request metadata mirroring for the Streamable HTTP transport.

Since MCP 2026-07-28 the transport mirrors selected JSON-RPC body fields into HTTP headers so intermediaries can route without parsing the body. Servers MUST reject a request whose headers disagree with its body, so the values here are always derived from the message actually being sent rather than from transport-level state.

https://modelcontextprotocol.io/specification/2026-07-28/basic/transports/streamable-http#request-metadata

View Source
const (
	// MCPProtocolVersion is the protocol version assumed until the local
	// client's own traffic tells us otherwise. It is the last revision that
	// used the initialize handshake, so it is the safe assumption for a client
	// that has not declared a version yet.
	MCPProtocolVersion = "2025-11-25"

	// HeaderMCPSessionID is the session ID header name.
	HeaderMCPSessionID = "Mcp-Session-Id"

	// HeaderMCPProtocolVersion is the protocol version header name.
	HeaderMCPProtocolVersion = "Mcp-Protocol-Version"
)
View Source
const HeaderWWWAuthenticate = "WWW-Authenticate"

HeaderWWWAuthenticate is the response header name carrying the Bearer challenge that points at the Protected Resource Metadata document (RFC 9728 §5.1).

Variables

This section is empty.

Functions

func ReadSSEEvents added in v0.2.0

func ReadSSEEvents(ctx context.Context, reader io.Reader, handler func(SSEEvent)) error

ReadSSEEvents reads SSE events from a reader and calls the handler for each complete event. It returns when the reader is exhausted or ctx is cancelled.

Types

type EventSource

type EventSource struct {

	// Callbacks
	OnOpen    func()
	OnMessage func(event string, data []byte)
	OnError   func(err error)
	// contains filtered or unexported fields
}

EventSource provides a client for Server-Sent Events (SSE)

func NewEventSource

func NewEventSource(request *http.Request, client *http.Client) *EventSource

NewEventSource creates a new SSE client

func (*EventSource) Close

func (es *EventSource) Close()

Close closes the SSE connection

func (*EventSource) Connect

func (es *EventSource) Connect() error

Connect establishes the SSE connection

func (*EventSource) IsConnected added in v0.3.0

func (es *EventSource) IsConnected() bool

IsConnected reports whether the stream is currently open. readEvents clears this when the stream ends, so it is written from another goroutine.

type Proxy

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

Proxy handles the bidirectional communication between stdio (MCP client) and the remote server

func NewProxy

func NewProxy(serverURL string, callbackPort int, headers map[string]string, serverURLHash string) (*Proxy, error)

NewProxy creates a new MCP proxy

func NewProxyWithOptions added in v0.2.0

func NewProxyWithOptions(serverURL string, callbackPort int, headers map[string]string, serverURLHash string, mode TransportMode, httpProxyURL string) (*Proxy, error)

NewProxyWithOptions creates a new MCP proxy with full configuration including HTTP proxy support

func NewProxyWithTransport added in v0.2.0

func NewProxyWithTransport(serverURL string, callbackPort int, headers map[string]string, serverURLHash string, mode TransportMode) (*Proxy, error)

NewProxyWithTransport creates a new MCP proxy with a specified transport mode

func (*Proxy) GetCommandEndpoint

func (p *Proxy) GetCommandEndpoint() string

GetCommandEndpoint returns the command endpoint URL (for backward compatibility in tests).

func (*Proxy) SetCommandEndpoint

func (p *Proxy) SetCommandEndpoint(endpoint string)

SetCommandEndpoint sets the command endpoint URL (for backward compatibility in tests).

func (*Proxy) SetStdio added in v0.2.0

func (p *Proxy) SetStdio(reader *bufio.Reader, writer *bufio.Writer)

SetStdio replaces the stdio reader and writer (for testing).

func (*Proxy) Shutdown

func (p *Proxy) Shutdown()

Shutdown gracefully stops the proxy

func (*Proxy) Start

func (p *Proxy) Start() error

Start initializes the proxy and begins bidirectional communication

type SSEEvent added in v0.2.0

type SSEEvent struct {
	Event string
	Data  []byte
	ID    string
}

SSEEvent represents a single Server-Sent Event

type SSETransport added in v0.2.0

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

SSETransport implements the legacy SSE transport (MCP 2024-11-05). It uses two endpoints: GET for SSE stream, POST for sending messages.

func NewSSETransport added in v0.2.0

func NewSSETransport(cfg SSETransportConfig) *SSETransport

NewSSETransport creates a new legacy SSE transport.

func (*SSETransport) Close added in v0.2.0

func (t *SSETransport) Close() error

func (*SSETransport) Connect added in v0.2.0

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

func (*SSETransport) Send added in v0.2.0

func (t *SSETransport) Send(ctx context.Context, message []byte) error

func (*SSETransport) SessionID added in v0.2.0

func (t *SSETransport) SessionID() string

func (*SSETransport) SetOnError added in v0.2.0

func (t *SSETransport) SetOnError(handler func(err error))

func (*SSETransport) SetOnMessage added in v0.2.0

func (t *SSETransport) SetOnMessage(handler func(event string, data []byte))

type SSETransportConfig added in v0.2.0

type SSETransportConfig struct {
	ServerURL    string
	Client       *http.Client
	Headers      map[string]string
	GetAuthToken func() string
}

SSETransportConfig holds configuration for creating an SSETransport.

type StreamableHTTPTransport added in v0.2.0

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

StreamableHTTPTransport implements the Streamable HTTP transport (MCP 2025-11-25). It uses a single endpoint for both POST (sending messages) and GET (receiving notifications).

func NewStreamableHTTPTransport added in v0.2.0

func NewStreamableHTTPTransport(cfg StreamableHTTPTransportConfig) *StreamableHTTPTransport

NewStreamableHTTPTransport creates a new Streamable HTTP transport.

func (*StreamableHTTPTransport) Close added in v0.2.0

func (t *StreamableHTTPTransport) Close() error

func (*StreamableHTTPTransport) Connect added in v0.2.0

func (t *StreamableHTTPTransport) Connect(ctx context.Context) error

func (*StreamableHTTPTransport) Send added in v0.2.0

func (t *StreamableHTTPTransport) Send(ctx context.Context, message []byte) error

func (*StreamableHTTPTransport) SessionID added in v0.2.0

func (t *StreamableHTTPTransport) SessionID() string

func (*StreamableHTTPTransport) SetOnError added in v0.2.0

func (t *StreamableHTTPTransport) SetOnError(handler func(err error))

func (*StreamableHTTPTransport) SetOnMessage added in v0.2.0

func (t *StreamableHTTPTransport) SetOnMessage(handler func(event string, data []byte))

type StreamableHTTPTransportConfig added in v0.2.0

type StreamableHTTPTransportConfig struct {
	Endpoint     string
	Client       *http.Client
	Headers      map[string]string
	GetAuthToken func() string

	// SkipNotificationStream suppresses the GET notification stream outright.
	// Set it only when the server cannot serve any revision that has one;
	// otherwise leave it false and let the transport decide from the client's
	// declared revision, since that is what determines whether the stream
	// exists for this connection.
	SkipNotificationStream bool
}

StreamableHTTPTransportConfig holds configuration for creating a StreamableHTTPTransport.

type Transport added in v0.2.0

type Transport interface {
	// Connect establishes the connection to the remote MCP server.
	Connect(ctx context.Context) error

	// Send sends a JSON-RPC message to the remote server.
	Send(ctx context.Context, message []byte) error

	// SetOnMessage sets the callback for messages received from the server.
	SetOnMessage(handler func(event string, data []byte))

	// SetOnError sets the callback for transport-level errors.
	SetOnError(handler func(err error))

	// Close terminates the transport connection.
	Close() error

	// SessionID returns the current session ID, if any.
	SessionID() string
}

Transport defines the interface for MCP transport implementations. Both the legacy SSE transport and the new Streamable HTTP transport implement this interface.

type TransportMode added in v0.2.0

type TransportMode string

TransportMode specifies which transport to use.

const (
	TransportModeAuto           TransportMode = "auto"
	TransportModeStreamableHTTP TransportMode = "streamable-http"
	TransportModeSSE            TransportMode = "sse"
)

type UnauthorizedError added in v0.2.2

type UnauthorizedError struct {
	StatusCode      int
	WWWAuthenticate string
}

UnauthorizedError signals an HTTP 401 from the MCP server and carries the WWW-Authenticate header so callers can locate the Protected Resource Metadata document per RFC 9728 §5.1.

func (*UnauthorizedError) Error added in v0.2.2

func (e *UnauthorizedError) Error() string

Jump to

Keyboard shortcuts

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