Documentation
¶
Overview ¶
Package mcp provides Model Context Protocol (MCP) client functionality. MCP enables connecting to external servers that provide tools and resources for AI models.
Index ¶
- type Client
- func (c *Client) Connect(ctx context.Context, config ServerConfig) error
- func (c *Client) Disconnect(name string) error
- func (c *Client) DisconnectAll() error
- func (c *Client) GetResources() []Resource
- func (c *Client) GetTools() tool.Set
- func (c *Client) ReadResource(ctx context.Context, uri string) (*ResourceContent, error)
- type HTTPTransport
- type MockContent
- type MockResource
- type MockResourceContent
- type MockTool
- type MockToolResult
- type MockTransport
- type Resource
- type ResourceContent
- type SSETransport
- type ServerConfig
- type ServerConnection
- type StdioTransport
- type Transport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client manages connections to MCP servers.
func (*Client) Connect ¶
func (c *Client) Connect(ctx context.Context, config ServerConfig) error
Connect connects to an MCP server.
func (*Client) Disconnect ¶
Disconnect disconnects from an MCP server.
func (*Client) DisconnectAll ¶
DisconnectAll disconnects from all MCP servers.
func (*Client) GetResources ¶
GetResources returns all resources from all connected servers.
func (*Client) ReadResource ¶
ReadResource reads the content of a resource.
type HTTPTransport ¶
type HTTPTransport struct {
// contains filtered or unexported fields
}
HTTPTransport implements the Transport interface using HTTP. This is a simpler transport that uses HTTP POST for request/response.
func NewHTTPTransport ¶
func NewHTTPTransport(url string, headers map[string]string) *HTTPTransport
NewHTTPTransport creates a new HTTP transport.
func (*HTTPTransport) Close ¶
func (t *HTTPTransport) Close() error
Close closes the HTTP transport.
func (*HTTPTransport) Connect ¶
func (t *HTTPTransport) Connect(ctx context.Context) error
Connect initializes the HTTP transport. For HTTP transport, this is a no-op since connections are made per-request.
func (*HTTPTransport) OnNotification ¶
func (t *HTTPTransport) OnNotification(handler func(method string, params json.RawMessage))
OnNotification registers a handler for server notifications. Note: HTTP transport doesn't support server-initiated notifications.
func (*HTTPTransport) Send ¶
func (t *HTTPTransport) Send(ctx context.Context, method string, params any) (json.RawMessage, error)
Send sends a JSON-RPC request via HTTP POST. Notifications (methods starting with "notifications/") are sent without an id and the response body is not parsed (server responds with 202 or 200).
type MockContent ¶
MockContent is a content block in a tool result.
type MockResource ¶
type MockResource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
MockResource defines a resource in the mock server.
func DefaultMockResources ¶
func DefaultMockResources() []MockResource
DefaultMockResources returns the default resources used by MockTransport.
type MockResourceContent ¶
type MockResourceContent struct {
URI string `json:"uri"`
Text string `json:"text,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
MockResourceContent defines the content returned for a resource.
func DefaultMockResourceContents ¶
func DefaultMockResourceContents() []MockResourceContent
DefaultMockResourceContents returns the default resource contents.
type MockTool ¶
type MockTool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema json.RawMessage `json:"inputSchema"`
}
MockTool defines a tool in the mock server.
func DefaultMockTools ¶
func DefaultMockTools() []MockTool
DefaultMockTools returns the default tools used by MockTransport.
type MockToolResult ¶
type MockToolResult struct {
Content []MockContent `json:"content"`
IsError bool `json:"isError,omitempty"`
}
MockToolResult defines a custom tool call result.
type MockTransport ¶
type MockTransport struct {
Tools []MockTool
Resources []MockResource
ResourceContents []MockResourceContent
ToolCallResults map[string]MockToolResult // tool name -> custom result
// FailOnInvalidToolParams makes tools/call return an error for any call.
FailOnInvalidToolParams bool
// InitializeResult overrides the initialize response.
InitializeResult json.RawMessage
// SendError makes Connect return an error.
SendError bool
// contains filtered or unexported fields
}
MockTransport implements the Transport interface for testing. It simulates an MCP server in-memory without any network I/O. Mirrors ai-sdk: packages/mcp/src/tool/mock-mcp-transport.ts
func NewMockTransport ¶
func NewMockTransport() *MockTransport
NewMockTransport creates a MockTransport with default tools and resources.
func (*MockTransport) Close ¶
func (t *MockTransport) Close() error
func (*MockTransport) OnNotification ¶
func (t *MockTransport) OnNotification(handler func(method string, params json.RawMessage))
func (*MockTransport) Send ¶
func (t *MockTransport) Send(ctx context.Context, method string, params any) (json.RawMessage, error)
type Resource ¶
type Resource struct {
// URI is the unique identifier for the resource.
URI string
// Name is the human-readable name.
Name string
// Description describes the resource.
Description string
// MimeType is the content type.
MimeType string
}
Resource represents an MCP resource.
type ResourceContent ¶
type ResourceContent struct {
// URI is the resource URI.
URI string
// MimeType is the content type.
MimeType string
// Text is the text content (for text resources).
Text string
// Blob is the binary content (for binary resources, base64 encoded).
Blob string
}
ResourceContent contains the content of a resource.
type SSETransport ¶
type SSETransport struct {
// contains filtered or unexported fields
}
SSETransport implements the Transport interface using Server-Sent Events.
func NewSSETransport ¶
func NewSSETransport(url string, headers map[string]string) *SSETransport
NewSSETransport creates a new SSE transport.
func (*SSETransport) Connect ¶
func (t *SSETransport) Connect(ctx context.Context) error
Connect establishes the SSE connection.
func (*SSETransport) OnNotification ¶
func (t *SSETransport) OnNotification(handler func(method string, params json.RawMessage))
OnNotification registers a handler for server notifications.
func (*SSETransport) Send ¶
func (t *SSETransport) Send(ctx context.Context, method string, params any) (json.RawMessage, error)
Send sends a JSON-RPC request via HTTP POST.
type ServerConfig ¶
type ServerConfig struct {
// Name is a unique identifier for this server.
Name string
// Transport specifies how to connect to the server.
// Options: "stdio", "sse", "http"
Transport string
// Command is the command to run for stdio transport.
Command string
// Args are arguments for the command.
Args []string
// Env are environment variables for the command.
Env map[string]string
// URL is the server URL for sse/http transport.
URL string
// Headers are HTTP headers for sse/http transport.
Headers map[string]string
}
ServerConfig contains configuration for an MCP server.
type ServerConnection ¶
type ServerConnection struct {
// contains filtered or unexported fields
}
ServerConnection represents a connection to an MCP server.
type StdioTransport ¶
type StdioTransport struct {
// contains filtered or unexported fields
}
StdioTransport implements the Transport interface using stdio.
func NewStdioTransport ¶
func NewStdioTransport(command string, args []string, env map[string]string) *StdioTransport
NewStdioTransport creates a new stdio transport.
func (*StdioTransport) Close ¶
func (t *StdioTransport) Close() error
Close terminates the subprocess.
func (*StdioTransport) Connect ¶
func (t *StdioTransport) Connect(ctx context.Context) error
Connect starts the subprocess and establishes communication.
func (*StdioTransport) OnNotification ¶
func (t *StdioTransport) OnNotification(handler func(method string, params json.RawMessage))
OnNotification registers a handler for server notifications.
func (*StdioTransport) Send ¶
func (t *StdioTransport) Send(ctx context.Context, method string, params any) (json.RawMessage, error)
Send sends a JSON-RPC request and waits for the response. Notifications (methods starting with "notifications/") are fire-and-forget: sent without an id and no response is expected.
type Transport ¶
type Transport interface {
// Connect establishes the connection.
Connect(ctx context.Context) error
// Close closes the connection.
Close() error
// Send sends a request and returns the response.
Send(ctx context.Context, method string, params any) (json.RawMessage, error)
// OnNotification registers a handler for server notifications.
OnNotification(handler func(method string, params json.RawMessage))
}
Transport is the interface for MCP transports.