Help us shape the future of Go! Take the 2026 Go Developer Survey

server

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 29 Imported by: 1

Documentation

Overview

Package server hosts an authorized MCP registry through the official Go SDK. Compact discovery is the default; direct publication requires WithMode(Direct). The application owns listeners, identity, request authentication and calls to Shutdown.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppCSP

type AppCSP struct {
	ConnectDomains  []string `json:"connectDomains,omitempty"`
	ResourceDomains []string `json:"resourceDomains,omitempty"`
	FrameDomains    []string `json:"frameDomains,omitempty"`
	BaseURIDomains  []string `json:"baseUriDomains,omitempty"`
}

AppCSP declares browser origins. Empty lists request no external access. Origins use http/https (also ws/wss for connections), optional ports and an optional leading *. subdomain. Paths, credentials and CSP expressions fail. Hosts enforce this policy and may impose further restrictions.

type AppPermissions

type AppPermissions struct {
	Camera         bool `json:"camera"`
	Microphone     bool `json:"microphone"`
	Geolocation    bool `json:"geolocation"`
	ClipboardWrite bool `json:"clipboardWrite"`
}

AppPermissions requests browser features; the host may refuse any request. Applications must detect availability before use.

type Apps

type Apps struct {
	URI         string         `json:"uri"`
	HTML        string         `json:"html"`
	Views       []string       `json:"views"`
	CSP         AppCSP         `json:"csp"`
	Permissions AppPermissions `json:"permissions"`
}

Apps supplies one public, static HTML5 shell for the compact dispatcher. The host owns HTML correctness and must exclude private data and hidden catalogues. URI is an exclusive, byte-exact Apps mount; do not reuse it in WithResources. Views lists the approved presentation identifiers. HTML is bounded to 8 MiB; the remaining encoded configuration is bounded to 64 KiB.

type Mode

type Mode string

Mode fixes how operations are published. The zero value resolves to Compact.

const (
	// Compact publishes only the search, inspection and invocation facade.
	Compact Mode = "compact"
	// Direct publishes authorized operations as individual native tools.
	Direct Mode = "direct"
)

type Option

type Option func(*configuration) error

Option fixes server configuration at construction. Repeated settings fail.

func WithApps

func WithApps(value Apps) Option

WithApps snapshots configuration when called and enables the static Apps shell. Apps currently requires Compact mode. The shell is linked from call_tool and omitted from resources/list. It is public to anyone admitted by host middleware; operation and application-resource authorization still runs on every request.

func WithIdentity

func WithIdentity(name, version string) Option

WithIdentity sets the advertised server name and version. Their complete JSON representation must fit 4 KiB, leaving room in the final response allowance.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger supplies the host's SDK logger. It must not write to protocol stdout.

func WithMode

func WithMode(mode Mode) Option

WithMode selects an immutable publication view. Separate servers may share a registry; discovery never changes their modes or the registry's capacity.

func WithResources

func WithResources(resources *mcp.Resources) Option

WithResources attaches an explicit resource catalogue with its own host policy. Omitting it leaves resource capabilities unadvertised unless Apps is enabled.

func WithStdio

func WithStdio(reader io.ReadCloser, writer io.WriteCloser) Option

WithStdio selects streams for RunStdio. Once RunStdio starts, the SDK owns and closes them. Reader.Close must unblock a pending Read. Defaults are stdin/stdout.

func WithTrustedOrigins

func WithTrustedOrigins(origins ...string) Option

WithTrustedOrigins names browser origins the HTTP handler accepts besides the request's own. Cross-origin requests are otherwise refused with 403, which is MCP's Origin rule; a non-browser client sends no Origin and is unaffected. Each origin is a scheme and host ("https://studio.example.com"), validated by the standard library's cross-origin protection. Repeating the option adds to the list.

type Server

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

Server is an immutable publication view with its own admission and lifetime. Obtain Handler without starting a listener. Shutdown stops admission permanently.

func New

func New(registry *mcp.Registry, opts ...Option) (*Server, error)

New constructs an immutable publication view. Compact is the default. It starts no workers.

Example
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"net/http"

	mcp "gitlab.com/phpboyscout/go/mcp"
	"gitlab.com/phpboyscout/go/mcp/server"
)

func main() {
	registry, err := mcp.New([]mcp.Operation{{
		Definition: mcp.Definition{Name: "project.status", Summary: "Read project status", InputSchema: json.RawMessage(`{"type":"object","additionalProperties":false}`)},
		Handle: func(context.Context, *mcp.Invocation) (mcp.Outcome, error) {
			result, err := mcp.TextResult("ready")
			return mcp.Finish(result), err
		},
	}}, mcp.WithPolicy(mcp.AllowRegistered()))
	if err != nil {
		panic(err)
	}
	host, err := server.New(registry, server.WithIdentity("project-tool", "0.1.0"))
	if err != nil {
		panic(err)
	}
	mux := http.NewServeMux()
	mux.Handle("/mcp", host.Handler())
	// The application supplies authentication, a listener and controls registration.
	// For a CLI, run host.RunStdio(ctx) instead of mounting an HTTP handler.
	fmt.Println(host.Status())
	fmt.Println(host.Shutdown(context.Background()))
}
Output:
<nil>
<nil>

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the protected SDK Streamable HTTP handler. Host middleware must authenticate requests and put current caller identity in the request context.

func (*Server) RunStdio

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

RunStdio serves one SDK session and owns the selected streams until it returns. It can run only once. Cancellation and Shutdown close the streams to unblock I/O.

func (*Server) Shutdown

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

Shutdown stops admission, invalidates this server's continuations, cancels active calls and waits within ctx's budget. A timeout does not mark work done.

func (*Server) Status

func (s *Server) Status() error

Status reports unfinished work after shutdown; a drained server is healthy.

Jump to

Keyboard shortcuts

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