server

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: May 28, 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 DefaultImplementer

type DefaultImplementer struct {
	Notifier           transport.Notifier
	Logger             logger.Logger
	Client             client.Operations
	ClientInitialize   *schema.InitializeRequestParams
	Subscription       *syncmap.Map[string, bool]
	ServerCapabilities *schema.ServerCapabilities
	*Registry
}

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

func NewDefaultImplementer

func NewDefaultImplementer(notifier transport.Notifier, logger logger.Logger, client client.Operations) *DefaultImplementer

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

func (*DefaultImplementer) CallTool

CallTool returns method-not-found by default.

func (*DefaultImplementer) Complete

Complete returns method-not-found by default.

func (*DefaultImplementer) GetPrompt

GetPrompt returns the result of a prompt call.

func (*DefaultImplementer) Implements

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

Implements returns true for supported methods.

func (*DefaultImplementer) Initialize

Initialize stores the initialization parameters.

func (*DefaultImplementer) ListPrompts

ListPrompts lists all registered prompts on this DefaultImplementer.

func (*DefaultImplementer) ListResourceTemplates

ListResourceTemplates returns method-not-found by default.

func (*DefaultImplementer) ListResources

ListResources returns method-not-found by default.

func (*DefaultImplementer) ListTools

ListTools returns method-not-found by default.

func (*DefaultImplementer) OnNotification

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

OnNotification is a no-op by default.

func (*DefaultImplementer) ReadResource

ReadResource returns method-not-found by default.

func (*DefaultImplementer) Subscribe

Subscribe adds the URI to the subscription map.

func (*DefaultImplementer) Unsubscribe

Unsubscribe removes the URI from the subscription map.

type Implementer

type Implementer interface {
	Operations

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

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

Implementer represents a implementer implementer

type NewImplementer

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

NewImplementer creates new implementer

func WithDefaultImplementer

func WithDefaultImplementer(ctx context.Context, options ...Option) NewImplementer

type Option

type Option func(implementer *DefaultImplementer) 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 DefaultImplementer.

func (*Registry) ListRegisteredResources added in v0.3.3

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

ListRegisteredResources returns metadata for all registered resources on this DefaultImplementer.

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 DefaultImplementer.

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 DefaultImplementer.

func (*Registry) RegisterResourceTemplate added in v0.3.3

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

RegisterResourceTemplate registers a resource template on this DefaultImplementer.

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 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