mcp

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 23 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 mcp is the MCP frontend, built on the official Model Context Protocol Go SDK (github.com/modelcontextprotocol/go-sdk). It exposes every registered command as an MCP tool through the three transports (stdio / SSE / streamable HTTP) and lets the caller pin which protocol versions the server negotiates.

Output contract: every successful call returns the human-readable text (the same rendering the CLI frontend uses) in Content plus the raw value as JSON in StructuredContent; failures are reported as isError=true results carrying the classified error message.

Index

Constants

View Source
const (
	ProtocolV2024_11_05 = "2024-11-05"
	ProtocolV2025_03_26 = "2025-03-26"
	ProtocolV2025_06_18 = "2025-06-18"
	ProtocolV2025_11_25 = "2025-11-25"
	ProtocolV2026_07_28 = "2026-07-28" // latest
)

Protocol versions known to the official Go SDK at v1.7.0. Negotiation prefers the client's requested version when the server supports it; 2026-07-28 is the latest revision and deprecates the legacy initialize handshake in favor of mutually-negotiated version lists.

Variables

DefaultVersions is the full set the SDK can serve, in negotiation preference order (newest first).

Functions

func HTTPHandler

func HTTPHandler(reg *registry.Registry, opts Options) (http.Handler, error)

func Run

func Run(reg *registry.Registry, args []string) int

Run parses the transport and flags from args and serves until the transport ends:

mcp stdio [flags]
mcp sse   [flags]     # flags: --addr, --versions v1,v2, --name, --server-version
mcp http  [flags]     # streamable HTTP; adds --json-response, --stateless

It returns the process exit code.

func RunContext

func RunContext(ctx context.Context, reg *registry.Registry, args []string) int

RunContext is Run with an explicit context (graceful shutdown), both for the stdio server and the http/sse listeners.

func RunContextWithOptions

func RunContextWithOptions(ctx context.Context, reg *registry.Registry, args []string, base Options) int

RunContextWithOptions combines RunContext and RunWithOptions.

func RunWithOptions

func RunWithOptions(reg *registry.Registry, args []string, base Options) int

RunWithOptions is Run with preset options (e.g. bearer tokens injected by the dispatcher's --xyz.bearer). Command-line flags still win over presets.

func Server

func Server(reg *registry.Registry, opts Options) (*sdkmcp.Server, error)

Server builds a ready sdkmcp.Server with one tool per registered command. Tool input schemas come straight from the registry's JSON Schema generation; the shared Invoke pipeline does all decoding and validation.

Types

type Options

type Options struct {
	// Name and Version identify *the application* (this MCP server) to
	// clients: they become serverInfo.name / serverInfo.version and the
	// _meta.xyz.app_name / app_version on every call result.
	// Defaults: binary base name and "0.0.0".
	Name    string
	Version string

	// SDKVersion is the xyz library's own version, reported as
	// _meta.xyz.sdk_version (and the X-XYZ-Version header on the http/sse
	// transports). Distinct from Version, which is the application's.
	SDKVersion string

	// Versions restricts which protocol versions this server stands behind,
	// subset of DefaultVersions. Order is the preference order used during
	// negotiation. Empty means "all of them".
	Versions []string

	// Instructions is shown to clients after initialization.
	Instructions string

	// Addr is the listen address for the sse and http transports.
	// Default ":8080".
	Addr string

	// JSONResponse makes streamable HTTP answer with application/json
	// instead of text/event-stream (handy for debugging).
	JSONResponse bool

	// Stateless enables the streamable HTTP stateless mode (SEP-2567).
	Stateless bool

	// BearerTokens turns on Bearer-token verification for the http and sse
	// transports (stdio is local and unaffected). Empty means no auth.
	BearerTokens []string

	// SessionTimeout configures idle-session expiry for streamable HTTP
	// (the SDK's StreamableHTTPOptions.SessionTimeout). 0 keeps sessions.
	SessionTimeout time.Duration

	// CORSOrigins enables CORS for the http/sse transports ("*" = any origin).
	CORSOrigins []string

	// Defaults 是通道级默认参数(--default k=v):调用未显式提供时补上。
	Defaults map[string]string

	// ResponseHeaders 是随每次调用结果 _meta.xyz.headers 透出的自定义上下文
	//(与 HTTP 的 ResponseHeaders 同源)。空则不写 headers 键。
	ResponseHeaders map[string]string

	// NoServerMeta 置 true 时不写结果 _meta 里的 xyz 服务器上下文
	//(版本/命令/耗时/自定义头/错误上下文)。serverInfo.version 不受影响。
	NoServerMeta bool
}

Options configures the MCP frontend. The zero value serves every protocol version the SDK knows.

Jump to

Keyboard shortcuts

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