mcp

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrToolNotFound    = errors.New("mcp: tool not found")
	ErrInitFailed      = errors.New("mcp: initialization failed")
	ErrTransportClosed = errors.New("mcp: transport closed")
)

Sentinel errors.

Functions

This section is empty.

Types

type CallResult

type CallResult struct {
	Content []Content `json:"content"`
	IsError bool      `json:"isError"`
}

CallResult is the result of a tools/call request.

type Client

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

Client is a high-level MCP protocol client.

func NewClient

func NewClient(t Transport) *Client

NewClient creates a new MCP client using the given transport.

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, args map[string]any) (*CallResult, error)

CallTool invokes a tool on the MCP server with the given arguments. If the server reports a tool-level error (isError=true), a *ToolError is returned.

func (*Client) Close

func (c *Client) Close() error

Close closes the underlying transport.

func (*Client) Initialize

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

Initialize performs the MCP handshake: sends an "initialize" request, validates the server response, then sends "notifications/initialized".

func (*Client) ListTools

func (c *Client) ListTools(ctx context.Context) ([]Tool, error)

ListTools fetches the list of tools from the MCP server, handling pagination.

func (*Client) Ping added in v1.2.0

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

Ping sends a ping request to the server and returns an error if unreachable.

func (*Client) ServerCapabilities added in v1.2.0

func (c *Client) ServerCapabilities() ServerCapabilities

ServerCapabilities returns the capabilities reported by the server.

func (*Client) ServerInfo added in v1.2.0

func (c *Client) ServerInfo() ServerInfo

ServerInfo returns the server identity information.

type Content

type Content struct {
	Type     string           `json:"type"`
	Text     string           `json:"text,omitempty"`
	Data     string           `json:"data,omitempty"` // base64 for image/audio
	MimeType string           `json:"mimeType,omitempty"`
	Resource *ResourceContent `json:"resource,omitempty"`
}

Content represents a single content block in a call result.

type HTTPTransport added in v1.2.0

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

HTTPTransport implements the MCP Streamable HTTP transport (2025-11-25 spec). It sends JSON-RPC requests via POST and handles both application/json and text/event-stream response content types.

func NewHTTPTransport added in v1.2.0

func NewHTTPTransport(baseURL string, headers map[string]string) *HTTPTransport

NewHTTPTransport creates a new Streamable HTTP transport.

func (*HTTPTransport) Close added in v1.2.0

func (t *HTTPTransport) Close() error

Close sends a DELETE to terminate the session and cleans up.

func (*HTTPTransport) Send added in v1.2.0

func (t *HTTPTransport) Send(ctx context.Context, req *Request) (*Response, error)

Send sends a JSON-RPC request via POST and waits for the response.

func (*HTTPTransport) SendNotification added in v1.2.0

func (t *HTTPTransport) SendNotification(ctx context.Context, notif *Request) error

SendNotification sends a JSON-RPC notification via POST, expecting 202 Accepted.

func (*HTTPTransport) SetNotificationHandler added in v1.2.0

func (t *HTTPTransport) SetNotificationHandler(handler NotificationHandler)

SetNotificationHandler sets a callback for server-initiated notifications.

type InitializeResult added in v1.2.0

type InitializeResult struct {
	ProtocolVersion string             `json:"protocolVersion"`
	Capabilities    ServerCapabilities `json:"capabilities"`
	ServerInfo      ServerInfo         `json:"serverInfo"`
}

InitializeResult holds the server's response to an initialize request.

type InputSchema

type InputSchema struct {
	Type       string                    `json:"type"`
	Properties map[string]PropertySchema `json:"properties"`
	Required   []string                  `json:"required"`
}

InputSchema describes the JSON Schema for a tool's input.

type Notification added in v1.2.0

type Notification struct {
	JSONRPC string          `json:"jsonrpc"`
	Method  string          `json:"method"`
	Params  json.RawMessage `json:"params,omitempty"`
}

Notification represents a server-initiated JSON-RPC notification.

type NotificationAware added in v1.2.0

type NotificationAware interface {
	SetNotificationHandler(handler NotificationHandler)
}

NotificationAware is optionally implemented by transports that support server-initiated notification callbacks.

type NotificationHandler added in v1.2.0

type NotificationHandler func(method string, params json.RawMessage)

NotificationHandler is a callback for server-initiated notifications.

type PropertySchema

type PropertySchema struct {
	Type        string          `json:"type"`
	Description string          `json:"description"`
	Default     any             `json:"default,omitempty"`
	Enum        []any           `json:"enum,omitempty"`
	Items       *PropertySchema `json:"items,omitempty"`
}

PropertySchema describes a single property in a tool's input schema.

type RPCError

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `json:"data,omitempty"`
}

