Documentation
¶
Index ¶
- Constants
- type BlobResourceContents
- type Capabilities
- type ClientInfoData
- type EmbeddedResource
- type ImageContent
- type InitializationRequestCapabilities
- type InitializationRequestParams
- type InitializationResponseData
- type IsContent
- type IsResourceContents
- type ListResourceTemplatesRequestParams
- type ListResourceTemplatesResultData
- type ListResourcesRequestParams
- type ListResourcesResultData
- type ListToolsRequestParams
- type ListToolsResultData
- type LoggingCapabilities
- type Notification
- type ProgressToken
- type PromptsCapabilities
- type ReadResourceRequestParams
- type ReadResourceResultData
- type Request
- type RequestMeta
- type Resource
- type ResourceReader
- type ResourceReaderMux
- func (m *ResourceReaderMux) Handle(uri string, h router.Handler[*Result[ReadResourceResultData]]) error
- func (m *ResourceReaderMux) HandleFunc(uri string, ...) error
- func (m *ResourceReaderMux) ReadResource(ctx context.Context, request *Request[ReadResourceRequestParams]) (*Result[ReadResourceResultData], error)
- func (m *ResourceReaderMux) SetNotFoundHandler(h router.Handler[*Result[ReadResourceResultData]])
- func (m *ResourceReaderMux) SetNotFoundHandlerFunc(...)
- type ResourceTemplate
- type ResourcesCapabilities
- type Result
- type RootsCapabilities
- type SamplingCapabilities
- type Server
- func (s *Server) CallTool(ctx context.Context, request *Request[ToolCallRequestParams]) (*Result[ToolCallResultData], error)
- func (s *Server) Close() error
- func (s *Server) HandleSession(ctx context.Context, id uint64, t transport.Session) error
- func (s *Server) Initialize(ctx context.Context, request *Request[InitializationRequestParams]) (*Result[InitializationResponseData], error)
- func (s *Server) Initialized(ctx context.Context, params struct{}) (struct{}, error)
- func (s *Server) ListResourceTemplates(ctx context.Context, request *Request[ListResourceTemplatesRequestParams]) (*Result[ListResourceTemplatesResultData], error)
- func (s *Server) ListResources(ctx context.Context, request *Request[ListResourcesRequestParams]) (*Result[ListResourcesResultData], error)
- func (s *Server) ListTools(ctx context.Context, request *Request[ListToolsRequestParams]) (*Result[ListToolsResultData], error)
- func (s *Server) Ping(ctx context.Context, _ *Request[struct{}]) (*Result[struct{}], error)
- func (s *Server) ReadResource(ctx context.Context, request *Request[ReadResourceRequestParams]) (*Result[ReadResourceResultData], error)
- func (s *Server) SSEHandler(baseURL string) (http.Handler, error)
- func (s *Server) Serve(ctx context.Context, id uint64, t transport.Session) error
- func (s *Server) ServeStdio(ctx context.Context) error
- type ServerInfoData
- type ServerOption
- func WithCustomHandler[Params, Result any](method string, handler jsonrpc2.Handler[Params, Result]) ServerOption
- func WithCustomHandlerFunc[Params, Result any](method string, ...) ServerOption
- func WithLogger(logger *slog.Logger) ServerOption
- func WithResource(resource Resource) ServerOption
- func WithResourceReader(reader ResourceReader) ServerOption
- func WithResourceTemplate(template ResourceTemplate) ServerOption
- func WithTool[Input, Output any](tool Tool[Input, Output]) ServerOption
- type TextContent
- type TextResourceContents
- type Tool
- type ToolCallRequestParams
- type ToolCallResultData
- type ToolHandler
- type ToolHandlerFunc
- type ToolsCapabilities
Constants ¶
const (
SupportedProtocolVersion = "2024-11-05"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlobResourceContents ¶
type BlobResourceContents struct {
// URI is the unique identifier of the resource.
URI string `json:"uri"`
// MimeType is the MIME type of the resource.
MimeType string `json:"mimeType,omitempty,omitzero"`
// Blob is the binary data of the resource.
// This field is base64 encoded when marshaling to JSON.
Blob []byte `json:"blob"`
}
BlobResourceContents is the contents of a blob resource.
func (BlobResourceContents) MarshalJSON ¶
func (r BlobResourceContents) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
type Capabilities ¶
type Capabilities struct {
Logging *LoggingCapabilities `json:"logging,omitempty,omitzero"`
Prompts *PromptsCapabilities `json:"prompts,omitempty,omitzero"`
Resources *ResourcesCapabilities `json:"resources,omitempty,omitzero"`
Tools *ToolsCapabilities `json:"tools,omitempty,omitzero"`
}
Capabilities is the capabilities for the server.
type ClientInfoData ¶
ClientInfoData is the data for the client info.
type EmbeddedResource ¶
type EmbeddedResource struct {
Resource IsResourceContents `json:"resource"`
}
EmbeddedResource is the embedded resource content of the tool call result. TODO: add Annotations field
func (EmbeddedResource) MarshalJSON ¶
func (t EmbeddedResource) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
type ImageContent ¶
ImageContent is the image content of the tool call result. TODO: add Annotations field
func (ImageContent) MarshalJSON ¶
func (t ImageContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
type InitializationRequestCapabilities ¶
type InitializationRequestCapabilities struct {
Roots *RootsCapabilities `json:"roots,omitempty,omitzero"`
Sampling *SamplingCapabilities `json:"sampling,omitempty,omitzero"`
}
InitializationRequestCapabilities is the capabilities for the initialization request.
type InitializationRequestParams ¶
type InitializationRequestParams struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities InitializationRequestCapabilities `json:"capabilities,omitzero"`
ClientInfo ClientInfoData `json:"clientInfo"`
}
InitializationRequestParams is the params for the initialization request.
type InitializationResponseData ¶
type InitializationResponseData struct {
ProtocolVersion string `json:"protocolVersion"`
Capabilities Capabilities `json:"capabilities"`
ServerInfo ServerInfoData `json:"serverInfo"`
}
InitializationResponseData is the data for the initialization response.
type IsContent ¶
type IsContent interface {
// contains filtered or unexported methods
}
IsContent is an interface for the content of the tool call result.
type IsResourceContents ¶
type IsResourceContents interface {
// contains filtered or unexported methods
}
IsResourceContents is an interface for the content of the read resource result.
type ListResourceTemplatesRequestParams ¶
type ListResourceTemplatesRequestParams struct {
Cursor string `json:"cursor"`
}
ListResourceTemplatesRequestParams is the parameters of the list resource templates request.
type ListResourceTemplatesResultData ¶
type ListResourceTemplatesResultData struct {
ResourceTemplates []ResourceTemplate `json:"resourceTemplates"`
NextCursor string `json:"nextCursor,omitempty,omitzero"`
}
ListResourceTemplatesResultData is the result of the list resource templates request.
type ListResourcesRequestParams ¶
type ListResourcesRequestParams struct {
Cursor string `json:"cursor"`
}
ListResourcesRequestParams is the parameters of the list resources request.
type ListResourcesResultData ¶
type ListResourcesResultData struct {
Resources []Resource `json:"resources"`
NextCursor string `json:"nextCursor,omitempty,omitzero"`
}
ListResourcesResultData is the result of the list resources request.
type ListToolsRequestParams ¶
type ListToolsRequestParams struct {
Cursor string `json:"cursor"`
}
ListToolsRequestParams is the parameters of the list tools request.
type ListToolsResultData ¶
type ListToolsResultData struct {
Tools []tool `json:"tools"`
NextCursor string `json:"nextCursor,omitempty"`
}
ListToolsResultData is the result of the list tools request.
type LoggingCapabilities ¶
type LoggingCapabilities struct{}
LoggingCapabilities is the capabilities for the logging feature.
type Notification ¶
func (*Notification[Params]) MarshalJSON ¶
func (r *Notification[Params]) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (*Notification[Params]) UnmarshalJSON ¶
func (r *Notification[Params]) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
type ProgressToken ¶
type ProgressToken struct {
// contains filtered or unexported fields
}
ProgressToken represents a token that can be used to track the progress of a request. It can be either a string or an integer.
func NewProgressToken ¶
func NewProgressToken[T interface{ string | int }](v T) ProgressToken
NewProgressToken creates a new ProgressToken from a string or an integer.
func (ProgressToken) IsNull ¶
func (id ProgressToken) IsNull() bool
IsNull returns true if the ProgressToken is null.
func (ProgressToken) MarshalJSON ¶
func (id ProgressToken) MarshalJSON() ([]byte, error)
MarshalJSON implements the json.Marshaler interface.
func (ProgressToken) String ¶
func (id ProgressToken) String() string
String returns the ProgressToken as a string. If the ProgressToken is an integer, it is converted to a string. If the ProgressToken is null, it returns an empty string.
func (*ProgressToken) UnmarshalJSON ¶
func (id *ProgressToken) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface.
type PromptsCapabilities ¶
type PromptsCapabilities struct {
ListChanged bool `json:"listChanged,omitempty,omitzero"`
}
PromptsCapabilities is the capabilities for the prompts feature.
type ReadResourceRequestParams ¶
type ReadResourceRequestParams struct {
URI string `json:"uri"`
}
ReadResourceRequestParams is the parameters of the read resource request.
type ReadResourceResultData ¶
type ReadResourceResultData struct {
Contents []IsResourceContents `json:"contents"`
}
ReadResourceResultData is the result of the read resource request.
type Request ¶
type Request[Params any] struct { Meta RequestMeta Params Params }
func (*Request[Params]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Request[Params]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type RequestMeta ¶
type RequestMeta struct {
ProgressToken ProgressToken `json:"progressToken"`
}
type Resource ¶
type Resource struct {
// URI is the unique identifier of the resource.
URI string `json:"uri"`
// Name is human-readable name of the resource.
Name string `json:"name"`
// Description of what the resource is.
Description string `json:"description,omitempty,omitzero"`
// MimeType is the MIME type of the resource.
MimeType string `json:"mimeType,omitempty,omitzero"`
// Size is the size of the resource in bytes before base64 encoding or any tokenization.
Size int64 `json:"size,omitempty,omitzero"`
}
Resource is a resource that can be used in the model context. TODO: add Annotations field.
type ResourceReader ¶
type ResourceReader interface {
ReadResource(ctx context.Context, request *Request[ReadResourceRequestParams]) (*Result[ReadResourceResultData], error)
}
ResourceHandler is the handler of the resource methods.
type ResourceReaderMux ¶
type ResourceReaderMux struct {
// contains filtered or unexported fields
}
ResourceReaderMux is a multiplexer for resource readers.
func NewResourceReaderMux ¶
func NewResourceReaderMux() *ResourceReaderMux
NewResourceReaderMux creates a new resource reader multiplexer.
func (*ResourceReaderMux) Handle ¶
func (m *ResourceReaderMux) Handle(uri string, h router.Handler[*Result[ReadResourceResultData]]) error
Handle registers a new route with a handler.
func (*ResourceReaderMux) HandleFunc ¶
func (m *ResourceReaderMux) HandleFunc(uri string, f func(context.Context, *router.Request) (*Result[ReadResourceResultData], error)) error
HandleFunc registers a new route with a handler function.
func (*ResourceReaderMux) ReadResource ¶
func (m *ResourceReaderMux) ReadResource(ctx context.Context, request *Request[ReadResourceRequestParams]) (*Result[ReadResourceResultData], error)
ReadResource reads a resource.
func (*ResourceReaderMux) SetNotFoundHandler ¶
func (m *ResourceReaderMux) SetNotFoundHandler(h router.Handler[*Result[ReadResourceResultData]])
SetNotFoundHandler sets the handler to be called when no matching route is found.
func (*ResourceReaderMux) SetNotFoundHandlerFunc ¶
func (m *ResourceReaderMux) SetNotFoundHandlerFunc(f func(context.Context, *router.Request) (*Result[ReadResourceResultData], error))
SetNotFoundHandlerFunc sets the handler to be called when no matching route is found.
type ResourceTemplate ¶
type ResourceTemplate struct {
// URITemplate is the URI template of the resource.
URITemplate string `json:"uriTemplate"`
// Name is human-readable name of the resource.
Name string `json:"name"`
// Description of what the resource is.
Description string `json:"description,omitempty,omitzero"`
// MimeType is the MIME type of the resource.
MimeType string `json:"mimeType,omitempty,omitzero"`
}
ResourceTemplate is the template of a resource. TODO: add Annotations field.
type ResourcesCapabilities ¶
type ResourcesCapabilities struct {
Subscribe bool `json:"subscribe,omitempty,omitzero"`
ListChanged bool `json:"listChanged,omitempty,omitzero"`
}
ResourcesCapabilities is the capabilities for the resources feature.
type Result ¶
func (*Result[Data]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Result[Data]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
type RootsCapabilities ¶
type RootsCapabilities struct {
ListChanged bool `json:"listChanged,omitempty,omitzero"`
}
RootsCapabilities is the capabilities for the roots feature.
type SamplingCapabilities ¶
type SamplingCapabilities struct{}
SamplingCapabilities is the capabilities for the sampling feature.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a MCP server.
func NewServer ¶
func NewServer(name, version string, opts ...ServerOption) (*Server, error)
NewServer creates a new MCP server.
func (*Server) CallTool ¶
func (s *Server) CallTool(ctx context.Context, request *Request[ToolCallRequestParams]) (*Result[ToolCallResultData], error)
CallTool implements the jsonrpc2.HandlerFunc
func (*Server) HandleSession ¶
HandleSession handles a session.
func (*Server) Initialize ¶
func (s *Server) Initialize(ctx context.Context, request *Request[InitializationRequestParams]) (*Result[InitializationResponseData], error)
Initialize initializes the server.
func (*Server) Initialized ¶ added in v0.0.8
func (*Server) ListResourceTemplates ¶
func (s *Server) ListResourceTemplates(ctx context.Context, request *Request[ListResourceTemplatesRequestParams]) (*Result[ListResourceTemplatesResultData], error)
ListResourceTemplates lists resource templates.
func (*Server) ListResources ¶
func (s *Server) ListResources(ctx context.Context, request *Request[ListResourcesRequestParams]) (*Result[ListResourcesResultData], error)
ListResources lists resources.
func (*Server) ListTools ¶
func (s *Server) ListTools(ctx context.Context, request *Request[ListToolsRequestParams]) (*Result[ListToolsResultData], error)
ListTools implements the jsonrpc2.HandlerFunc
func (*Server) ReadResource ¶
func (s *Server) ReadResource(ctx context.Context, request *Request[ReadResourceRequestParams]) (*Result[ReadResourceResultData], error)
ReadResource reads a resource.
func (*Server) SSEHandler ¶
SSEHandler returns a handler for the SSE transport.
type ServerInfoData ¶
ServerInfoData is the data for the server info.
type ServerOption ¶
type ServerOption func(*Server)
ServerOption is a function that configures a Server.
func WithCustomHandler ¶
func WithCustomHandler[Params, Result any](method string, handler jsonrpc2.Handler[Params, Result]) ServerOption
WithCustomHandler sets a custom handler for a method. You can use this to override the default handlers.
func WithCustomHandlerFunc ¶
func WithCustomHandlerFunc[Params, Result any](method string, handler func(ctx context.Context, params Params) (Result, error)) ServerOption
WithCustomHandlerFunc sets a custom handler for a method. You can use this to override the default handlers.
func WithLogger ¶ added in v0.0.6
func WithLogger(logger *slog.Logger) ServerOption
WithLogger sets a logger for the server.
func WithResource ¶
func WithResource(resource Resource) ServerOption
WithResource sets a resource for the server.
func WithResourceReader ¶
func WithResourceReader(reader ResourceReader) ServerOption
WithResourceReader sets a resource reader for the server.
func WithResourceTemplate ¶
func WithResourceTemplate(template ResourceTemplate) ServerOption
WithResourceTemplate sets a resource template for the server.
func WithTool ¶
func WithTool[Input, Output any](tool Tool[Input, Output]) ServerOption
WithTool sets a tool for the server.
type TextContent ¶
type TextContent struct {
Text string `json:"text"`
}
TextContent is the text content of the tool call result. TODO: add Annotations field
func (TextContent) MarshalJSON ¶
func (t TextContent) MarshalJSON() ([]byte, error)
MarshalJSON implements json.Marshaler.
type TextResourceContents ¶
type TextResourceContents struct {
// URI is the unique identifier of the resource.
URI string `json:"uri"`
// MimeType is the MIME type of the resource.
MimeType string `json:"mimeType,omitempty,omitzero"`
// Text is the text content of the resource.
Text string `json:"text"`
}
TextResourceContents is the contents of a text resource.
type Tool ¶
type Tool[Input, Output any] struct { // Name is the name of the tool. Name string `json:"name"` // Description is the description of the tool. Description string `json:"description"` // InputSchema is the schema of the tool's input. InputSchema jsonschema.Object `json:"inputSchema"` // Handler is the handler of the tool. Handler ToolHandler[Input, Output] `json:"-"` }
Tool is a tool definition in the MCP.
func NewTool ¶
func NewTool[Input, Output any](name, description string, inputSchema jsonschema.Object, handler ToolHandler[Input, Output]) Tool[Input, Output]
NewTool creates a new tool.
func NewToolFunc ¶
func NewToolFunc[Input, Output any](name, description string, inputSchema jsonschema.Object, handler func(ctx context.Context, input Input) (Output, error)) Tool[Input, Output]
NewToolFunc creates a new tool with a handler function.
func (Tool[Input, Output]) Handle ¶
func (t Tool[Input, Output]) Handle(ctx context.Context, input json.RawMessage) (*ToolCallResultData, error)
Handle handles the tool call.
type ToolCallRequestParams ¶
type ToolCallRequestParams struct {
Name string `json:"name"`
Arguments json.RawMessage `json:"arguments"`
}
ToolCallRequestParams is the parameters of the tool call request.
type ToolCallResultData ¶
type ToolCallResultData struct {
IsError bool `json:"isError"`
Content []IsContent `json:"content"`
}
ToolCallResultData is the result of the tool call.
type ToolHandler ¶
type ToolHandler[Input, Output any] interface { Handle(ctx context.Context, input Input) (Output, error) }
ToolHandler is the handler of the tool. If Handle *ToolCallResultData, it returns the result as is. If Handle returns a slice, it converts each element to the Content type. Otherwise, it returns the result as the ToolCallResultData with single Content.
type ToolHandlerFunc ¶
ToolHandlerFunc is a function that implements ToolHandler.
type ToolsCapabilities ¶
type ToolsCapabilities struct {
ListChanged bool `json:"listChanged,omitempty,omitzero"`
}
ToolsCapabilities is the capabilities for the tools feature.