mcp

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package mcp is a thin idiomatic Go wrapper around the mark3labs MCP SDK with forge-style typed tool registration: an MCP tool is defined by a Go struct (input) + Go struct (output) and a typed handler; the JSON Schema exposed to clients is generated from the input struct via reflection.

Typical use:

srv := mcp.New(mcp.Config{Name: "my-server", Version: "0.1"})
mcp.Register(srv, mcp.Tool[EchoIn, EchoOut]{
    Name:        "echo",
    Description: "Echoes its argument back.",
    Handler:     func(ctx context.Context, in EchoIn) (EchoOut, error) { ... },
})
_ = srv.Run(ctx)

Transports: stdio (default), HTTP. The package never imports kit/unreal/* or any project service so unrelated MCP projects (trading-bot, ops MCPs, unreal-mcp) can all share the harness.

Index

Constants

This section is empty.

Variables

View Source
var ErrHTTPClosed = errors.New("mcp: HTTP transport closed")

ErrHTTPClosed is returned by HTTP transport when the server stops because the context was cancelled. Wraps net/http's ErrServerClosed semantics so callers can branch cleanly.

View Source
var ErrSchemaUnsupported = errors.New("mcp: type not mappable to JSON Schema")

ErrSchemaUnsupported is returned by schema introspection helpers when a field's Go type can't be mapped to JSON Schema. The schema generator itself never returns this — it falls back to a generic object — but it's exported so callers can implement strict checks.

Functions

func Register

func Register[In, Out any](s *Server, t Tool[In, Out])

Register installs a typed Tool on a Server.

func SchemaFor

func SchemaFor[T any]() json.RawMessage

SchemaFor returns a JSON-Schema document describing the public fields of T (recursively). Field metadata is read from struct tags:

json:"name,omitempty" — sets the property name (and omitempty makes
                        the field optional).
desc:"…"            — human-readable description for the field.
required:"true"     — force-include in the schema's required list
                        regardless of omitempty.

Unsupported field types fall back to `{"type":"object"}` so the schema is always well-formed.

Types

type Config

type Config struct {
	// Name is the server name advertised to clients.
	Name string
	// Version is the server version advertised to clients.
	Version string
	// Transport selects the wire protocol. Defaults to Stdio.
	Transport Transport
}

Config configures a Server.

type Server

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

Server is a typed MCP server. Internally it owns a mark3labs/mcp-go MCPServer; the wrapper exists so consumers don't bind to mcp-go's API directly and so we can add forge concerns (typed registration, schema generation, lifecycle integration) without forking upstream.

func New

func New(cfg Config) *Server

New constructs a Server. Panics if Name is empty.

func (*Server) MCP

func (s *Server) MCP() *server.MCPServer

MCP returns the underlying mcp-go server for advanced callers that need direct access (custom hooks, resources, prompts). Most code shouldn't need this — register tools via the typed Register helper.

func (*Server) Run

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

Run starts serving over the configured transport until ctx is cancelled or the transport returns an error.

type Tool

type Tool[In, Out any] struct {
	// Name is the MCP tool identifier. Required.
	Name string
	// Description is shown to MCP clients (and to the LLM). Required.
	Description string
	// Handler runs the tool. Errors are mapped to MCP error responses.
	Handler func(ctx context.Context, in In) (Out, error)
}

Tool is a typed MCP tool. The input/output schemas exposed to clients are derived from In and Out via reflection (see SchemaFor). The handler is invoked with the decoded In and its returned Out is JSON-marshalled into a structured CallToolResult.

type Transport

type Transport interface {
	// contains filtered or unexported methods
}

Transport abstracts the wire on which the server speaks MCP. Implementations are produced by Stdio(), HTTP(), etc.

func HTTP

func HTTP(addr string) Transport

HTTP returns the streamable-HTTP transport bound to `addr`. The resulting server is shut down gracefully when the supplied context is cancelled.

func Stdio

func Stdio() Transport

Stdio returns the standard MCP transport — JSON-RPC framed over stdin/stdout. The default transport when no other is specified.

Jump to

Keyboard shortcuts

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