httpserve

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 20 Imported by: 0 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 httpserve serves the MCP server over local, stateless Streamable HTTP.

The server is for programs on the same computer. Each request goes through these checks, in this order:

  1. Host: the Host header must be localhost, 127.0.0.1 or [::1] with the port that the connection came in on, and the connection must have come in on a loopback address. This stops a web page that uses DNS rebinding (a name of the attacker that points to 127.0.0.1). Refused with 403.
  2. Origin: a request with an Origin header comes from a browser. It is refused with 403 unless its origin is in the allow-list, which is empty by default. A request without Origin passes. Programs send no Origin, but a browser also leaves it out of a GET navigation and of some "no-cors" loads. Such a request is still harmless here: a browser cannot add an Authorization header to it, and the stateless SDK handler answers only POST.
  3. Body size: a body of more than 15 MB is refused with 413.
  4. Bearer token: the Authorization header must carry a valid token of the tokens package. Refused with 401.

So a page that uses DNS rebinding gets 403 before any token is looked at. Only then does the request reach the MCP handler of the SDK, in stateless mode: the server keeps no session between requests.

Answers to refused requests have fixed texts. They never repeat a value of the request. The log gets a fixed code for each refusal by this package and for each token that the token check refused. A request without an Authorization header, or with one that is not "Bearer <token>", gets the SDK's own 401 and is not logged. The log never gets a token, the Authorization header or a request body.

Index

Constants

View Source
const MaxBodyBytes = 15 << 20

MaxBodyBytes is the largest request body: 15 MB. A message may have 10 MB of attachments, and base64 makes them a third larger.

Variables

View Source
var ErrNoToken = errors.New("httpserve: the request has no verified token")

ErrNoToken means that a tool call came without a verified token. Principal returns it.

Functions

func CheckLoopbackAddr

func CheckLoopbackAddr(addr string) error

CheckLoopbackAddr returns an error unless addr is a loopback address with a port: an IPv4 address in 127.0.0.0/8, [::1] or localhost, then a port from 0 to 65535. The HTTP server must not listen on any other address, such as 0.0.0.0, so "serve --http" calls this before it listens. The address must be written exactly: brackets only around an IPv6 address (so "[127.0.0.1]:8080" and "[localhost]:8080" are refused), and an IP address in its usual short form (so "[0:0:0:0:0:0:0:1]:8080" is refused).

func ErrorLog

func ErrorLog(l *slog.Logger, msg string) *log.Logger

ErrorLog returns a logger for http.Server.ErrorLog. net/http writes some of its own errors there (for example a broken TLS handshake or a bad request line), and such a text can hold a part of a request. Each line becomes one log line at Warn level with msg and the fixed code "http_server_error". The text is dropped.

func Handler

func Handler(newServer func() *mcp.Server, tm *tokens.Manager, allowedOrigins []string, opts ...Option) http.Handler

Handler returns the HTTP handler of the MCP server. See the package documentation for the checks and their order.

newServer returns the MCP server for a request. The SDK may call it more than once for one request, so it should return a server that already exists. tm checks the tokens. allowedOrigins lists the browser origins that may call the server, such as "http://localhost:5173". It is usually empty. Each entry must pass NormalizeOrigin, and is compared in its normalized form with the Origin header.

The port in the Host header is compared with the port of the connection's local address, which net/http puts in the request context (http.LocalAddrContextKey). So the handler needs no port setting, and it refuses every request that did not come through an http.Server on a loopback address.

The handler sends no CORS headers. A page on another origin must first send a CORS preflight (an OPTIONS request without the Authorization header). The preflight gets 401, so the browser never sends the real request. Only a page of the same origin as the server can use an allow-listed origin.

It panics when newServer or tm is nil, or when an entry of allowedOrigins is not valid (see NormalizeOrigin). All are mistakes in the code or the settings that set the program up, and the program must not start with them. A caller that reads origins from the user can check them with NormalizeOrigin first.

func LoopbackPort

func LoopbackPort(r *http.Request) (string, bool)

LoopbackPort returns the port of the connection that r came in on, as decimal text such as "8080". It reads the local address that net/http puts in the request context (http.LocalAddrContextKey). It returns false when the request did not come through an http.Server, or when the connection did not come in on a loopback address (see isLoopback).

A Host check compares the Host header with this port, so it needs no port setting and refuses a request that came in on any other address. The web package uses it for its own Host check.

func NormalizeOrigin

func NormalizeOrigin(origin string) (string, error)

NormalizeOrigin checks an entry of the origin allow-list and returns it in the form that a browser sends: "scheme://host" or "scheme://host:port", with the scheme and the host in lower case. It returns an error for "null", for an entry without a scheme or a host, and for an entry with a user, a path (even "/"), a query, a fragment, a port that is not a number, or white space. The error repeats the entry: it is a setting of the operator, not a value of a request.

func Principal

Principal finds the caller of a tool call that came over HTTP. It is the mcpserver.Options.Principal of the HTTP server.

It reads the TokenInfo that the token check put on the request: the UserID is the tenant and the scopes are the scopes of the token. A call without a TokenInfo or without a UserID is refused with ErrNoToken. The scopes are a new list that is never nil, so a token can never mean "no limit". The client name comes from the request. It is not trusted: the audit log cleans it.

Types

type Option

type Option func(*config)

Option changes a setting of Handler.

func WithLogger

func WithLogger(l *slog.Logger) Option

WithLogger sets the logger. It gets one line for each refused request and for a failed token check, with a fixed code. Without this option nothing is logged.

Jump to

Keyboard shortcuts

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