mcp

package
v0.16.119 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mcp provides support for Message Control Protocol (MCP) transports, including local and SSE-based communication for distributed agentic flows.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotations

type Annotations struct {
	// Describes who the intended customer of this object or data is.
	//
	// It can include multiple entries to indicate ToolResponse useful for multiple
	// audiences (e.g., `["user", "assistant"]`).
	Audience []Role `json:"audience,omitempty" yaml:"audience,omitempty" mapstructure:"audience,omitempty"`

	// Describes how important this data is for operating the server.
	//
	// A value of 1 means "most important," and indicates that the data is
	// effectively required, while 0 means "least important," and indicates that
	// the data is entirely optional.
	Priority *float64 `json:"priority,omitempty" yaml:"priority,omitempty" mapstructure:"priority,omitempty"`
}

type BlobResourceContents

type BlobResourceContents struct {
	// A base64-encoded string representing the binary data of the item.
	Blob string `json:"blob" yaml:"blob" mapstructure:"blob"`

	// The MIME type of this resource, if known.
	MimeType *string `json:"mimeType,omitempty" yaml:"mimeType,omitempty" mapstructure:"mimeType,omitempty"`

	// The URI of this resource.
	Uri string `json:"uri" yaml:"uri" mapstructure:"uri"`
}

type CallToolResult

type CallToolResult struct {
	// This result property is reserved by the protocol to allow clients and servers
	// to attach additional metadata to their responses.
	Meta map[string]any `json:"_meta,omitempty" yaml:"_meta,omitempty" mapstructure:"_meta,omitempty"`

	// Content corresponds to the JSON schema field "content".
	Content []*Content `json:"content" yaml:"content" mapstructure:"content"`

	// Whether the tool call ended in an error.
	//
	// If not set, this is assumed to be false (the call was successful).
	IsError *bool `json:"isError,omitempty" yaml:"isError,omitempty" mapstructure:"isError,omitempty"`

	// NEW in MCP v2025-06-18
	StructuredContent any `json:"structuredContent,omitempty" yaml:"structuredContent,omitempty" mapstructure:"structuredContent,omitempty"`
}

type Client

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

Client represents an MCP client that can connect to and interact with MCP servers

func NewClient

func NewClient(transport transport.Transport) *Client

NewClient creates a new MCP client with the specified transport

func NewClientWithInfo

func NewClientWithInfo(transport transport.Transport, info ClientInfo) *Client

NewClientWithInfo create a new client with info. This is required by anthorpic mcp tools

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, arguments any) (*ToolResponse, error)

CallTool calls a specific tool on the server with the provided arguments

func (*Client) GetCapabilities

func (c *Client) GetCapabilities() *ServerCapabilities

GetCapabilities returns the server capabilities obtained during initialization

func (*Client) GetPrompt

func (c *Client) GetPrompt(ctx context.Context, name string, arguments any) (*PromptResponse, error)

GetPrompt retrieves a specific prompt from the server

func (*Client) Initialize

func (c *Client) Initialize(ctx context.Context) (*InitializeResponse, error)

Initialize connects to the server and retrieves its capabilities

func (*Client) ListPrompts

func (c *Client) ListPrompts(ctx context.Context, cursor *string) (*ListPromptsResponse, error)

ListPrompts retrieves the list of available prompts from the server

func (*Client) ListResources

func (c *Client) ListResources(ctx context.Context, cursor *string) (*ListResourcesResponse, error)

ListResources retrieves the list of available resources from the server

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context, cursor *string) (*ToolsResponse, error)

ListTools retrieves the list of available tools from the server

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping sends a ping request to the server to check connectivity

func (*Client) ReadResource

func (c *Client) ReadResource(ctx context.Context, uri string) (*ResourceResponse, error)

ReadResource reads a specific resource from the server

type ClientInfo

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

type Content

type Content struct {
	Type             ContentType
	TextContent      *TextContent
	ImageContent     *ImageContent
	EmbeddedResource *EmbeddedResource
	Annotations      *Annotations
}

func NewBlobResourceContent

func NewBlobResourceContent(uri string, base64EncodedData string, mimeType string) *Content

NewBlobResourceContent creates a new ToolResponse that is a blob of binary data. The given data is base64-encoded; the client will decode it. The client will render this as a blob; it will not be human-readable.

func NewImageContent

