mcp

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package mcp implements the MCP client with stdio and SSE transports.

Index

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 an MCP server connection.

func NewClient

func NewClient(transport Transport) *Client

NewClient creates a new MCP client with the given transport.

func (*Client) CallTool

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

CallTool calls the MCP tools/call method with the given name and arguments.

func (*Client) Close

func (c *Client) Close() error

Close closes the transport connection.

func (*Client) Connect

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

Connect establishes the transport connection.

func (*Client) ListTools

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

ListTools calls the MCP tools/list method and returns discovered tools.

type JSONRPCError

type JSONRPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

JSONRPCError represents a JSON-RPC error.

type JSONRPCRequest

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

JSONRPCRequest is a JSON-RPC 2.0 request.

type JSONRPCResponse

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

JSONRPCResponse is a JSON-RPC 2.0 response.

type ListToolsResult

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

ListToolsResult is the result of a tools/list call.

type MCPServer added in v0.3.0

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

MCPServer is the MCP server that exposes tools via SSE + JSON-RPC 2.0.

func NewMCPServer added in v0.3.0

func NewMCPServer(cfg ...ServerConfig) *MCPServer

NewMCPServer creates a new MCPServer with optional configuration.

func (*MCPServer) HandleJSONRPC added in v0.6.0

func (s *MCPServer) HandleJSONRPC(ctx context.Context, raw json.RawMessage) ([]byte, error)

HandleJSONRPC parses a JSON-RPC 2.0 request from raw JSON and returns the serialized response. Supported methods: initialize, tools/list, tools/call, resources/list, prompts/get.

func (*MCPServer) ListTools added in v0.6.0

func (s *MCPServer) ListTools() []ToolDescriptor

ListTools returns a copy of all registered tool descriptors.

func (*MCPServer) RegisterResource added in v0.3.0

func (s *MCPServer) RegisterResource(res Resource)

RegisterResource registers a static resource.

func (*MCPServer) RegisterTool added in v0.3.0

func (s *MCPServer) RegisterTool(name, description string, inputSchema any, handler ToolHandler)

RegisterTool registers an MCP tool with its handler.

func (*MCPServer) Serve added in v0.3.0

func (s *MCPServer) Serve(listener net.Listener) error

Serve starts the MCP server on the given listener.

func (*MCPServer) Stop added in v0.3.0

func (s *MCPServer) Stop(ctx context.Context) error

Stop gracefully shuts down the MCP server.

type Resource added in v0.3.0

type Resource struct {
	URI         string `json:"uri"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	MIMEType    string `json:"mimeType,omitempty"`
}

Resource holds MCP resource metadata.

type SSETransport

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

SSETransport connects to an MCP server via HTTP SSE.

func NewSSETransport

func NewSSETransport(url string) *SSETransport

NewSSETransport creates a new SSE transport for the given URL.

func (*SSETransport) Close

func (t *SSETransport) Close() error

Close shuts down the SSE transport and releases resources.

func (*SSETransport) Connect

func (t *SSETransport) Connect(ctx context.Context) error

Connect establishes the HTTP SSE connection to the MCP server.

func (*SSETransport) Receive

func (t *SSETransport) Receive() ([]byte, error)

Receive reads the next event from the SSE stream.

func (*SSETransport) Send

func (t *SSETransport) Send(msg []byte) error

Send posts a message to the MCP server via HTTP POST.

type ServerConfig added in v0.3.0

type ServerConfig struct {
	HeartbeatInterval time.Duration
}

ServerConfig holds optional configuration for the MCPServer.

type StdioTransport

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

StdioTransport connects to an MCP server via subprocess stdin/stdout.

func NewStdioTransport

func NewStdioTransport(command string, args ...string) *StdioTransport

NewStdioTransport creates a new stdio transport for the given command.

func (*StdioTransport) Close

func (t *StdioTransport) Close() error

Close stops the subprocess and cleans up resources.

func (*StdioTransport) Connect

func (t *StdioTransport) Connect(ctx context.Context) error

Connect starts the subprocess and prepares stdin/stdout for communication.

func (*StdioTransport) Receive

func (t *StdioTransport) Receive() ([]byte, error)

Receive reads the next message from the subprocess stdout.

func (*StdioTransport) Send

func (t *StdioTransport) Send(msg []byte) error

Send writes a message to the subprocess stdin.

type ToolAdapter

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

ToolAdapter adapts an MCP ToolDescriptor to the tools.Tool interface.

func NewToolAdapter

func NewToolAdapter(desc ToolDescriptor, client *Client) *ToolAdapter

NewToolAdapter creates a new ToolAdapter.

func (*ToolAdapter) Description

func (a *ToolAdapter) Description() string

Description implements the Tool interface.

func (*ToolAdapter) Execute

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

Execute implements the Tool interface.

func (*ToolAdapter) Name

func (a *ToolAdapter) Name() string

Name implements the Tool interface.

func (*ToolAdapter) Parameters

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

Parameters implements the Tool interface.

type ToolCallResult

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

ToolCallResult is the result of a tools/call.

type ToolContent

type ToolContent struct {
	Type string `json:"type"`
	Text string `json:"text"`
}

ToolContent represents content returned by an MCP tool.

type ToolDescriptor

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

ToolDescriptor represents metadata about an MCP tool from tools/list.

type ToolHandler added in v0.3.0

type ToolHandler func(ctx context.Context, args map[string]any) (string, error)

ToolHandler is a function that handles an MCP tool call.

type Transport

type Transport interface {
	Connect(ctx context.Context) error
	Send(msg []byte) error
	Receive() ([]byte, error)
	Close() error
}

Transport defines the interface for MCP transport connections.

Jump to

Keyboard shortcuts

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