server

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 10, 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](base *DefaultImplementer, 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](implementer *DefaultImplementer, 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]
	// ToolRegistry holds per-instance registered tools and handlers.
	ToolRegistry             *syncmap.Map[string, *ToolEntry]
	ResourceRegistry         *syncmap.Map[string, *ResourceEntry]
	ResourceTemplateRegistry *syncmap.Map[string, *ResourceTemplateEntry]
	Methods                  *syncmap.Map[string, bool]
}

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 method-not-found by default.

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 returns method-not-found by default.

func (*DefaultImplementer) ListRegisteredResourceTemplates

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

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

func (*DefaultImplementer) ListRegisteredResources

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

ListRegisteredResources returns metadata for all registered resources on this DefaultImplementer.

func (*DefaultImplementer) ListRegisteredTools

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

ListRegisteredTools returns metadata for all registered tools on this Base.

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

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

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

func (*DefaultImplementer) RegisterResourceTemplate

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

RegisterResourceTemplate registers a resource template on this DefaultImplementer.

func (*DefaultImplementer) RegisterToolWithSchema

func (d *DefaultImplementer) 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.

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

Jump to

Keyboard shortcuts

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