server

package
v0.0.0-...-5dfb09d Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 6

Documentation

Overview

Package server contains the *protocol-level* abstractions that make up the “server side” of the Model Context Protocol (MCP).

The MCP specification itself calls this role “server”. Within the code we expose a Handler interface (plus DefaultHandler) to emphasise that this component handles already-decoded JSON-RPC requests, while a separate transport layer (in another module) is responsible for listening on HTTP, stdio, WebSockets, etc.

A Handler typically embeds DefaultHandler and selectively overrides the Operations it needs. The package is intentionally transport-agnostic so it can be reused by different listener implementations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSamplingNotSupported is returned when the client doesn't support sampling
	ErrSamplingNotSupported = errors.New("client does not support sampling")
	// ErrElicitationNotSupported is returned when the client doesn't support elicitation
	ErrElicitationNotSupported = errors.New("client does not support elicitation")
)

Functions

func RegisterResource

func RegisterResource[I any](registry *Registry, resource schema.Resource, handler func(ctx context.Context, uri string) (*schema.ReadResourceResult, *jsonrpc.Error))

RegisterResource registers a resource using a typed handler that returns a Go struct. The struct will be JSON-marshaled into the ReadResourceResult.Contents field.

func RegisterTool

func RegisterTool[I any, O any](registry *Registry, name, description string, handler func(ctx context.Context, input I) (*schema.CallToolResult, *jsonrpc.Error)) error

RegisterTool derives JSON schemas from the generic I/O types and registers the tool.

Types

type DefaultHandler

type DefaultHandler struct {
	Notifier           transport.Notifier
	Logger             logger.Logger
	Client             client.Operations
	ClientInitialize   *schema.InitializeRequestParams
	Subscription       *syncmap.Map[string, bool]
	ServerCapabilities *schema.ServerCapabilities
	// Configuration for listChanged capabilities
	ToolsListChanged     *bool
	ResourcesListChanged *bool
	PromptsListChanged   *bool
	*Registry
}

DefaultHandler provides default implementations for server-side methods. You can embed this in your own Handler and register tools/resources via its helper methods.

func NewDefaultHandler

func NewDefaultHandler(notifier transport.Notifier, logger logger.Logger, client client.Operations) *DefaultHandler

NewDefaultHandler creates a new DefaultHandler with initialized registries. You can then call RegisterResource, RegisterTool, etc., on it before running the server.

func (*DefaultHandler) CallTool

CallTool returns method-not-found by default.

func (*DefaultHandler) CanElicit

func (d *DefaultHandler) CanElicit() bool

CanElicit checks if the client supports elicitation

func (*DefaultHandler) CanSample

func (d *DefaultHandler) CanSample() bool

CanSample checks if the client supports sampling

func (*DefaultHandler) Complete

Complete returns method-not-found by default.

func (*DefaultHandler) GetElicitationCapability

func (d *DefaultHandler) GetElicitationCapability() *schema.ElicitationCapability

GetElicitationCapability returns the client's elicitation capability if available

func (*DefaultHandler) GetPrompt

GetPrompt returns the result of a prompt call.

func (*DefaultHandler) GetSamplingCapability

func (d *DefaultHandler) GetSamplingCapability() *schema.SamplingCapability

GetSamplingCapability returns the client's sampling capability if available

func (*DefaultHandler) Implements

func (d *DefaultHandler) Implements(method string) bool

Implements returns true for supported methods.

func (*DefaultHandler) Initialize

Initialize stores the initialization parameters.

func (*DefaultHandler) ListPrompts

ListPrompts lists all registered prompts on this DefaultHandler.

func (*DefaultHandler) ListResourceTemplates

ListResourceTemplates returns method-not-found by default.

func (*DefaultHandler) ListResources

ListResources returns method-not-found by default.

func (*DefaultHandler) ListTools

ListTools returns method-not-found by default.

func (*DefaultHandler) OnNotification

func (d *DefaultHandler) OnNotification(ctx context.Context, notification *jsonrpc.Notification)

OnNotification is a no-op by default.

func (*DefaultHandler) ReadResource

ReadResource returns method-not-found by default.

func (*DefaultHandler) Subscribe

Subscribe adds the URI to the subscription map.

func (*DefaultHandler) SupportsElicitationMode

func (d *DefaultHandler) SupportsElicitationMode(mode string) bool

SupportsElicitationMode checks if the client supports the specified mode

func (*DefaultHandler) Unsubscribe

Unsubscribe removes the URI from the subscription map.

