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 ¶
- Variables
- func RegisterResource[I any](registry *Registry, resource schema.Resource, ...)
- func RegisterTool[I any, O any](registry *Registry, name, description string, ...) error
- type DefaultHandler
- func (d *DefaultHandler) CallTool(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.CallToolRequest]) (*schema.CallToolResult, *jsonrpc.Error)
- func (d *DefaultHandler) CanElicit() bool
- func (d *DefaultHandler) CanSample() bool
- func (d *DefaultHandler) Complete(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.CompleteRequest]) (*schema.CompleteResult, *jsonrpc.Error)
- func (d *DefaultHandler) Discover(_ context.Context, result *schema.DiscoverResult)
- func (d *DefaultHandler) GetElicitationCapability() *schema.ElicitationCapability
- func (d *DefaultHandler) GetPrompt(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.GetPromptRequest]) (*schema.GetPromptResult, *jsonrpc.Error)
- func (d *DefaultHandler) GetSamplingCapability() *schema.SamplingCapability
- func (d *DefaultHandler) GetSkill(ctx context.Context, request *jsonrpc.TypedRequest[*schema.GetSkillRequest]) (*schema.GetSkillResult, *jsonrpc.Error)
- func (d *DefaultHandler) Implements(method string) bool
- func (d *DefaultHandler) Initialize(ctx context.Context, init *schema.InitializeRequestParams, ...)
- func (d *DefaultHandler) ListPrompts(ctx context.Context, ...) (*schema.ListPromptsResult, *jsonrpc.Error)
- func (d *DefaultHandler) ListResourceTemplates(ctx context.Context, ...) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
- func (d *DefaultHandler) ListResources(ctx context.Context, ...) (*schema.ListResourcesResult, *jsonrpc.Error)
- func (d *DefaultHandler) ListSkills(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListSkillsRequest]) (*schema.ListSkillsResult, *jsonrpc.Error)
- func (d *DefaultHandler) ListTools(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.ListToolsRequest]) (*schema.ListToolsResult, *jsonrpc.Error)
- func (d *DefaultHandler) OnNotification(ctx context.Context, notification *jsonrpc.Notification)
- func (d *DefaultHandler) ReadResource(ctx context.Context, ...) (*schema.ReadResourceResult, *jsonrpc.Error)
- func (d *DefaultHandler) ReadResourceDirectory(ctx context.Context, ...) (*schema.ReadResourceDirectoryResult, *jsonrpc.Error)
- func (d *DefaultHandler) Subscribe(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.SubscribeRequest]) (*schema.SubscribeResult, *jsonrpc.Error)
- func (d *DefaultHandler) SupportsElicitationMode(mode string) bool
- func (d *DefaultHandler) Unsubscribe(ctx context.Context, ...) (*schema.UnsubscribeResult, *jsonrpc.Error)
- type Handler
- type NewHandler
- type Operations
- type Option
- type PromptEntry
- type PromptHandlerFunc
- type Prompts
- type Registry
- func (r *Registry) ImplementsSkills() bool
- func (d *Registry) ListRegisteredResourceTemplates() []schema.ResourceTemplate
- func (d *Registry) ListRegisteredResources() []schema.Resource
- func (r *Registry) ListRegisteredSkills() []schema.Skill
- func (d *Registry) ListRegisteredTools() []schema.Tool
- func (d *Registry) ListToolsFor(ctx context.Context) ([]schema.Tool, error)
- func (r *Registry) ReadStaticSkillResource(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error, bool)
- func (d *Registry) RegisterPrompts(prompt *schema.Prompt, handler PromptHandlerFunc)
- func (d *Registry) RegisterResource(resource schema.Resource, handler ResourceHandlerFunc) error
- func (d *Registry) RegisterResourceTemplate(template schema.ResourceTemplate, handler ResourceHandlerFunc)
- func (r *Registry) RegisterSkill(entry schema.Skill) error
- func (r *Registry) RegisterStaticSkill(compiled *skillformat.Static) error
- func (d *Registry) RegisterTool(entry *ToolEntry)
- func (d *Registry) RegisterToolWithSchema(name, description string, inputSchema schema.ToolInputSchema, ...)
- type ResourceDirectories
- type ResourceEntry
- type ResourceHandlerFunc
- type ResourceTemplateEntry
- type Skills
- type ToolEntry
- type ToolHandlerFunc
- type Tools
Constants ¶
This section is empty.
Variables ¶
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.
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
// SkillsCacheTTL and SkillsCacheScope are the caching hints (ttlMs and
// cacheScope) that skills/list and skills/get results carry in 2026-07-28.
// Left unset, clients get no freshness (0 ms) and private scope, so they
// fetch the metadata again each time they need it.
SkillsCacheTTL time.Duration
SkillsCacheScope schema.CacheableResultCacheScope
// SkillsDirectoryRead turns resources/directory/read (SEP-2640) on or off
// for the skills this handler registers. Unset, it is on while every
// skill is compiled (RegisterStaticSkill) and off once a dynamic skill is
// registered. Its listings carry the skills cache hints above. See
// ReadResourceDirectory for what the default lists.
SkillsDirectoryRead *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 ¶
func (d *DefaultHandler) CallTool(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.CallToolRequest]) (*schema.CallToolResult, *jsonrpc.Error)
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 ¶
func (d *DefaultHandler) Complete(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.CompleteRequest]) (*schema.CompleteResult, *jsonrpc.Error)
Complete returns method-not-found by default.
func (*DefaultHandler) Discover ¶
func (d *DefaultHandler) Discover(_ context.Context, result *schema.DiscoverResult)
Discover populates July server capabilities without retaining client state.
func (*DefaultHandler) GetElicitationCapability ¶
func (d *DefaultHandler) GetElicitationCapability() *schema.ElicitationCapability
GetElicitationCapability returns the client's elicitation capability, or nil if the client didn't declare one. Per the spec, declaring the capability object means elicitation is supported; its optional "form" and "url" members name the supported modes, and an empty object means form mode only (for compatibility with clients predating modes).
func (*DefaultHandler) GetPrompt ¶
func (d *DefaultHandler) GetPrompt(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.GetPromptRequest]) (*schema.GetPromptResult, *jsonrpc.Error)
GetPrompt returns the result of a prompt call.
func (*DefaultHandler) GetSamplingCapability ¶
func (d *DefaultHandler) GetSamplingCapability() *schema.SamplingCapability
GetSamplingCapability returns the client's sampling capability, or nil if the client didn't declare one. Per the spec, declaring the capability object means sampling is supported; its optional "tools" member means tool-enabled sampling is supported.
func (*DefaultHandler) GetSkill ¶
func (d *DefaultHandler) GetSkill(ctx context.Context, request *jsonrpc.TypedRequest[*schema.GetSkillRequest]) (*schema.GetSkillResult, *jsonrpc.Error)
func (*DefaultHandler) Implements ¶
func (d *DefaultHandler) Implements(method string) bool
Implements returns true for supported methods.
func (*DefaultHandler) Initialize ¶
func (d *DefaultHandler) Initialize(ctx context.Context, init *schema.InitializeRequestParams, result *schema.InitializeResult)
Initialize stores the initialization parameters.
func (*DefaultHandler) ListPrompts ¶
func (d *DefaultHandler) ListPrompts(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.ListPromptsRequest]) (*schema.ListPromptsResult, *jsonrpc.Error)
ListPrompts lists all registered prompts on this DefaultHandler.
func (*DefaultHandler) ListResourceTemplates ¶
func (d *DefaultHandler) ListResourceTemplates(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListResourceTemplatesRequest]) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
ListResourceTemplates returns method-not-found by default.
func (*DefaultHandler) ListResources ¶
func (d *DefaultHandler) ListResources(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListResourcesRequest]) (*schema.ListResourcesResult, *jsonrpc.Error)
ListResources returns method-not-found by default.
func (*DefaultHandler) ListSkills ¶
func (d *DefaultHandler) ListSkills(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListSkillsRequest]) (*schema.ListSkillsResult, *jsonrpc.Error)
func (*DefaultHandler) ListTools ¶
func (d *DefaultHandler) ListTools(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.ListToolsRequest]) (*schema.ListToolsResult, *jsonrpc.Error)
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 ¶
func (d *DefaultHandler) ReadResource(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.ReadResourceRequest]) (*schema.ReadResourceResult, *jsonrpc.Error)
ReadResource returns method-not-found by default.
func (*DefaultHandler) ReadResourceDirectory ¶
func (d *DefaultHandler) ReadResourceDirectory(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ReadResourceDirectoryRequest]) (*schema.ReadResourceDirectoryResult, *jsonrpc.Error)
ReadResourceDirectory lists the direct children of a directory inside a registered skill: the skill root (skill://pdf-processing) or any directory below it (skill://pdf-processing/templates). The listing comes from the resources registered for the skills, so it covers a static skill completely. A dynamic skill's files are listed only if they are registered resources, so a handler with dynamic skills has directory reads off unless it sets SkillsDirectoryRead to true (all its files are registered) or overrides this method.
func (*DefaultHandler) Subscribe ¶
func (d *DefaultHandler) Subscribe(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.SubscribeRequest]) (*schema.SubscribeResult, *jsonrpc.Error)
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 ¶
func (d *DefaultHandler) Unsubscribe(ctx context.Context, jRequest *jsonrpc.TypedRequest[*schema.UnsubscribeRequest]) (*schema.UnsubscribeResult, *jsonrpc.Error)
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 ¶
type Operations interface {
Initialize(ctx context.Context, init *schema.InitializeRequestParams, result *schema.InitializeResult)
ListResources(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListResourcesRequest]) (*schema.ListResourcesResult, *jsonrpc.Error)
ListResourceTemplates(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListResourceTemplatesRequest]) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
ReadResource(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ReadResourceRequest]) (*schema.ReadResourceResult, *jsonrpc.Error)
Subscribe(ctx context.Context, request *jsonrpc.TypedRequest[*schema.SubscribeRequest]) (*schema.SubscribeResult, *jsonrpc.Error)
Unsubscribe(ctx context.Context, request *jsonrpc.TypedRequest[*schema.UnsubscribeRequest]) (*schema.UnsubscribeResult, *jsonrpc.Error)
ListTools(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListToolsRequest]) (*schema.ListToolsResult, *jsonrpc.Error)
CallTool(ctx context.Context, request *jsonrpc.TypedRequest[*schema.CallToolRequest]) (*schema.CallToolResult, *jsonrpc.Error)
ListPrompts(ctx context.Context, request *jsonrpc.TypedRequest[*schema.ListPromptsRequest]) (*schema.ListPromptsResult, *jsonrpc.Error)
GetPrompt(ctx context.Context, request *jsonrpc.TypedRequest[*schema.GetPromptRequest]) (*schema.GetPromptResult, *jsonrpc.Error)
Complete(ctx context.Context, request *jsonrpc.TypedRequest[*schema.CompleteRequest]) (*schema.CompleteResult, *jsonrpc.Error)
}
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 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]
// contains filtered or unexported fields
}
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) ImplementsSkills ¶
func (*Registry) ListRegisteredResourceTemplates ¶
func (d *Registry) ListRegisteredResourceTemplates() []schema.ResourceTemplate
ListRegisteredResourceTemplates returns metadata for all registered resource templates on this handler.
func (*Registry) ListRegisteredResources ¶
ListRegisteredResources returns metadata for all registered resources on this handler.
func (*Registry) ListRegisteredSkills ¶
ListRegisteredSkills returns detached metadata only, never fetching content.
func (*Registry) ListRegisteredTools ¶
ListRegisteredTools returns metadata for all registered tools.
func (*Registry) ListToolsFor ¶
ListToolsFor resolves metadata using the current authenticated request context. Errors fail discovery closed; static metadata is never a fallback on denial.
func (*Registry) ReadStaticSkillResource ¶
func (r *Registry) ReadStaticSkillResource(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error, bool)
ReadStaticSkillResource claims the entire sealed root, including misses. This prevents an ordinary resource/template from adding unlisted files afterward.
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) error
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) RegisterStaticSkill ¶
func (r *Registry) RegisterStaticSkill(compiled *skillformat.Static) error
RegisterStaticSkill installs compiler-owned readers and inventory together. It never invokes an existing resource handler to infer bytes or completeness.
func (*Registry) RegisterTool ¶
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 ResourceDirectories ¶
type ResourceDirectories interface {
ReadResourceDirectory(context.Context, *jsonrpc.TypedRequest[*schema.ReadResourceDirectoryRequest]) (*schema.ReadResourceDirectoryResult, *jsonrpc.Error)
}
ResourceDirectories is optional. A handler that implements it serves SEP-2640's resources/directory/read.
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 Skills ¶
type Skills interface {
ListSkills(context.Context, *jsonrpc.TypedRequest[*schema.ListSkillsRequest]) (*schema.ListSkillsResult, *jsonrpc.Error)
GetSkill(context.Context, *jsonrpc.TypedRequest[*schema.GetSkillRequest]) (*schema.GetSkillResult, *jsonrpc.Error)
}
Skills is optional so existing ordinary-resource handlers remain compatible.
type ToolEntry ¶
type ToolEntry struct {
Handler ToolHandlerFunc
Metadata schema.Tool
// MetadataFor resolves caller-specific metadata without mutating the registration.
MetadataFor func(context.Context) (schema.Tool, error)
}
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.