func NewImageContent(base64EncodedStringData string, mimeType string) *Content

NewImageContent creates a new ToolResponse that is an image. The given data is base64-encoded

func NewTextContent

func NewTextContent(content string) *Content

NewTextContent creates a new ToolResponse that is a simple text string. The client will render this as a single string.

func NewTextResourceContent

func NewTextResourceContent(uri string, text string, mimeType string) *Content

NewTextResourceContent creates a new ToolResponse that is an embedded resource of type "text". The given text is embedded in the response as a TextResourceContents, which contains the given MIME type and URI. The text is not base64-encoded.

func (Content) MarshalJSON

func (c Content) MarshalJSON() ([]byte, error)

Custom JSON marshaling for ToolResponse Content

func (*Content) UnmarshalJSON

func (c *Content) UnmarshalJSON(b []byte) error

func (*Content) WithAnnotations

func (c *Content) WithAnnotations(annotations Annotations) *Content

type ContentType

type ContentType string
const (
	// The value is the value of the "type" field in the Content so do not change
	ContentTypeText             ContentType = "text"
	ContentTypeImage            ContentType = "image"
	ContentTypeEmbeddedResource ContentType = "resource"
)

type EmbeddedResource

type EmbeddedResource struct {
	EmbeddedResourceType embeddedResourceType
	TextResourceContents *TextResourceContents
	BlobResourceContents *BlobResourceContents
}

The contents of a resource, embedded into a prompt or tool call result.

It is up to the client how best to render embedded resources for the benefit of the LLM and/or the user.

func NewBlobEmbeddedResource

func NewBlobEmbeddedResource(uri string, base64EncodedData string, mimeType string) *EmbeddedResource

func NewTextEmbeddedResource

func NewTextEmbeddedResource(uri string, text string, mimeType string) *EmbeddedResource

func (EmbeddedResource) MarshalJSON

func (c EmbeddedResource) MarshalJSON() ([]byte, error)

Custom JSON marshaling for EmbeddedResource

func (*EmbeddedResource) UnmarshalJSON

func (c *EmbeddedResource) UnmarshalJSON(data []byte) error

Custom JSON unmarshaling for EmbeddedResource

type ImageContent

type ImageContent struct {
	// The base64-encoded image data.
	Data string `json:"data" yaml:"data" mapstructure:"data"`

	// The MIME type of the image. Different providers may support different image
	// types.
	MimeType string `json:"mimeType" yaml:"mimeType" mapstructure:"mimeType"`
}

An image provided to or from an LLM.

type InitializeResponse

type InitializeResponse struct {
	// This result property is reserved by the protocol to allow clients and servers
	// to attach additional metadata to their responses.
	Meta initializeResultMeta `json:"_meta,omitempty" yaml:"_meta,omitempty" mapstructure:"_meta,omitempty"`

	// Capabilities corresponds to the JSON schema field "capabilities".
	Capabilities ServerCapabilities `json:"capabilities" yaml:"capabilities" mapstructure:"capabilities"`

	// Instructions describing how to use the server and its features.
	//
	// This can be used by clients to improve the LLM's understanding of available
	// tools, resources, etc. It can be thought of like a "hint" to the model. For
	// example, this information MAY be added to the system prompt.
	Instructions *string `json:"instructions,omitempty" yaml:"instructions,omitempty" mapstructure:"instructions,omitempty"`

	// The version of the Model Context Protocol that the server wants to use. This
	// may not match the version that the client requested. If the client cannot
	// support this version, it MUST disconnect.
	ProtocolVersion string `json:"protocolVersion" yaml:"protocolVersion" mapstructure:"protocolVersion"`

	// ServerInfo corresponds to the JSON schema field "serverInfo".
	ServerInfo implementation `json:"serverInfo" yaml:"serverInfo" mapstructure:"serverInfo"`
}

After receiving an initialize request from the client, the server sends this response.

func (*InitializeResponse) UnmarshalJSON

func (j *InitializeResponse) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaler.

type ListPromptsResponse

type ListPromptsResponse struct {
	// Prompts corresponds to the JSON schema field "prompts".
	Prompts []*PromptSchema `json:"prompts" yaml:"prompts" mapstructure:"prompts"`
	// NextCursor is a cursor for pagination. If not nil, there are more prompts available.
	NextCursor *string `json:"nextCursor,omitempty" yaml:"nextCursor,omitempty" mapstructure:"nextCursor,omitempty"`
}

