mcp

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package mcp implements the Model Context Protocol (MCP) client. It supports connecting to MCP servers via stdio transport, listing tools and resources, and invoking tool calls. The SSE transport is not yet implemented and fails cleanly at construction time (see NewClient).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterTools

func RegisterTools(ctx context.Context, client *Client, registry *tool.Registry) (int, error)

RegisterTools fetches tools from the MCP server and registers them in the given tool registry. Each MCP tool becomes a tool.Definition whose handler routes calls through the MCP client.

func ToolInfoToDefinitions

func ToolInfoToDefinitions(client *Client, tools []ToolInfo) []*tool.Definition

ToolInfoToDefinitions converts MCP ToolInfo items into tool.Definition items without registering them. Useful for inspection or custom registration.

func ToolInfoToJSON

func ToolInfoToJSON(tools []ToolInfo) ([]byte, error)

ToolInfoToJSON converts tool info into a JSON representation suitable for model tool definitions.

Types

type Client

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

Client communicates with an MCP server using JSON-RPC 2.0 over stdio.

Concurrency model:

  • mu serializes in-flight requests. It is held while writing a request and reading its response. The read honors the per-call context via a read deadline, so it never blocks indefinitely while holding mu.
  • Close does NOT acquire mu. It force-kills the subprocess and clears any read deadline, which unblocks any in-flight read so a hung server can be torn down even while a call is stuck. The closed flag is atomic so it can be observed without the lock.

func NewClient

func NewClient(cfg ServerConfig) (*Client, error)

NewClient creates a new MCP client for the given server configuration. For stdio transport, it launches the command as a subprocess on Connect.

The SSE transport is not yet implemented; NewClient rejects it at construction time with a clear error rather than deferring the failure to call time.

func (*Client) CallTool

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

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

func (*Client) Close

func (c *Client) Close() error

Close shuts down the MCP server connection. It force-kills the subprocess and clears any read deadline so that an in-flight call blocked on a hung server is unblocked promptly. Close does not acquire the request mutex, so it can tear down a client even while a call is stuck in a read. Close is idempotent and safe to call multiple times.

func (*Client) Connect

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

Connect starts the MCP server process and performs the initialize handshake.

func (*Client) Info

func (c *Client) Info() ServerInfo

Info returns the server information from the initialize handshake.

func (*Client) ListResources

func (c *Client) ListResources(ctx context.Context) ([]ResourceInfo, error)

ListResources fetches the available resources from the MCP server.

func (*Client) ListTools

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

ListTools fetches the available tools from the MCP server.

func (*Client) ReadResource

func (c *Client) ReadResource(ctx context.Context, uri string) ([]ResourceContent, error)

ReadResource fetches the content of a resource by URI.

type ResourceContent

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

ResourceContent holds the content returned when reading a resource.

type ResourceInfo

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

ResourceInfo describes a resource provided by an MCP server.

type ServerConfig

type ServerConfig struct {
	Name      string    `json:"name" yaml:"name"`
	Transport Transport `json:"transport" yaml:"transport"`
	Command   string    `json:"command,omitempty" yaml:"command,omitempty"`
	Args      []string  `json:"args,omitempty" yaml:"args,omitempty"`
	URL       string    `json:"url,omitempty" yaml:"url,omitempty"`
}

ServerConfig holds the connection configuration for an MCP server.

type ServerInfo

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

ServerInfo holds the server's initialization response.

type ToolInfo

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

ToolInfo describes a tool provided by an MCP server.

type Transport

type Transport string

Transport defines how the client communicates with an MCP server.

const (
	TransportStdio Transport = "stdio"
	TransportSSE   Transport = "sse"
)

Jump to

Keyboard shortcuts

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