type Handler

type Handler interface {
	Operations

	OnNotification(ctx context.Context, notification *jsonrpc.Notification)

	// Implements checks if the method is implemented.
	Implements(method string) bool
}

Handler represents a protocol implementer.

type NewHandler

type NewHandler func(ctx context.Context, notifier transport.Notifier, logger logger.Logger, client client.Operations) (Handler, error)

NewHandler creates new handler implementer.

func WithDefaultHandler

func WithDefaultHandler(ctx context.Context, options ...Option) NewHandler

type Operations

Operations lists all JSON-RPC methods that an MCP handler may implement.

type Option

type Option func(server *DefaultHandler) error

Option can be supplied to WithDefaultHandler to mutate the handler before use.

type PromptEntry

type PromptEntry struct {
	Handler PromptHandlerFunc
	Prompt  *schema.Prompt
}

PromptEntry holds a handler with its metadata.

type PromptHandlerFunc

type PromptHandlerFunc func(ctx context.Context, request *schema.GetPromptRequestParams) (*schema.GetPromptResult, *jsonrpc.Error)

PromptHandlerFunc defines a function to handle a prompt call.

type Prompts

type Prompts []*PromptEntry

Prompts is a collection of PromptEntry.

type Registry

type Registry struct {
	ToolRegistry             *syncmap.Map[string, *ToolEntry]
	ResourceRegistry         *syncmap.Map[string, *ResourceEntry]
	ResourceTemplateRegistry *syncmap.Map[string, *ResourceTemplateEntry]
	Prompts                  *syncmap.Map[string, *PromptEntry]
	Methods                  *syncmap.Map[string, bool]
}

Registry holds registered tools, resources, prompts, etc. for a handler instance.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates and initialises an empty Registry.

func (*Registry) ListRegisteredResourceTemplates

func (d *Registry) ListRegisteredResourceTemplates() []schema.ResourceTemplate

ListRegisteredResourceTemplates returns metadata for all registered resource templates on this handler.

func (*Registry) ListRegisteredResources

func (d *Registry) ListRegisteredResources() []schema.Resource

ListRegisteredResources returns metadata for all registered resources on this handler.

func (*Registry) ListRegisteredTools

func (d *Registry) ListRegisteredTools() []schema.Tool

ListRegisteredTools returns metadata for all registered tools.

func (*Registry) RegisterPrompts

func (d *Registry) RegisterPrompts(prompt *schema.Prompt, handler PromptHandlerFunc)

RegisterPrompts registers a prompt on this handler.

func (*Registry) RegisterResource

func (d *Registry) RegisterResource(resource schema.Resource, handler ResourceHandlerFunc)

RegisterResource registers a resource with metadata and handler on this handler.

func (*Registry) RegisterResourceTemplate

func (d *Registry) RegisterResourceTemplate(template schema.ResourceTemplate, handler ResourceHandlerFunc)

RegisterResourceTemplate registers a resource template on this handler.

func (*Registry) RegisterTool

func (d *Registry) RegisterTool(entry *ToolEntry)

RegisterTool adds a prepared ToolEntry to the registry.

func (*Registry) RegisterToolWithSchema

func (d *Registry) RegisterToolWithSchema(name, description string, inputSchema schema.ToolInputSchema, outputSchema *schema.ToolOutputSchema, handler ToolHandlerFunc)

RegisterToolWithSchema registers a tool with an explicit JSON schema.

type ResourceEntry

type ResourceEntry struct {
	Handler  ResourceHandlerFunc
	Metadata schema.Resource
}

ResourceEntry holds a handler with its metadata.

type ResourceHandlerFunc

type ResourceHandlerFunc func(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error)

ResourceHandlerFunc defines a function to handle a resource read.

type ResourceTemplateEntry

type ResourceTemplateEntry struct {
	Metadata schema.ResourceTemplate
	Handler  ResourceHandlerFunc
}

ResourceTemplateEntry holds metadata for a resource template.

type ToolEntry

type ToolEntry struct {
	Handler  ToolHandlerFunc
	Metadata schema.Tool
}

ToolEntry holds a handler together with its public metadata.

type ToolHandlerFunc

type ToolHandlerFunc func(ctx context.Context, request *schema.CallToolRequest) (*schema.CallToolResult, *jsonrpc.Error)

ToolHandlerFunc defines a function to handle a tool call.

type Tools

type Tools []*ToolEntry

Tools is a collection of ToolEntry

Jump to

Keyboard shortcuts

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