The server's response to a prompts/list request from the client.

type ListResourceTemplatesResponse

type ListResourceTemplatesResponse struct {
	// Templates corresponds to the JSON schema field "templates".
	Templates []*ResourceTemplateSchema `json:"resourceTemplates" yaml:"resourceTemplates" mapstructure:"resourceTemplates"`
	// NextCursor is a cursor for pagination. If not nil, there are more templates available.
	NextCursor *string `json:"nextCursor,omitempty" yaml:"nextCursor,omitempty" mapstructure:"nextCursor,omitempty"`
}

The server's response to a resources/templates/list request from the client.

type ListResourcesResponse

type ListResourcesResponse struct {
	// Resources corresponds to the JSON schema field "resources".
	Resources []*ResourceSchema `json:"resources" yaml:"resources" mapstructure:"resources"`
	// NextCursor is a cursor for pagination. If not nil, there are more resources available.
	NextCursor *string `json:"nextCursor,omitempty" yaml:"nextCursor,omitempty" mapstructure:"nextCursor,omitempty"`
}

The server's response to a resources/list request from the client.

type PromptMessage

type PromptMessage struct {
	Content *Content `json:"content" yaml:"content" mapstructure:"content"`
	Role    Role     `json:"role" yaml:"role" mapstructure:"role"`
}

func NewPromptMessage

func NewPromptMessage(content *Content, role Role) *PromptMessage

type PromptResponse

type PromptResponse struct {
	// An optional description for the prompt.
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// Messages corresponds to the JSON schema field "messages".
	Messages []*PromptMessage `json:"messages" yaml:"messages" mapstructure:"messages"`
}

The server's response to a prompts/get request from the client.

func NewPromptResponse

func NewPromptResponse(description string, messages ...*PromptMessage) *PromptResponse

type PromptSchema

type PromptSchema struct {
	// A list of arguments to use for templating the prompt.
	Arguments []PromptSchemaArgument `json:"arguments,omitempty" yaml:"arguments,omitempty" mapstructure:"arguments,omitempty"`

	// An optional description of what this prompt provides
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// The name of the prompt or prompt template.
	Name string `json:"name" yaml:"name" mapstructure:"name"`
}

A PromptSchema or prompt template that the server offers.

type PromptSchemaArgument

type PromptSchemaArgument struct {
	// A human-readable description of the argument.
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// The name of the argument.
	Name string `json:"name" yaml:"name" mapstructure:"name"`

	// Whether this argument must be provided.
	Required *bool `json:"required,omitempty" yaml:"required,omitempty" mapstructure:"required,omitempty"`
}

type ResourceResponse

type ResourceResponse struct {
	Contents []*EmbeddedResource `json:"contents"`
}

func NewResourceResponse

func NewResourceResponse(contents ...*EmbeddedResource) *ResourceResponse

type ResourceSchema

type ResourceSchema struct {
	// Annotations corresponds to the JSON schema field "annotations".
	Annotations *Annotations `json:"annotations,omitempty" yaml:"annotations,omitempty" mapstructure:"annotations,omitempty"`

	// A description of what this resource represents.
	//
	// This can be used by clients to improve the LLM's understanding of available
	// resources. It can be thought of like a "hint" to the model.
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// The MIME type of this resource, if known.
	MimeType *string `json:"mimeType,omitempty" yaml:"mimeType,omitempty" mapstructure:"mimeType,omitempty"`

	// A human-readable name for this resource.
	//
	// This can be used by clients to populate UI elements.
	Name string `json:"name" yaml:"name" mapstructure:"name"`

	// The URI of this resource.
	Uri string `json:"uri" yaml:"uri" mapstructure:"uri"`
}

A known resource that the server is capable of reading.

type ResourceTemplateSchema

type ResourceTemplateSchema struct {
	// Annotations corresponds to the JSON schema field "annotations".
	Annotations *Annotations `json:"annotations,omitempty" yaml:"annotations,omitempty" mapstructure:"annotations,omitempty"`

	// A description of what resources matching this template represent.
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// The MIME type of resources matching this template, if known.
	MimeType *string `json:"mimeType,omitempty" yaml:"mimeType,omitempty" mapstructure:"mimeType,omitempty"`

	// A human-readable name for this template.
	Name string `json:"name" yaml:"name" mapstructure:"name"`

	// The URI template following RFC 6570.
	UriTemplate string `json:"uriTemplate" yaml:"uriTemplate" mapstructure:"uriTemplate"`
}

