mcp

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2025 License: MIT Imports: 17 Imported by: 2

Documentation

Index

Constants

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

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

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

type ImageContent struct {
	Data     []byte `json:"data"`
	MimeType string `json:"mimeType"`
}

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

type Notification[Params any] struct {
	Meta   map[string]any
	Params Params
}

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

func (r *Request[Params]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Request[Params]) UnmarshalJSON

func (r *Request[Params]) UnmarshalJSON(data []byte) error

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

Handle registers a new route with a handler.

func (*ResourceReaderMux) HandleFunc

HandleFunc registers a new route with a handler function.

func (*ResourceReaderMux) ReadResource

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

type Result[Data any] struct {
	Meta map[string]any
	Data Data
}

func (*Result[Data]) MarshalJSON

func (r *Result[Data]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Result[Data]) UnmarshalJSON

func (r *Result[Data]) UnmarshalJSON(data []byte) error

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

CallTool implements the jsonrpc2.HandlerFunc

func (*Server) Close

func (s *Server) Close() error

Close closes the server.

func (*Server) HandleSession

func (s *Server) HandleSession(ctx context.Context, id uint64, t transport.Session) error

HandleSession handles a session.

func (*Server) Initialize

Initialize initializes the server.

func (*Server) Initialized added in v0.0.8

func (s *Server) Initialized(ctx context.Context, params struct{}) (struct{}, error)

func (*Server) ListResourceTemplates

ListResourceTemplates lists resource templates.

func (*Server) ListResources

ListResources lists resources.

func (*Server) ListTools

ListTools implements the jsonrpc2.HandlerFunc

func (*Server) Ping

func (s *Server) Ping(ctx context.Context, _ *Request[struct{}]) (*Result[struct{}], error)

func (*Server) ReadResource

ReadResource reads a resource.

func (*Server) SSEHandler

func (s *Server) SSEHandler(baseURL string) (http.Handler, error)

SSEHandler returns a handler for the SSE transport.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, id uint64, t transport.Session) error

Serve starts the server.

func (*Server) ServeStdio

func (s *Server) ServeStdio(ctx context.Context) error

ServeStdio serves the server over stdin and stdout.

type ServerInfoData

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

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.

func (Tool[Input, Output]) Validate

func (t Tool[Input, Output]) Validate(v json.RawMessage) error

Validate validates the input.

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

type ToolHandlerFunc[Input, Output any] func(ctx context.Context, input Input) (Output, error)

ToolHandlerFunc is a function that implements ToolHandler.

func (ToolHandlerFunc[Input, Output]) Handle

func (f ToolHandlerFunc[Input, Output]) Handle(ctx context.Context, input Input) (Output, error)

Handle implements ToolHandler.

type ToolsCapabilities

type ToolsCapabilities struct {
	ListChanged bool `json:"listChanged,omitempty,omitzero"`
}

ToolsCapabilities is the capabilities for the tools feature.

Jump to

Keyboard shortcuts

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