protocol

package
v0.0.0-...-22b9b45 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ServerInit    = "initialize"
	ListPrompts   = "prompts/list"
	GetPrompt     = "prompts/get"
	ListTools     = "tools/list"
	InvokeTool    = "tools/call"
	ToolsChanged  = "notifications/tools/list_changed"
	ListResources = "resources/list"
	GetResource   = "resources/read"
)
View Source
const (
	PARSE_ERROR      = -32700
	INVALID_REQUEST  = -32600
	METHOD_NOT_FOUND = -32601
	INVALID_PARAMS   = -32602
	INTERNAL_ERROR   = -32603
)

Variables

View Source
var ContentTypes = &contentTypes{
	ts: map[ContentType]string{
		Text:  "text",
		Image: "image",
		Audio: "audio",
	},
}
View Source
var PromptRoles = map[PromptRole]string{
	User:   "user",
	System: "system",
}

Functions

This section is empty.

Types

type CallToolRequest

type CallToolRequest struct {
	//BasicMessage
	Params *Params `json:"params"`
}

type Capabilities

type Capabilities struct {
	*LoggingCapability  `json:"logging"`
	*PromptCapability   `json:"prompts"`
	*ToolsCapability    `json:"tools"`
	*ResourceCapability `json:"resources"`
}

type ComponentType

type ComponentType uint
const (
	CmpTool ComponentType = iota
	CmpPrompt
	CmpResource
)

type Content

type Content struct {
	Type     string    `json:"type"`
	Text     string    `json:"text,omitempty"`
	Data     string    `json:"data,omitempty"`
	MimeType string    `json:"mimeType,omitempty"`
	Resource *Resource `json:"resource,omitempty"`
}

func (*Content) SetData

func (c *Content) SetData(data string)

func (*Content) SetMime

func (c *Content) SetMime(mime string)

func (*Content) SetResource

func (c *Content) SetResource(resource *Resource)

func (*Content) SetType

func (c *Content) SetType(typ string)

type ContentGetter

type ContentGetter interface {
	// contains filtered or unexported methods
}

type ContentType

type ContentType uint
const (
	Text ContentType = iota
	Image
	Audio
)

for Tool and Prompt invoke result

type Descriptor

type Descriptor interface {
	SetDescribe(description string)
}

type Encoder

type Encoder interface {
	MarshalJson() ([]byte, error)
}

type Error

type Error struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

func (*Error) Error

func (e *Error) Error() string

type HandleFunc

type HandleFunc func(map[string]any) (string, error)

HandleFunc provide external function calls for Tool and Prompt only

type HandlerSetter

type HandlerSetter interface {
	SetHandler(handler HandleFunc)
}

type IContent

type IContent interface {
	SetType(typ string)
	// SetData set data or text field
	SetData(data string)
	SetMime(mime string)
	SetResource(resource *Resource)
}

IContent interface for Tool and Prompt content

func NewContent

func NewContent(typ ContentType) IContent

type IPrompt

type IPrompt interface {
	Descriptor
	HandlerSetter
	ContentGetter
	Describe(description string) IPrompt
	Handler(handler HandleFunc) IPrompt
	Role(role string) IPrompt
	Content(typ ContentType) IPrompt
	Argument(name string, required bool) IPrompt
	// contains filtered or unexported methods
}

type IResource

type IResource interface {
	Descriptor
	Describe(description string) IResource
	URI(uri string) IResource
	Handler(handler ResourceReader) IResource
	Mime(mime string) IResource
	SetHandler(handler ResourceReader)
	Content(typ ResourceType) IResource
	Read() (string, error)
	// contains filtered or unexported methods
}

type IResourceContent

type IResourceContent interface {
	// SetData set text or blog field
	SetData(resType ResourceType, data string)
	SetMime(mime string)
	SetUri(uri string)
}

IResourceContent interface for Resource content

type IServer

type IServer interface {
	AddHandler(method string, hd handler)
	SetHandler(method string, hd handler)
	RemoveHandler(method string)
	Tool(name string) ITool
	AddTools(tools ...ITool)
	Prompt(name string) IPrompt
	AddPrompts(prompts ...IPrompt)
	Resource(name string) IResource
	AddResources(resources ...IResource)
	GetTool(name string) (ITool, error)
	GetTools() ([]ITool, error)
	GetPrompt(name string) (IPrompt, error)
	GetResource(name string) (IResource, error)
	InvokeHandler(req Request) *Response
}

func NewServer

func NewServer(name, version string) IServer

type ITool

type ITool interface {
	Descriptor
	HandlerSetter
	ContentGetter
	Describe(description string) ITool
	Handler(handler HandleFunc) ITool
	Property(property Property) ITool
	Properties(ps ...Property) ITool
	Content(typ ContentType) ITool
	// contains filtered or unexported methods
}