A resource template that defines a pattern for dynamic resources.

type Role

type Role string
const RoleAssistant Role = "assistant"
const RoleUser Role = "user"

type Server

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

func NewServer

func NewServer(transport transport.Transport, options ...ServerOptions) *Server

func (*Server) CheckPromptRegistered

func (s *Server) CheckPromptRegistered(name string) bool

func (*Server) CheckResourceRegistered

func (s *Server) CheckResourceRegistered(uri string) bool

func (*Server) CheckResourceTemplateRegistered

func (s *Server) CheckResourceTemplateRegistered(uriTemplate string) bool

func (*Server) CheckToolRegistered

func (s *Server) CheckToolRegistered(name string) bool

func (*Server) DeregisterPrompt

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

func (*Server) DeregisterResource

func (s *Server) DeregisterResource(uri string) error

func (*Server) DeregisterResourceTemplate

func (s *Server) DeregisterResourceTemplate(uriTemplate string) error

func (*Server) DeregisterTool

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

func (*Server) RegisterPrompt

func (s *Server) RegisterPrompt(name string, description string, handler any) error

func (*Server) RegisterResource

func (s *Server) RegisterResource(uri string, name string, description string, mimeType string, handler any) error

func (*Server) RegisterResourceTemplate

func (s *Server) RegisterResourceTemplate(uriTemplate string, name string, description string, mimeType string) error

func (*Server) RegisterTool

func (s *Server) RegisterTool(name string, description string, handler any) error

RegisterTool registers a new tool with the server

func (*Server) Serve

func (s *Server) Serve() error

type ServerCapabilities

type ServerCapabilities struct {
	// Experimental, non-standard capabilities that the server supports.
	Experimental ServerCapabilitiesExperimental `json:"experimental,omitempty" yaml:"experimental,omitempty" mapstructure:"experimental,omitempty"`

	// Present if the server supports sending log messages to the client.
	Logging ServerCapabilitiesLogging `json:"logging,omitempty" yaml:"logging,omitempty" mapstructure:"logging,omitempty"`

	// Present if the server offers any prompt templates.
	Prompts *ServerCapabilitiesPrompts `json:"prompts,omitempty" yaml:"prompts,omitempty" mapstructure:"prompts,omitempty"`

	// Present if the server offers any resources to read.
	Resources *ServerCapabilitiesResources `json:"resources,omitempty" yaml:"resources,omitempty" mapstructure:"resources,omitempty"`

	// Present if the server offers any tools to call.
	Tools *ServerCapabilitiesTools `json:"tools,omitempty" yaml:"tools,omitempty" mapstructure:"tools,omitempty"`
}

Capabilities that a server may support. Known capabilities are defined here, in this schema, but this is not a closed set: any server can define its own, additional capabilities.

type ServerCapabilitiesExperimental

type ServerCapabilitiesExperimental map[string]map[string]any

Experimental, non-standard capabilities that the server supports.

type ServerCapabilitiesLogging

type ServerCapabilitiesLogging map[string]any

Present if the server supports sending log messages to the client.

type ServerCapabilitiesPrompts

type ServerCapabilitiesPrompts struct {
	// Whether this server supports notifications for changes to the prompt list.
	ListChanged *bool `json:"listChanged,omitempty" yaml:"listChanged,omitempty" mapstructure:"listChanged,omitempty"`
}

Present if the server offers any prompt templates.

type ServerCapabilitiesResources

type ServerCapabilitiesResources struct {
	// Whether this server supports notifications for changes to the resource list.
	ListChanged *bool `json:"listChanged,omitempty" yaml:"listChanged,omitempty" mapstructure:"listChanged,omitempty"`

	// Whether this server supports subscribing to resource updates.
	Subscribe *bool `json:"subscribe,omitempty" yaml:"subscribe,omitempty" mapstructure:"subscribe,omitempty"`
}

Present if the server offers any resources to read.

type ServerCapabilitiesTools

type ServerCapabilitiesTools struct {
	// Whether this server supports notifications for changes to the tool list.
	ListChanged *bool `json:"listChanged,omitempty" yaml:"listChanged,omitempty" mapstructure:"listChanged,omitempty"`
}

