streamhttp

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package streamhttp is LeanProxy's own MCP Streamable HTTP front end (issue #309): one shared local gateway that any number of MCP clients reach by URL, instead of every IDE spawning its own `server run --stdio` process with its own child servers.

It speaks the Streamable HTTP transport of the MCP revisions LeanProxy negotiates (2025-03-26, 2025-06-18, 2025-11-25) on a single endpoint:

  • POST carries one client message (or, for 2025-03-26 sessions, a batch). Requests are answered with application/json, or with a text/event-stream when server-to-client messages (progress, elicitation, ...) must go out before the answer; notifications and responses get 202 Accepted.
  • GET opens the session's server-initiated SSE stream (list changes, resource updates, and server-to-client requests with no request of their own).
  • DELETE ends the session.

Every message goes through the same *mcp.Handler — and so the same middleware chain (redaction, injection guard, response cache, tool pinning, per-tool policy, telemetry) — as the stdio front end. Each Mcp-Session-Id maps to one mcp.ClientSession: its negotiated protocol version, declared capabilities and server-to-client requests.

Security (see Options and docs/security.md): loopback bind by default and a refusal to bind elsewhere without a token; Host and Origin validation against DNS rebinding and cross-site requests (pkg/httpsec); bearer-token auth compared in constant time; unguessable session ids bound to the credential that created them, capped in number and expired when idle; request body, header, read and write limits; and a cap on the requests handled at once.

Resumability (Last-Event-ID) is not supported: events carry no id and a reconnecting GET stream starts afresh.

Index

Constants

View Source
const (
	HeaderSessionID       = "Mcp-Session-Id"
	HeaderProtocolVersion = "Mcp-Protocol-Version"
)

Transport headers.

View Source
const (
	DefaultEndpoint           = "/mcp"
	DefaultMaxBodyBytes       = 64 << 20
	DefaultMaxSessions        = 256
	DefaultSessionIdleTimeout = 30 * time.Minute
	DefaultMaxConcurrent      = 64
	DefaultReadHeaderTimeout  = 10 * time.Second
	DefaultReadTimeout        = 60 * time.Second
	DefaultWriteTimeout       = 30 * time.Second
	DefaultIdleTimeout        = 120 * time.Second
	DefaultHeartbeat          = 25 * time.Second
)

Defaults for the zero values of Options.

Variables

View Source
var ErrUnauthenticatedExposure = errors.New("refusing to serve MCP over HTTP on a non-loopback address without a bearer token")

ErrUnauthenticatedExposure is returned by New for a non-loopback Addr without a Token.

Functions

func CheckOptions

func CheckOptions(opts Options) error

CheckOptions validates the exposure settings of opts: a listen address, and a token unless it binds a loopback interface only.

Types

type Handler

type Handler interface {
	HandleRequest(ctx context.Context, req *mcp.Request) (*mcp.Response, error)
	OpenSession(notify mcp.NotifyFunc) (*mcp.ClientSession, func())
}

Handler is what the front end needs from *mcp.Handler.

type Options

type Options struct {
	// Addr is the "host:port" to listen on. Required.
	Addr string
	// Endpoint is the MCP endpoint path (DefaultEndpoint).
	Endpoint string
	// Token is the bearer token every request must present. Empty
	// disables authentication, which New only accepts on a loopback Addr.
	Token string
	// AllowedHosts are Host header values accepted beyond the bind host
	// and the loopback names (see httpsec.AllowedHosts).
	AllowedHosts []string
	// AllowedOrigins are the browser origins ("https://app.example")
	// allowed beyond the server's own. Requests without an Origin header
	// (every non-browser client) are not affected.
	AllowedOrigins []string
	// MaxBodyBytes caps one POST body.
	MaxBodyBytes int64
	// MaxSessions caps the sessions open at once. An initialize beyond it
	// ends the least recently active session that has nothing in flight;
	// it gets 503 only when every session is in use.
	MaxSessions int
	// SessionIdleTimeout ends a session with no request in flight and no
	// GET stream open for this long.
	SessionIdleTimeout time.Duration
	// MaxConcurrent caps the client requests handled at once, across
	// sessions; more wait for a slot.
	MaxConcurrent int
	// ReadHeaderTimeout, ReadTimeout (the whole POST body), WriteTimeout
	// (each write) and IdleTimeout (keep-alive) bound slow clients.
	ReadHeaderTimeout time.Duration
	ReadTimeout       time.Duration
	WriteTimeout      time.Duration
	IdleTimeout       time.Duration
	// Heartbeat is the interval of the SSE keep-alive comments on a GET
	// stream.
	Heartbeat time.Duration
	Logger    *slog.Logger
}

Options configures a Server. Zero values mean the defaults above.

type Server

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

Server is a running Streamable HTTP front end.

func New

func New(h Handler, opts Options) (*Server, error)

New validates opts and returns a Server that is not listening yet.

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the address the server listens on ("host:port").

func (*Server) Authenticated

func (s *Server) Authenticated() bool

Authenticated reports whether the server requires a bearer token.

func (*Server) SessionCount

func (s *Server) SessionCount() int

SessionCount returns the number of open sessions.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown ends every session (canceling their requests and closing their streams), then stops the HTTP server, waiting at most until ctx ends.

func (*Server) Start

func (s *Server) Start() error

Start listens on Addr and serves in the background.

func (*Server) URL

func (s *Server) URL() string

URL returns the MCP endpoint URL.

Jump to

Keyboard shortcuts

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