type InputSchema

type InputSchema struct {
	sync.RWMutex
	Type       string              `json:"type"`
	Properties map[string]Property `json:"properties"`
}

type LoggingCapability

type LoggingCapability struct {
	Logging map[string]any `json:"-"`
}

type MCPServerInitResult

type MCPServerInitResult struct {
	ProtocolVersion string        `json:"protocolVersion"`
	Capabilities    *Capabilities `json:"capabilities"`
	ServerInfo      *ServerInfo   `json:"serverInfo"`
}

type Message

type Message struct {
	Ro      string   `json:"role"`
	Content IContent `json:"content"`
}

func NewPromptMessage

func NewPromptMessage(role string) *Message

func (*Message) AddContent

func (m *Message) AddContent(content IContent)

func (*Message) Role

func (m *Message) Role(role string) *Message

type Notification

type Notification struct {
	JsonRPC string         `json:"jsonrpc"`
	Method  string         `json:"method"`
	Meta    map[string]any `json:"_meta,omitempty"`
	Params  map[string]any `json:"params,omitempty"`
}

func NewNotification

func NewNotification(method string) *Notification

func (*Notification) MarshalJson

func (n *Notification) MarshalJson() ([]byte, error)

func (*Notification) String

func (n *Notification) String() string

func (*Notification) Write

func (n *Notification) Write(key string, value any)

type Params

type Params struct {
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments"`
}

type Prompt

type Prompt struct {
	Name        string            `json:"name"`
	Description string            `json:"description,omitempty"`
	Args        []*PromptArgument `json:"arguments,omitempty"`
	// contains filtered or unexported fields
}

func (*Prompt) Argument

func (p *Prompt) Argument(name string, required bool) IPrompt

Argument add argument without description to Prompt directly

func (*Prompt) Content

func (p *Prompt) Content(typ ContentType) IPrompt

Content mark content type of handler result

func (*Prompt) Describe

func (p *Prompt) Describe(description string) IPrompt

func (*Prompt) Handler

func (p *Prompt) Handler(handler HandleFunc) IPrompt

func (*Prompt) Role

func (p *Prompt) Role(role string) IPrompt

func (*Prompt) SetDescribe

func (p *Prompt) SetDescribe(description string)

func (*Prompt) SetHandler

func (p *Prompt) SetHandler(handler HandleFunc)

type PromptArgument

type PromptArgument struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	IsRequired  bool   `json:"required,omitempty"`
}

type PromptCapability

type PromptCapability struct {
	ListChanged bool `json:"listChanged"`
}

type PromptRole

type PromptRole uint
const (
	User PromptRole = iota
	System
)

type Property

type Property struct {
	Name        string
	Type        string `json:"type"`
	Description string `json:"description,omitempty"`
}

func NewToolProperty

func NewToolProperty(name, typ, description string) Property

type Request

type Request struct {
	JsonRPC string         `json:"jsonrpc"`
	ID      any            `json:"id"`
	Method  string         `json:"method"`
	Params  map[string]any `json:"params,omitempty"`
}

func (Request) GetParam

func (r Request) GetParam(param string) (any, error)

type Resource

type Resource struct {
	Uri         string `json:"uri"`
	Description string `json:"description,omitempty"`
	Name        string `json:"name"`
	MimeType    string `json:"mimeType,omitempty"`
	// contains filtered or unexported fields
}

func (*Resource) Content

func (r *Resource) Content(typ ResourceType) IResource

func (*Resource) Describe

func (r *Resource) Describe(description string) IResource

func (*Resource) Handler

func (r *Resource) Handler(handler ResourceReader) IResource

func (*Resource) Mime

func (r *Resource) Mime(mime string) IResource

func (*Resource) Read

func (r *Resource) Read() (string, error)

func (*Resource) SetDescribe

func (r *Resource) SetDescribe(description string)

func (*Resource) SetHandler

func (r *Resource) SetHandler(handler ResourceReader)

func (*Resource) URI

func (r *Resource) URI(uri string) IResource

type ResourceCapability

type ResourceCapability struct {
	Subscribe   bool `json:"subscribe,omitempty"`
	ListChanged bool `json:"listChanged,omitempty"`
}

type ResourceContent

type ResourceContent struct {
	Uri      string `json:"uri"`
	Text     string `json:"text,omitempty"`
	Blob     string `json:"blob,omitempty"`
	MimeType string `json:"mimeType,omitempty"`
}

func (*ResourceContent) SetData

func (r *ResourceContent) SetData(resType ResourceType, data string)

func (*ResourceContent) SetMime

func (r *ResourceContent) SetMime(mime string)

func (*ResourceContent) SetUri

func (r *ResourceContent) SetUri(uri string)

type ResourceReader

type ResourceReader func(uri string) (string, error)

type ResourceTemplate

