server

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 8 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 server serves an mcp.Server over stdio, HTTP, or both.

HTTP transports require a caller-configured *http.Server. Its handler, address, timeouts, TLS settings, and hooks are used unchanged. A non-nil TLSConfig selects HTTPS. WithShutdownTimeout controls graceful shutdown.

A stateless streamable HTTP handler supports protocol 2026-07-28 and current elicitation. Older clients need stdio or a stateful handler for gated writes. Stateful multi-replica deployments need session affinity.

Without WithLogger, or with WithLogger(nil), lifecycle logs are discarded. Both runs stdio and HTTP concurrently; either transport stopping cancels the other. New defaults to Stdio and a 30-second shutdown timeout.

HTTP and Both require a server, handler, and valid address. Invalid configurations return ErrNoHTTPServer, ErrNilHandler, or ErrInvalidAddr. HTTPS requires caller-provided certificates.

Prefer StreamableHTTPOptions{Stateless: true, JSONResponse: true}. Stateless permits negotiation of protocol 2026-07-28; stateful limits it to 2025-11-25. JSONResponse does not affect negotiation. SDK list-result caching applies only to stateless sessions; tool-call results are not cached.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilServer        = errors.New("nil mcp server")
	ErrInvalidTransport = errors.New("invalid transport")
	ErrInvalidAddr      = errors.New("invalid server address")
	ErrServe            = errors.New("serve")
	ErrShutdown         = errors.New("shutdown")
	ErrNilHandler       = errors.New("nil http handler")
	ErrNoHTTPServer     = errors.New("no http server")
)

Sentinels for server failures, wrapped with detail at the entry point.

Functions

This section is empty.

Types

type Option

type Option func(*Server)

Option configures a Server.

func WithHTTPServer added in v0.2.0

func WithHTTPServer(srv *http.Server) Option

WithHTTPServer sets the caller-owned *http.Server for HTTP and Both.

func WithLogger added in v0.12.0

func WithLogger(l *slog.Logger) Option

WithLogger sets the lifecycle logger; nil disables logging.

func WithShutdownTimeout

func WithShutdownTimeout(d time.Duration) Option

WithShutdownTimeout sets the HTTP graceful-shutdown timeout (default 30s).

func WithTransport

func WithTransport(t Transport) Option

WithTransport sets the transport (default Stdio).

type Server

type Server struct {
	MCP *mcp.Server // Underlying MCP server.
	// contains filtered or unexported fields
}

Server serves an mcp.Server over the configured transport(s).

func New

func New(mcpServer *mcp.Server, opts ...Option) *Server

New builds a Server wrapping mcpServer (default: Stdio, 30s shutdown).

func (*Server) ListenAndServe

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

ListenAndServe validates, serves on the transport(s), and shuts down on ctx.

type Transport

type Transport string

Transport selects the MCP transport mechanism.

const (
	Stdio Transport = "stdio" // Stdio uses standard input and output.
	HTTP  Transport = "http"  // HTTP uses streamable HTTP.
	Both  Transport = "both"  // Both runs Stdio and HTTP concurrently.
)

Supported transport values.

func ParseTransport

func ParseTransport(s string) (Transport, error)

ParseTransport parses s, wrapping ErrInvalidTransport for a bad value.

func (*Transport) UnmarshalText

func (t *Transport) UnmarshalText(b []byte) error

UnmarshalText lets text config loaders (env, flag, json) parse a Transport.

Jump to

Keyboard shortcuts

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