Documentation
¶
Overview ¶
Package server defines the server-side abstractions of the Model Context Protocol (MCP).
At its core sits the Operations interface that mirrors the set of JSON-RPC methods mandated by the specification (initialize, resources/list, tools/call …). Concrete servers implement Operations and may embed DefaultServer to inherit no-op stubs for every method, only overriding what they actually support.
Additional helpers make it easy to register new resources or tools and to spin up servers over different transports without coupling user code to a particular implementation.
Index ¶
- func RegisterResource[I any](registry *Registry, resource schema.Resource, ...)
- func RegisterTool[I any](registry *Registry, name string, description string, ...) error
- type DefaultServer
- func (d *DefaultServer) CallTool(ctx context.Context, request *schema.CallToolRequest) (*schema.CallToolResult, *jsonrpc.Error)
- func (d *DefaultServer) Complete(ctx context.Context, request *schema.CompleteRequest) (*schema.CompleteResult, *jsonrpc.Error)
- func (d *DefaultServer) GetPrompt(ctx context.Context, request *schema.GetPromptRequest) (*schema.GetPromptResult, *jsonrpc.Error)
- func (d *DefaultServer) Implements(method string) bool
- func (d *DefaultServer) Initialize(ctx context.Context, init *schema.InitializeRequestParams, ...)
- func (d *DefaultServer) ListPrompts(ctx context.Context, request *schema.ListPromptsRequest) (*schema.ListPromptsResult, *jsonrpc.Error)
- func (d *DefaultServer) ListResourceTemplates(ctx context.Context, request *schema.ListResourceTemplatesRequest) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
- func (d *DefaultServer) ListResources(ctx context.Context, request *schema.ListResourcesRequest) (*schema.ListResourcesResult, *jsonrpc.Error)
- func (d *DefaultServer) ListTools(ctx context.Context, request *schema.ListToolsRequest) (*schema.ListToolsResult, *jsonrpc.Error)
- func (d *DefaultServer) OnNotification(ctx context.Context, notification *jsonrpc.Notification)
- func (d *DefaultServer) ReadResource(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error)
- func (d *DefaultServer) Subscribe(ctx context.Context, request *schema.SubscribeRequest) (*schema.SubscribeResult, *jsonrpc.Error)
- func (d *DefaultServer) Unsubscribe(ctx context.Context, request *schema.UnsubscribeRequest) (*schema.UnsubscribeResult, *jsonrpc.Error)
- type NewServer
- type Operations
- type Option
- type PromptEntry
- type PromptHandlerFunc
- type Prompts
- type Registry
- func (d *Registry) ListRegisteredResourceTemplates() []schema.ResourceTemplate
- func (d *Registry) ListRegisteredResources() []schema.Resource
- func (d *Registry) ListRegisteredTools() []schema.Tool
- func (d *Registry) RegisterPrompts(prompt *schema.Prompt, handler PromptHandlerFunc)
- func (d *Registry) RegisterResource(resource schema.Resource, handler ResourceHandlerFunc)
- func (d *Registry) RegisterResourceTemplate(template schema.ResourceTemplate, handler ResourceHandlerFunc)
- func (d *Registry) RegisterTool(entry *ToolEntry)
- func (d *Registry) RegisterToolWithSchema(name string, description string, inputSchema schema.ToolInputSchema, ...)
- type ResourceEntry
- type ResourceHandlerFunc
- type ResourceTemplateEntry
- type ResourceTemplates
- type Resources
- type Server
- type ToolEntry
- type ToolHandlerFunc
- type Tools
Constants ¶
This section is empty.
Variables ¶
This section is empty.
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.Text field. RegisterResource registers a resource using a typed handler that returns a Go struct. The struct will be JSON-marshaled into the ReadResourceResult.Contents.
func RegisterTool ¶
func RegisterTool[I any](registry *Registry, name string, description string, handler func(ctx context.Context, input I) (*schema.CallToolResult, *jsonrpc.Error)) error
RegisterTool registers a tool on this Base by deriving its input schema from a struct type. Handler receives a typed input value and returns a CallToolResult.
Types ¶
type DefaultServer ¶ added in v0.3.9
type DefaultServer struct {
Notifier transport.Notifier
Logger logger.Logger
Client client.Operations
ClientInitialize *schema.InitializeRequestParams
Subscription *syncmap.Map[string, bool]
ServerCapabilities *schema.ServerCapabilities
*Registry
}
DefaultServer provides default implementations for server-side methods. You can embed this in your own Server and register tools/resources via its helper methods.
func NewDefaultServer ¶ added in v0.3.9
func NewDefaultServer(notifier transport.Notifier, logger logger.Logger, client client.Operations) *DefaultServer
NewDefaultServer creates a new DefaultServer with initialized registries. You can then call RegisterResource, RegisterTool, etc., on it before running the server.
func (*DefaultServer) CallTool ¶ added in v0.3.9
func (d *DefaultServer) CallTool(ctx context.Context, request *schema.CallToolRequest) (*schema.CallToolResult, *jsonrpc.Error)
CallTool returns method-not-found by default.
func (*DefaultServer) Complete ¶ added in v0.3.9
func (d *DefaultServer) Complete(ctx context.Context, request *schema.CompleteRequest) (*schema.CompleteResult, *jsonrpc.Error)
Complete returns method-not-found by default.
func (*DefaultServer) GetPrompt ¶ added in v0.3.9
func (d *DefaultServer) GetPrompt(ctx context.Context, request *schema.GetPromptRequest) (*schema.GetPromptResult, *jsonrpc.Error)
GetPrompt returns the result of a prompt call.
func (*DefaultServer) Implements ¶ added in v0.3.9
func (d *DefaultServer) Implements(method string) bool
Implements returns true for supported methods.
func (*DefaultServer) Initialize ¶ added in v0.3.9
func (d *DefaultServer) Initialize(ctx context.Context, init *schema.InitializeRequestParams, result *schema.InitializeResult)
Initialize stores the initialization parameters.
func (*DefaultServer) ListPrompts ¶ added in v0.3.9
func (d *DefaultServer) ListPrompts(ctx context.Context, request *schema.ListPromptsRequest) (*schema.ListPromptsResult, *jsonrpc.Error)
ListPrompts lists all registered prompts on this DefaultServer.
func (*DefaultServer) ListResourceTemplates ¶ added in v0.3.9
func (d *DefaultServer) ListResourceTemplates(ctx context.Context, request *schema.ListResourceTemplatesRequest) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
ListResourceTemplates returns method-not-found by default.
func (*DefaultServer) ListResources ¶ added in v0.3.9
func (d *DefaultServer) ListResources(ctx context.Context, request *schema.ListResourcesRequest) (*schema.ListResourcesResult, *jsonrpc.Error)
ListResources returns method-not-found by default.
func (*DefaultServer) ListTools ¶ added in v0.3.9
func (d *DefaultServer) ListTools(ctx context.Context, request *schema.ListToolsRequest) (*schema.ListToolsResult, *jsonrpc.Error)
ListTools returns method-not-found by default.
func (*DefaultServer) OnNotification ¶ added in v0.3.9
func (d *DefaultServer) OnNotification(ctx context.Context, notification *jsonrpc.Notification)
OnNotification is a no-op by default.
func (*DefaultServer) ReadResource ¶ added in v0.3.9
func (d *DefaultServer) ReadResource(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error)
ReadResource returns method-not-found by default.
func (*DefaultServer) Subscribe ¶ added in v0.3.9
func (d *DefaultServer) Subscribe(ctx context.Context, request *schema.SubscribeRequest) (*schema.SubscribeResult, *jsonrpc.Error)
Subscribe adds the URI to the subscription map.
func (*DefaultServer) Unsubscribe ¶ added in v0.3.9
func (d *DefaultServer) Unsubscribe(ctx context.Context, request *schema.UnsubscribeRequest) (*schema.UnsubscribeResult, *jsonrpc.Error)
Unsubscribe removes the URI from the subscription map.
type NewServer ¶ added in v0.3.9
type NewServer func(ctx context.Context, notifier transport.Notifier, logger logger.Logger, client client.Operations) (Server, error)
NewServer creates new implementer
type Operations ¶
type Operations interface {
Initialize(ctx context.Context, init *schema.InitializeRequestParams, result *schema.InitializeResult)
ListResources(ctx context.Context, request *schema.ListResourcesRequest) (*schema.ListResourcesResult, *jsonrpc.Error)
ListResourceTemplates(ctx context.Context, request *schema.ListResourceTemplatesRequest) (*schema.ListResourceTemplatesResult, *jsonrpc.Error)
ReadResource(ctx context.Context, request *schema.ReadResourceRequest) (*schema.ReadResourceResult, *jsonrpc.Error)
Subscribe(ctx context.Context, request *schema.SubscribeRequest) (*schema.SubscribeResult, *jsonrpc.Error)
Unsubscribe(ctx context.Context, request *schema.UnsubscribeRequest) (*schema.UnsubscribeResult, *jsonrpc.Error)
ListTools(ctx context.Context, request *schema.ListToolsRequest) (*schema.ListToolsResult, *jsonrpc.Error)
CallTool(ctx context.Context, request *schema.CallToolRequest) (*schema.CallToolResult, *jsonrpc.Error)
ListPrompts(ctx context.Context, request *schema.ListPromptsRequest) (*schema.ListPromptsResult, *jsonrpc.Error)
GetPrompt(ctx context.Context, request *schema.GetPromptRequest) (*schema.GetPromptResult, *jsonrpc.Error)
Complete(ctx context.Context, request *schema.CompleteRequest) (*schema.CompleteResult, *jsonrpc.Error)
}
Operations represents implementation interface
type Option ¶
type Option func(server *DefaultServer) error
Option is a default implementation of the server interface
type PromptEntry ¶ added in v0.3.2
type PromptEntry struct {
Handler PromptHandlerFunc
Prompt *schema.Prompt
}
PromptEntry holds a handler with its metadata.
type PromptHandlerFunc ¶ added in v0.3.2
type PromptHandlerFunc func(ctx context.Context, request *schema.GetPromptRequestParams) (*schema.GetPromptResult, *jsonrpc.Error)
PromptHandlerFunc defines a function to handle a tool call.
type Registry ¶ added in v0.3.3
type Registry struct {
// ToolRegistry holds per-instance registered tools and handlers.
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 handlers and tools.
func NewRegistry ¶ added in v0.3.4
func NewRegistry() *Registry
NewHandlerRegistry creates a new Registry instance.
func (*Registry) ListRegisteredResourceTemplates ¶ added in v0.3.3
func (d *Registry) ListRegisteredResourceTemplates() []schema.ResourceTemplate
ListRegisteredResourceTemplates returns metadata for all registered resource templates on this DefaultServer.
func (*Registry) ListRegisteredResources ¶ added in v0.3.3
ListRegisteredResources returns metadata for all registered resources on this DefaultServer.
func (*Registry) ListRegisteredTools ¶ added in v0.3.3
ListRegisteredTools returns metadata for all registered tools on this Base.
func (*Registry) RegisterPrompts ¶ added in v0.3.3
func (d *Registry) RegisterPrompts(prompt *schema.Prompt, handler PromptHandlerFunc)
RegisterPrompts registers a prompt on this DefaultServer.
func (*Registry) RegisterResource ¶ added in v0.3.3
func (d *Registry) RegisterResource(resource schema.Resource, handler ResourceHandlerFunc)
RegisterResource registers a resource with metadata and handler on this DefaultServer.
func (*Registry) RegisterResourceTemplate ¶ added in v0.3.3
func (d *Registry) RegisterResourceTemplate(template schema.ResourceTemplate, handler ResourceHandlerFunc)
RegisterResourceTemplate registers a resource template on this DefaultServer.
func (*Registry) RegisterTool ¶ added in v0.3.4
RegisterTool registers a tool with name, description, input schema, and handler
func (*Registry) RegisterToolWithSchema ¶ added in v0.3.3
func (d *Registry) RegisterToolWithSchema(name string, description string, inputSchema schema.ToolInputSchema, handler ToolHandlerFunc)
RegisterToolWithSchema registers a tool with name, description, input schema, and handler on this Base. The tool will be advertised to clients with the provided metadata.
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 ResourceTemplates ¶ added in v0.3.3
type ResourceTemplates []*ResourceTemplateEntry
ResourceTemplates is a collection of ResourceTemplateEntry
type Resources ¶ added in v0.3.3
type Resources []*ResourceEntry
Resources is a collection of ResourceEntry
type Server ¶ added in v0.3.9
type Server interface {
Operations
OnNotification(ctx context.Context, notification *jsonrpc.Notification)
// Implements checks if the method is implemented
Implements(method string) bool
}
Server represents a implementer implementer
type ToolEntry ¶
type ToolEntry struct {
Handler ToolHandlerFunc
Metadata schema.Tool
}
ToolEntry holds a handler with its 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.