type ResourceTemplate struct {
	UriTemplate string `json:"uriTemplate"`
	Description string `json:"description,omitempty"`
	Name        string `json:"name"`
	MimeType    string `json:"mimeType,omitempty"`
}

type ResourceType

type ResourceType uint
const (
	TextRes ResourceType = iota
	BinaryRes
)

type Response

type Response struct {
	JsonRPC string         `json:"jsonrpc"`
	ID      any            `json:"id"`
	Result  map[string]any `json:"result"`
	Error   *Error         `json:"error,omitempty"`
}

func NewResponse

func NewResponse(id any) *Response

func (*Response) MarshalJson

func (r *Response) MarshalJson() ([]byte, error)

func (*Response) String

func (r *Response) String() string

func (*Response) Write

func (r *Response) Write(key string, value any)

func (*Response) WriteError

func (r *Response) WriteError(code int, err error) error

type Server

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

func (*Server) AddContentTypes

func (s *Server) AddContentTypes(typ ContentType, name string)

AddContentTypes add customized content type for Tool and Prompt invoke

func (*Server) AddHandler

func (s *Server) AddHandler(method string, hd handler)

func (*Server) AddPrompt

func (s *Server) AddPrompt(prompt IPrompt)

func (*Server) AddPrompts

func (s *Server) AddPrompts(prompts ...IPrompt)

func (*Server) AddResource

func (s *Server) AddResource(resource IResource)

func (*Server) AddResources

func (s *Server) AddResources(resources ...IResource)

func (*Server) AddTool

func (s *Server) AddTool(tool ITool)

func (*Server) AddTools

func (s *Server) AddTools(tools ...ITool)

func (*Server) Capabilities

func (s *Server) Capabilities(listChangedTools, listChangedPrompts, listChangedSources bool, subscribeResources bool)

func (*Server) FetchPrompt

func (s *Server) FetchPrompt(req Request) *Response

func (*Server) FetchPrompts

func (s *Server) FetchPrompts(req Request) *Response

func (*Server) FetchResource

func (s *Server) FetchResource(req Request) *Response

func (*Server) FetchResources

func (s *Server) FetchResources(req Request) *Response

func (*Server) FetchTools

func (s *Server) FetchTools(req Request) *Response

func (*Server) GetPrompt

func (s *Server) GetPrompt(name string) (IPrompt, error)

func (*Server) GetPrompts

func (s *Server) GetPrompts() ([]IPrompt, error)

func (*Server) GetResource

func (s *Server) GetResource(uri string) (IResource, error)

func (*Server) GetResources

func (s *Server) GetResources() ([]IResource, error)

func (*Server) GetTool

func (s *Server) GetTool(name string) (ITool, error)

func (*Server) GetTools

func (s *Server) GetTools() ([]ITool, error)

func (*Server) Initialize

func (s *Server) Initialize(req Request) *Response

func (*Server) InvokeHandler

func (s *Server) InvokeHandler(req Request) *Response

func (*Server) InvokeTool

func (s *Server) InvokeTool(req Request) *Response

func (*Server) ProcessRequest

func (s *Server) ProcessRequest(req Request) *Response

func (*Server) Prompt

func (s *Server) Prompt(name string) IPrompt

func (*Server) RemoveHandler

func (s *Server) RemoveHandler(method string)

func (*Server) RemoveTool

func (s *Server) RemoveTool(name string) error

func (*Server) Resource

func (s *Server) Resource(name string) IResource

func (*Server) SetHandler

func (s *Server) SetHandler(method string, hd handler)

func (*Server) Tool

func (s *Server) Tool(name string) ITool

type ServerInfo

type ServerInfo struct {
	Name    string `json:"name"`
	Version string `json:"version"`
}

type Tool

type Tool struct {
	Name        string       `json:"name"`
	Description string       `json:"description"`
	InputSchema *InputSchema `json:"inputSchema,omitempty"`
	// contains filtered or unexported fields
}

func (*Tool) Content

func (t *Tool) Content(typ ContentType) ITool

Content mark type of handler result

func (*Tool) Describe

func (t *Tool) Describe(description string) ITool

func (*Tool) Handler

func (t *Tool) Handler(handler HandleFunc) ITool

func (*Tool) Properties

func (t *Tool) Properties(ps ...Property) ITool

func (*Tool) Property

func (t *Tool) Property(property Property) ITool

func (*Tool) SetDescribe

func (t *Tool) SetDescribe(description string)

func (*Tool) SetHandler

func (t *Tool) SetHandler(handler HandleFunc)

type ToolsCapability

type ToolsCapability struct {
	ListChanged bool `json:"listChanged"`
}

type Writer

type Writer interface {
	Write(key string, value any)
}

Writer write data to client the param is a key-value pair

Jump to

Keyboard shortcuts

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