Documentation
¶
Overview ¶
Package mcp implements the Model Context Protocol (MCP) client. It supports connecting to MCP servers via stdio or HTTP SSE transport, listing tools and resources, and invoking tool calls.
Index ¶
- func RegisterTools(ctx context.Context, client *Client, registry *tool.Registry) (int, error)
- func ToolInfoToDefinitions(client *Client, tools []ToolInfo) []*tool.Definition
- func ToolInfoToJSON(tools []ToolInfo) ([]byte, error)
- type Client
- func (c *Client) CallTool(ctx context.Context, name string, args map[string]any) (any, error)
- func (c *Client) Close() error
- func (c *Client) Connect(ctx context.Context) error
- func (c *Client) Info() ServerInfo
- func (c *Client) ListResources(ctx context.Context) ([]ResourceInfo, error)
- func (c *Client) ListTools(ctx context.Context) ([]ToolInfo, error)
- func (c *Client) ReadResource(ctx context.Context, uri string) ([]ResourceContent, error)
- type ResourceContent
- type ResourceInfo
- type ServerConfig
- type ServerInfo
- type ToolInfo
- type Transport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterTools ¶
RegisterTools fetches tools from the MCP server and registers them in the given tool registry. Each MCP tool becomes a tool.Definition whose handler routes calls through the MCP client.
func ToolInfoToDefinitions ¶
func ToolInfoToDefinitions(client *Client, tools []ToolInfo) []*tool.Definition
ToolInfoToDefinitions converts MCP ToolInfo items into tool.Definition items without registering them. Useful for inspection or custom registration.
func ToolInfoToJSON ¶
ToolInfoToJSON converts tool info into a JSON representation suitable for model tool definitions.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client communicates with an MCP server using JSON-RPC 2.0 over stdio.
func NewClient ¶
func NewClient(cfg ServerConfig) (*Client, error)
NewClient creates a new MCP client for the given server configuration. For stdio transport, it launches the command as a subprocess.
func (*Client) Connect ¶
Connect starts the MCP server process and performs the initialize handshake.
func (*Client) Info ¶
func (c *Client) Info() ServerInfo
Info returns the server information from the initialize handshake.
func (*Client) ListResources ¶
func (c *Client) ListResources(ctx context.Context) ([]ResourceInfo, error)
ListResources fetches the available resources from the MCP server.
func (*Client) ReadResource ¶
ReadResource fetches the content of a resource by URI.
type ResourceContent ¶
type ResourceContent struct {
URI string `json:"uri"`
MimeType string `json:"mimeType,omitempty"`
Text string `json:"text,omitempty"`
}
ResourceContent holds the content returned when reading a resource.
type ResourceInfo ¶
type ResourceInfo struct {
URI string `json:"uri"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
ResourceInfo describes a resource provided by an MCP server.
type ServerConfig ¶
type ServerConfig struct {
Name string `json:"name" yaml:"name"`
Transport Transport `json:"transport" yaml:"transport"`
Command string `json:"command,omitempty" yaml:"command,omitempty"`
Args []string `json:"args,omitempty" yaml:"args,omitempty"`
URL string `json:"url,omitempty" yaml:"url,omitempty"`
}
ServerConfig holds the connection configuration for an MCP server.
type ServerInfo ¶
type ServerInfo struct {
Name string `json:"name"`
Version string `json:"version"`
ProtocolVer string `json:"protocolVersion"`
}
ServerInfo holds the server's initialization response.