mcp

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

ABOUTME: Implements the Tool adapter - wraps MCP tools to implement ABOUTME: the mux Tool interface for seamless integration.

ABOUTME: Defines the Client interface for MCP server communication. ABOUTME: Factory function creates appropriate transport implementation.

ABOUTME: Implements the HTTP transport for MCP using Streamable HTTP. ABOUTME: Supports session management and SSE-based notifications.

ABOUTME: Implements SSE (Server-Sent Events) parsing for Streamable HTTP. ABOUTME: Parses event streams per the SSE specification.

ABOUTME: Implements the stdio transport for MCP - manages JSON-RPC 2.0 communication ABOUTME: with MCP servers over stdin/stdout pipes.

ABOUTME: Defines MCP protocol types - JSON-RPC 2.0 messages, tool info, ABOUTME: and server configuration structures.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionExpired  = errors.New("mcp: session expired")
	ErrNotConnected    = errors.New("mcp: client not connected")
	ErrTransportClosed = errors.New("mcp: transport closed")
)

Sentinel errors for MCP client operations.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Start initializes the connection and performs MCP handshake.
	Start(ctx context.Context) error

	// ListTools retrieves available tools from the server.
	ListTools(ctx context.Context) ([]ToolInfo, error)

	// CallTool executes a tool on the server.
	CallTool(ctx context.Context, name string, args map[string]any) (*ToolCallResult, error)

	// Notifications returns a channel for server-initiated messages.
	// Returns nil for transports that don't support notifications (stdio).
	Notifications() <-chan Notification

	// Close shuts down the connection.
	Close() error
}

Client is the interface for MCP server communication.

func NewClient

func NewClient(config ServerConfig) (Client, error)

NewClient creates an MCP client based on transport config.

type ClientInfo

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

ClientInfo identifies the MCP client.

type ContentBlock

type ContentBlock struct {
	Type     string `json:"type"`
	Text     string `json:"text,omitempty"`
	MimeType string `json:"mimeType,omitempty"`
	Data     string `json:"data,omitempty"`
}

ContentBlock is an MCP content block.

type InitializeParams

type InitializeParams struct {
	ProtocolVersion string     `json:"protocolVersion"`
	Capabilities    any        `json:"capabilities"`
	ClientInfo      ClientInfo `json:"clientInfo"`
}

InitializeParams for MCP handshake.

type Notification

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

Notification is a server-initiated message (no ID field).

type RPCError

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

RPCError represents a JSON-RPC error.

func (*RPCError) Error

func (e *RPCError) Error() string

type Request

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

Request is a JSON-RPC 2.0 request.

func NewRequest

func NewRequest(method string, params any) *Request

NewRequest creates a new request with auto-incrementing ID.

type Response

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

Response is a JSON-RPC 2.0 response.

type ServerConfig

type ServerConfig struct {
	Name      string            `json:"name"`
	Transport string            `json:"transport"`
	Command   string            `json:"command"`
	Args      []string          `json:"args,omitempty"`
	Env       map[string]string `json:"env,omitempty"`

	// HTTP transport fields
	URL     string            `json:"url,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
}

ServerConfig describes an MCP server.

type ToolAdapter

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

ToolAdapter wraps an MCP tool to implement the Tool interface.

func NewToolAdapter

func NewToolAdapter(info ToolInfo, caller ToolCaller) *ToolAdapter

NewToolAdapter creates a new adapter for an MCP tool.

func (*ToolAdapter) Description

func (a *ToolAdapter) Description() string

Description returns the tool description.

func (*ToolAdapter) Execute

func (a *ToolAdapter) Execute(ctx context.Context, params map[string]any) (*tool.Result, error)

Execute calls the MCP tool and converts the result.

Return pattern note: This method returns both a *tool.Result and an error. When CallTool fails (network error, timeout, etc), we return BOTH:

  • An error result object (for consistent tool.Result handling)
  • The actual error (for proper error propagation)

This dual return allows callers to either handle the error directly OR use the Result object uniformly with other tool results.

func (*ToolAdapter) InputSchema

func (a *ToolAdapter) InputSchema() map[string]any

InputSchema returns the JSON schema for tool parameters.

func (*ToolAdapter) Name

func (a *ToolAdapter) Name() string

Name returns the tool name.

func (*ToolAdapter) RequiresApproval

func (a *ToolAdapter) RequiresApproval(params map[string]any) bool

RequiresApproval returns true - MCP tools require approval by default.

type ToolCallParams

type ToolCallParams struct {
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments,omitempty"`
}

ToolCallParams is the input for tools/call.

type ToolCallResult

type ToolCallResult struct {
	Content []ContentBlock `json:"content"`
	IsError bool           `json:"isError,omitempty"`
}

ToolCallResult is the response from tools/call.

type ToolCaller

type ToolCaller interface {
	CallTool(ctx context.Context, name string, args map[string]any) (*ToolCallResult, error)
}

ToolCaller is the interface for calling MCP tools.

type ToolInfo

type ToolInfo struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	InputSchema map[string]any `json:"inputSchema"`
}

ToolInfo describes an MCP tool.

type ToolManager

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

ToolManager manages multiple MCP tool adapters.

func NewToolManager

func NewToolManager(provider ToolProvider) *ToolManager

NewToolManager creates a manager for MCP tools.

func (*ToolManager) Get

func (m *ToolManager) Get(name string) (*ToolAdapter, bool)

Get retrieves a specific tool adapter.

func (*ToolManager) Refresh

func (m *ToolManager) Refresh(ctx context.Context) error

Refresh reloads tools from the MCP server.

func (*ToolManager) RegisterAll

func (m *ToolManager) RegisterAll(registry *tool.Registry)

RegisterAll adds all MCP tools to a tool registry.

func (*ToolManager) Tools

func (m *ToolManager) Tools() []*ToolAdapter

Tools returns all available tool adapters.

type ToolProvider

type ToolProvider interface {
	ToolCaller
	ListTools(ctx context.Context) ([]ToolInfo, error)
}

ToolProvider is the interface for discovering and calling MCP tools.

type ToolsListResult

type ToolsListResult struct {
	Tools []ToolInfo `json:"tools"`
}

ToolsListResult is the response from tools/list.

Jump to

Keyboard shortcuts

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