mcp

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const ProtocolVersion = "2025-03-26"

ProtocolVersion is the MCP protocol version supported by this implementation.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an MCP Streamable HTTP client.

func NewClient

func NewClient(upstream string, opts ...ClientOption) *Client

NewClient creates a new MCP client targeting upstream with the given options.

func (*Client) CallTool

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

CallTool invokes a named tool on the MCP server with the given arguments.

func (*Client) Initialize

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

Initialize sends an initialize request to the MCP server.

func (*Client) ListTools

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

ListTools retrieves the list of tools from the MCP server.

type ClientInfo

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

ClientInfo identifies an MCP client.

type ClientOption

type ClientOption func(*Client)

ClientOption configures a Client.

func WithBearerToken

func WithBearerToken(envVar string) ClientOption

WithBearerToken returns a ClientOption that reads a bearer token from the given environment variable at call time and adds it as an Authorization header.

Note: Only the last auth option takes effect. WithBearerToken and WithHeader both set the auth function; later calls override earlier ones.

func WithHeader

func WithHeader(headerName, envVar string) ClientOption

WithHeader returns a ClientOption that reads a value from the given environment variable at call time and sets it as a custom request header.

Note: Only the last auth option takes effect. WithHeader and WithBearerToken both set the auth function; later calls override earlier ones.

type ContentBlock

type ContentBlock struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

ContentBlock is a content block in a tool call result.

type Handler

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

Handler processes MCP tool calls.

type InitializeParams

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

InitializeParams is the parameters for an MCP initialize request.

type InitializeResult

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

InitializeResult is the result of an MCP initialize request.

type JSONRPCError

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

JSONRPCError is a JSON-RPC 2.0 error object.

type JSONRPCRequest

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

JSONRPCRequest is a JSON-RPC 2.0 request message.

type JSONRPCResponse

type JSONRPCResponse struct {
	JSONRPC string        `json:"jsonrpc"`
	ID      any           `json:"id"`
	Result  any           `json:"result,omitempty"`
	Error   *JSONRPCError `json:"error,omitempty"`
}

JSONRPCResponse is a JSON-RPC 2.0 response message.

type ListToolsResult

type ListToolsResult struct {
	Tools []Tool `json:"tools"`
}

ListToolsResult is the result of a tools/list request.

type Server

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

Server is an MCP Streamable HTTP server.

func NewServer

func NewServer(tools []Tool, handler Handler) *Server

NewServer creates an MCP server with the given tools and handler.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler.

type ServerCapabilities

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

ServerCapabilities describes the capabilities of an MCP server.

type ServerInfo

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

ServerInfo identifies an MCP server.

type StdioClient

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

StdioClient is an MCP client that communicates with a subprocess over stdio using newline-delimited JSON-RPC.

func NewStdioClient

func NewStdioClient(name string, args ...string) (*StdioClient, error)

NewStdioClient creates a new StdioClient that spawns the given command. The command is started immediately. Call Close to shut it down.

func (*StdioClient) CallTool

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

CallTool invokes a named tool on the MCP server with the given arguments.

func (*StdioClient) Close

func (c *StdioClient) Close() error

Close shuts down the subprocess by closing stdin and killing the process.

func (*StdioClient) Initialize

func (c *StdioClient) Initialize(ctx context.Context) (*InitializeResult, error)

Initialize sends an initialize request to the MCP server, followed by an initialized notification.

func (*StdioClient) ListTools

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

ListTools retrieves the list of tools from the MCP server.

type Tool

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

Tool describes an MCP tool.

type ToolCallParams

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

ToolCallParams is the parameters for a tools/call request.

type ToolCallResult

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

ToolCallResult is the result of a tools/call request.

type ToolCaller

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

ToolCaller is the interface for MCP clients that can initialize, discover tools, and call tools. Both HTTP and stdio clients implement this.

type ToolsCapability

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

ToolsCapability indicates the server supports tool operations.

Jump to

Keyboard shortcuts

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