Present if the server offers any tools to call.

type ServerOptions

type ServerOptions func(*Server)

func WithInstructions

func WithInstructions(instructions string) ServerOptions

func WithName

func WithName(name string) ServerOptions

func WithPaginationLimit

func WithPaginationLimit(limit int) ServerOptions

Beware: As of 2024-12-13, it looks like Claude does not support pagination yet

func WithProtocol

func WithProtocol(protocol *protocol.Protocol) ServerOptions

func WithVersion

func WithVersion(version string) ServerOptions

type TextContent

type TextContent struct {
	// The text ToolResponse of the message.
	Text string `json:"text" yaml:"text" mapstructure:"text"`
}

Text provided to or from an LLM.

type TextResourceContents

type TextResourceContents struct {
	// The MIME type of this resource, if known.
	MimeType *string `json:"mimeType,omitempty" yaml:"mimeType,omitempty" mapstructure:"mimeType,omitempty"`

	// The text of the item. This must only be set if the item can actually be
	// represented as text (not binary data).
	Text string `json:"text" yaml:"text" mapstructure:"text"`

	// The URI of this resource.
	Uri string `json:"uri" yaml:"uri" mapstructure:"uri"`
}

type ToolResponse

type ToolResponse struct {
	// This result property is reserved by the protocol to allow clients and servers
	// to attach additional metadata to their responses.
	Meta map[string]any `json:"_meta,omitempty" yaml:"_meta,omitempty" mapstructure:"_meta,omitempty"`

	Content []*Content `json:"content" yaml:"content" mapstructure:"content"`
	// NEW in MCP v2025-06-18
	StructuredContent any `json:"structuredContent,omitempty" yaml:"structuredContent,omitempty" mapstructure:"structuredContent,omitempty"`
}

This is a union type of all the different ToolResponse that can be sent back to the client. We allow creation through constructors only to make sure that the ToolResponse is valid.

func NewToolResponse

func NewToolResponse(content ...*Content) *ToolResponse

func (*ToolResponse) WithMeta

func (t *ToolResponse) WithMeta(meta map[string]any) *ToolResponse

func (*ToolResponse) WithStructuredContent

func (t *ToolResponse) WithStructuredContent(resp any) *ToolResponse

type ToolRetType

type ToolRetType struct {
	// A human-readable description of the tool.
	Description *string `json:"description,omitempty" yaml:"description,omitempty" mapstructure:"description,omitempty"`

	// A JSON Schema object defining the expected parameters for the tool.
	InputSchema any `json:"inputSchema" yaml:"inputSchema" mapstructure:"inputSchema"`

	// The name of the tool.
	Name string `json:"name" yaml:"name" mapstructure:"name"`
}

Definition for a tool the client can call.

type ToolsResponse

type ToolsResponse struct {
	Tools      []ToolRetType `json:"tools" yaml:"tools" mapstructure:"tools"`
	NextCursor *string       `json:"nextCursor,omitempty" yaml:"nextCursor,omitempty" mapstructure:"nextCursor,omitempty"`
}

Directories

Path Synopsis
internal
protocol
This file implements the core protocol layer for JSON-RPC communication in the MCP SDK.
This file implements the core protocol layer for JSON-RPC communication in the MCP SDK.
localtransport
Package localtransport implements local transport mechanisms for MCP, enabling efficient in-process and local communication for agentic flows.
Package localtransport implements local transport mechanisms for MCP, enabling efficient in-process and local communication for agentic flows.
sse
Package sse implements Server-Sent Events (SSE) transport for MCP, enabling real-time, bidirectional communication for agentic flows over HTTP.
Package sse implements Server-Sent Events (SSE) transport for MCP, enabling real-time, bidirectional communication for agentic flows over HTTP.
sse/internal/sse
Package sse provides the low-level implementation of Server-Sent Events (SSE) transport for MCP, handling connection management, message streaming, and protocol details.
Package sse provides the low-level implementation of Server-Sent Events (SSE) transport for MCP, handling connection management, message streaming, and protocol details.
stdio/internal/stdio
This file implements the stdio transport layer for JSON-RPC communication.
This file implements the stdio transport layer for JSON-RPC communication.

Jump to

Keyboard shortcuts

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