server

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreatePromptHandler added in v0.1.1

func CreatePromptHandler[T any](handler PromptHandler[T]) any

CreatePromptHandler 创建一个类型安全的提示处理函数 该函数用于创建与RegisterPromptTyped兼容的处理函数 使用例子:

type GreetInput struct {
  Name    string `json:"name"`
  Formal  bool   `json:"formal"`
}

server.RegisterPromptTyped("greet", "问候信息",

server.CreatePromptHandler(func(req GreetInput) (*types.GetPromptResult, error) {
  // 处理逻辑...
  return &types.GetPromptResult{...}, nil
})

)

func CreateToolHandler added in v0.1.1

func CreateToolHandler[T any](handler ToolHandler[T]) any

CreateToolHandler 创建一个类型安全的工具处理函数 该函数用于创建与RegisterToolTyped兼容的处理函数 使用例子:

type WeatherRequest struct {
  Longitude float64 `json:"longitude"`
  Latitude  float64 `json:"latitude"`
}

server.RegisterToolTyped("getWeather", "获取天气",

server.CreateToolHandler(func(req WeatherRequest) (*types.CallToolResult, error) {
  // 处理逻辑...
  return &types.CallToolResult{...}, nil
})

)

Types

type BaseServer

type BaseServer struct {
	// contains filtered or unexported fields
}

BaseServer is a base implementation of the Server interface

func NewBaseServer

func NewBaseServer(name, version string) *BaseServer

NewBaseServer creates a new base server

func (*BaseServer) CallTool

func (s *BaseServer) CallTool(ctx context.Context, name string, arguments map[string]any) (*types.CallToolResult, error)

CallTool implements Server.CallTool

func (*BaseServer) GetPrompt

func (s *BaseServer) GetPrompt(ctx context.Context, name string, arguments map[string]any) (*types.GetPromptResult, error)

GetPrompt implements Server.GetPrompt

func (*BaseServer) Initialize

func (s *BaseServer) Initialize(ctx context.Context, options any) error

Initialize implements Server.Initialize

func (*BaseServer) ListPrompts

func (s *BaseServer) ListPrompts(ctx context.Context) ([]types.Prompt, error)

ListPrompts implements Server.ListPrompts

func (*BaseServer) ListPromptsPaginated

func (s *BaseServer) ListPromptsPaginated(ctx context.Context, options types.PaginationOptions) (*types.PaginatedResult, error)

ListPromptsPaginated implements Server.ListPromptsPaginated

func (*BaseServer) ListResources

func (s *BaseServer) ListResources(ctx context.Context) ([]types.Resource, error)

ListResources implements Server.ListResources

func (*BaseServer) ListResourcesPaginated

func (s *BaseServer) ListResourcesPaginated(ctx context.Context, options types.PaginationOptions) (*types.PaginatedResult, error)

ListResourcesPaginated implements Server.ListResourcesPaginated

func (*BaseServer) ListTools

func (s *BaseServer) ListTools(ctx context.Context) ([]types.Tool, error)

ListTools implements Server.ListTools

func (*BaseServer) ListToolsPaginated

func (s *BaseServer) ListToolsPaginated(ctx context.Context, options types.PaginationOptions) (*types.PaginatedResult, error)

ListToolsPaginated implements Server.ListToolsPaginated

func (*BaseServer) ReadResource

func (s *BaseServer) ReadResource(ctx context.Context, name string) ([]byte, string, error)

ReadResource implements Server.ReadResource

func (*BaseServer) RegisterNotificationHandler

func (s *BaseServer) RegisterNotificationHandler(handler types.NotificationHandler)

RegisterNotificationHandler implements Server.RegisterNotificationHandler

func (*BaseServer) RegisterPrompt

func (s *BaseServer) RegisterPrompt(prompt types.Prompt)

RegisterPrompt implements Server.RegisterPrompt

func (*BaseServer) RegisterPromptTyped

func (s *BaseServer) RegisterPromptTyped(name, description string, handler any) error

RegisterPromptTyped implements Server.RegisterPromptTyped

func (*BaseServer) RegisterResource

func (s *BaseServer) RegisterResource(resource types.Resource)

RegisterResource implements Server.RegisterResource

func (*BaseServer) RegisterTool

func (s *BaseServer) RegisterTool(tool types.Tool)

RegisterTool implements Server.RegisterTool

func (*BaseServer) RegisterToolTyped

func (s *BaseServer) RegisterToolTyped(name, description string, handler any) error

RegisterToolTyped implements Server.RegisterToolTyped

type PromptHandler added in v0.1.1

type PromptHandler[T any] func(request T) (*types.GetPromptResult, error)

PromptHandler defines the type for prompt processing functions

type Server

type Server interface {
	// Initialize initializes the server with the given options
	Initialize(ctx context.Context, options any) error

	// GetPrompt retrieves a prompt with the given name and arguments
	GetPrompt(ctx context.Context, name string, arguments map[string]any) (*types.GetPromptResult, error)

	// ListPrompts lists all available prompts
	ListPrompts(ctx context.Context) ([]types.Prompt, error)

	// ListPromptsPaginated lists prompts with pagination
	ListPromptsPaginated(ctx context.Context, options types.PaginationOptions) (*types.PaginatedResult, error)

	// CallTool calls a tool with the given name and arguments
	CallTool(ctx context.Context, name string, arguments map[string]any) (*types.CallToolResult, error)

	// ListTools lists all available tools
	ListTools(ctx context.Context) ([]types.Tool, error)

	// ListToolsPaginated lists tools with pagination
	ListToolsPaginated(ctx context.Context, options types.PaginationOptions) (*types.PaginatedResult, error)

	// ReadResource reads a resource with the given name
	ReadResource(ctx context.Context, name string) ([]byte, string, error)

	// ListResources lists all available resources
	ListResources(ctx context.Context) ([]types.Resource, error)

	// ListResourcesPaginated lists resources with pagination
	ListResourcesPaginated(ctx context.Context, options types.PaginationOptions) (*types.PaginatedResult, error)

	// RegisterPrompt registers a prompt with the server
	RegisterPrompt(prompt types.Prompt)

	// RegisterPromptTyped registers a prompt with the server using a typed handler
	RegisterPromptTyped(name string, description string, handler any) error

	// RegisterTool registers a tool with the server
	RegisterTool(tool types.Tool)

	// RegisterToolTyped registers a tool with the server using a typed handler
	// handler must be a function that accepts a struct parameter and returns (*types.CallToolResult, error)
	// This approach reduces the use of runtime reflection, improving performance
	RegisterToolTyped(name string, description string, handler any) error

	// RegisterResource registers a resource with the server
	RegisterResource(resource types.Resource)

	// RegisterNotificationHandler registers a notification handler
	RegisterNotificationHandler(handler types.NotificationHandler)
}

Server defines the interface for an MCP server

type ToolHandler added in v0.1.1

type ToolHandler[T any] func(request T) (*types.CallToolResult, error)

ToolHandler defines the type for tool processing functions

type TypedPromptHandler added in v0.1.1

type TypedPromptHandler interface {
	Execute(ctx context.Context, arguments map[string]any) (*types.GetPromptResult, error)
}

TypedPromptHandler defines the interface for prompt processing functions

type TypedToolHandler added in v0.1.1

type TypedToolHandler interface {
	Execute(ctx context.Context, arguments map[string]any) (*types.CallToolResult, error)
}

TypedToolHandler defines the interface for tool processing functions

Jump to

Keyboard shortcuts

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