server

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2025 License: Apache-2.0 Imports: 10 Imported by: 11

Documentation

Index

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

CallTool returns method-not-found by default.

func (*DefaultServer) Complete added in v0.3.9

Complete returns method-not-found by default.

func (*DefaultServer) GetPrompt added in v0.3.9

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

Initialize stores the initialization parameters.

func (*DefaultServer) ListPrompts added in v0.3.9

ListPrompts lists all registered prompts on this DefaultServer.

func (*DefaultServer) ListResourceTemplates added in v0.3.9

ListResourceTemplates returns method-not-found by default.

func (*DefaultServer) ListResources added in v0.3.9

ListResources returns method-not-found by default.

func (*DefaultServer) ListTools added in v0.3.9

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

ReadResource returns method-not-found by default.

func (*DefaultServer) Subscribe added in v0.3.9

Subscribe adds the URI to the subscription map.

func (*DefaultServer) Unsubscribe added in v0.3.9

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

func WithDefaultServer added in v0.3.9

func WithDefaultServer(ctx context.Context, options ...Option) NewServer

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 Prompts added in v0.3.3

type Prompts []*PromptEntry

Prompts is a collection of PromptEntry

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

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

ListRegisteredResources returns metadata for all registered resources on this DefaultServer.

func (*Registry) ListRegisteredTools added in v0.3.3

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

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

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

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.

type Tools added in v0.3.3

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