RPCError represents a JSON-RPC 2.0 error object.

func (*RPCError) Error

func (e *RPCError) Error() string

type Request

type Request struct {
	JSONRPC string `json:"jsonrpc"`
	ID      *int64 `json:"id,omitempty"`
	Method  string `json:"method"`
	Params  any    `json:"params,omitempty"`
}

Request represents a JSON-RPC 2.0 request or notification.

type ResourceContent added in v1.2.0

type ResourceContent struct {
	URI      string `json:"uri"`
	MimeType string `json:"mimeType,omitempty"`
	Text     string `json:"text,omitempty"`
	Blob     string `json:"blob,omitempty"` // base64
}

ResourceContent represents an embedded resource in a content block.

type Response

type Response struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      *int64          `json:"id"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *RPCError       `json:"error,omitempty"`
}

Response represents a JSON-RPC 2.0 response.

type SSETransport added in v1.2.0

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

SSETransport implements the legacy MCP HTTP+SSE transport. It opens a persistent GET connection for SSE events and sends requests via POST.

func NewSSETransport added in v1.2.0

func NewSSETransport(ctx context.Context, sseURL string, headers map[string]string) (*SSETransport, error)

NewSSETransport connects to a legacy SSE MCP server. It opens the SSE stream and waits for the "endpoint" event to learn the POST URL.

func (*SSETransport) Close added in v1.2.0

func (t *SSETransport) Close() error

Close closes the SSE stream and cleans up pending requests.

func (*SSETransport) Send added in v1.2.0

func (t *SSETransport) Send(ctx context.Context, req *Request) (*Response, error)

Send posts a JSON-RPC request and waits for the response on the SSE stream.

func (*SSETransport) SendNotification added in v1.2.0

func (t *SSETransport) SendNotification(ctx context.Context, notif *Request) error

SendNotification sends a notification via POST (no response expected).

func (*SSETransport) SetNotificationHandler added in v1.2.0

func (t *SSETransport) SetNotificationHandler(handler NotificationHandler)

SetNotificationHandler sets a callback for server-initiated notifications.

type ServerCapabilities added in v1.2.0

type ServerCapabilities struct {
	Tools   *ToolsCapability `json:"tools,omitempty"`
	Logging *struct{}        `json:"logging,omitempty"`
}

ServerCapabilities describes what the server supports.

type ServerInfo added in v1.2.0

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

ServerInfo identifies the MCP server.

type StdioTransport

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

StdioTransport communicates with an MCP server subprocess via stdin/stdout JSON-RPC.

func NewStdioTransport

func NewStdioTransport(command string, args []string, env []string) (*StdioTransport, error)

NewStdioTransport spawns a subprocess and returns a transport that communicates with it over stdin/stdout using line-delimited JSON-RPC.

func (*StdioTransport) Close

func (t *StdioTransport) Close() error

Close shuts down the transport: closes stdin, waits for the process to exit (with a 5-second timeout), and kills it if necessary.

func (*StdioTransport) Dead

func (t *StdioTransport) Dead() <-chan struct{}

Dead returns a channel that is closed when the subprocess's stdout pipe closes, indicating the MCP server process has died unexpectedly.

func (*StdioTransport) Send

func (t *StdioTransport) Send(ctx context.Context, req *Request) (*Response, error)

Send assigns an ID to the request, sends it, and waits for the response.

func (*StdioTransport) SendNotification

func (t *StdioTransport) SendNotification(ctx context.Context, notif *Request) error

SendNotification sends a JSON-RPC notification (no ID, no response expected).

func (*StdioTransport) SetNotificationHandler added in v1.2.0

func (t *StdioTransport) SetNotificationHandler(handler NotificationHandler)

SetNotificationHandler sets a callback for server-initiated notifications.

type Tool

type Tool struct {
	Name        string      `json:"name"`
	Description string      `json:"description"`
	InputSchema InputSchema `json:"inputSchema"`
}

Tool describes an MCP tool exposed by a server.

type ToolError

type ToolError struct {
	Name    string
	Message string
	Code    int
}

ToolError represents an error reported by an MCP tool (isError=true).

func (*ToolError) Error

func (e *ToolError) Error() string

type ToolsCapability added in v1.2.0

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

ToolsCapability describes tool-related capabilities.

type Transport

type Transport interface {
	// Send sends a request and waits for the corresponding response.
	Send(ctx context.Context, req *Request) (*Response, error)
	// SendNotification sends a notification (no response expected).
	SendNotification(ctx context.Context, notif *Request) error
	// Close shuts down the transport and releases resources.
	Close() error
}

Transport defines the interface for communicating with an MCP server.

Jump to

Keyboard shortcuts

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