mcp

package
v0.11.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0, MIT Imports: 21 Imported by: 0

Documentation

Overview

Package mcp implements a Go client for the Model Context Protocol. MCP servers are subprocesses that speak JSON-RPC 2.0 over stdio.

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 is a single MCP server connection over stdio.

func NewClient

func NewClient(name, command string, args []string, env map[string]string) *Client

NewClient prepares a client; call Start to spawn the subprocess and run the JSON-RPC handshake.

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, args json.RawMessage) (Result, error)

CallTool invokes a tool by name with the given JSON-encoded args.

func (*Client) Close

func (c *Client) Close() error

Close kills the subprocess and tears down pipes. Waits for background goroutines to drain before returning.

func (*Client) Connected

func (c *Client) Connected() bool

Connected reports the live status.

func (*Client) LastError

func (c *Client) LastError() error

LastError surfaces the last connection error, if any.

func (*Client) ListResources

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

ListResources queries the server's resources catalog. Optional capability; returns an empty slice on -32601 (method not found).

func (*Client) ListTools

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

ListTools queries the server for its tool catalog.

func (*Client) Name

func (c *Client) Name() string

Name returns the configured server name.

func (*Client) Start

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

Start launches the subprocess, runs the MCP handshake, and caches the tool list. Returns once the server has been initialized.

func (*Client) Tools

func (c *Client) Tools() []Tool

Tools returns the cached tool list (populated at Start).

type ContentItem

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

ContentItem matches the MCP `content` block (text/image/resource).

type Manager

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

Manager owns a pool of MCP clients keyed by server name.

func NewManager

func NewManager(cfg config.MCPConfig) *Manager

NewManager builds a Manager from configuration. Servers aren't started until Start is called.

func (*Manager) Call

func (m *Manager) Call(ctx context.Context, server, tool string, args json.RawMessage) (Result, error)

Call routes a tool call to the right server client.

func (*Manager) Counts

func (m *Manager) Counts() (connected int, failed int)

Counts returns (connected, failed). Failed = configured but unable to connect at this instant.

func (*Manager) RescanTools

func (m *Manager) RescanTools(has func(name string) bool, register func(adapter *ToolAdapter) error) int

RescanTools walks every connected server's tool list and registers any tools not already in the registry, returning the count added. The caller supplies a `has` predicate (registry name lookup) and a `register` callback that registers a freshly-built adapter — this keeps the mcp package free of any tools package import.

The adapter naming convention is `mcp:<server>:<tool>`, matching NewToolAdapter().

func (*Manager) SetPolicy

func (m *Manager) SetPolicy(p *mcpshield.Policy)

SetPolicy attaches an mcpshield policy. Pass nil to disable.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context) error

Start spawns each configured server in its own goroutine with retry backoff. Returns immediately — readiness can be observed via Status().

func (*Manager) Status

func (m *Manager) Status() []ServerStatus

Status returns a snapshot of all clients (including failed ones).

func (*Manager) Stop

func (m *Manager) Stop()

Stop tears down all running clients. Blocks until every supervisor goroutine has observed the stop signal and exited, so callers can safely free the Manager + its clients after Stop returns. The old fire-and-forget close-stop-then-return left supervisor goroutines alive (sleeping in backoff) that could still write to m.clients after the caller had moved on.

func (*Manager) Tools

func (m *Manager) Tools() []ToolWithServer

Tools returns a flat snapshot of every connected server's tools.

type Resource

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

Resource is an MCP resource (e.g. a file or document).

type Result

type Result struct {
	Content []ContentItem `json:"content"`
	IsError bool          `json:"isError,omitempty"`
	Text    string        `json:"-"`
}

Result wraps a tool/call response. The MCP spec returns a list of content items; we keep the raw payload alongside a flattened text representation for callers that just want the output as a string.

type ServerStatus

type ServerStatus struct {
	Name       string
	Connected  bool
	ToolsCount int
	LastError  string
}

ServerStatus is a snapshot of one MCP client for the TUI status panel.

type Tool

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

Tool describes an MCP tool exposed by a server.

type ToolAdapter

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

ToolAdapter implements internal/tools.Tool by routing calls into a Manager. The wrapper name is `mcp:<server>:<tool>` so multiple servers exposing identically-named tools don't collide in the registry.

func NewToolAdapter

func NewToolAdapter(m *Manager, server, tool string) *ToolAdapter

NewToolAdapter wraps one MCP tool for registration in the agent's tool registry.

func (*ToolAdapter) Available

func (a *ToolAdapter) Available() bool

Available hides stale or denied catalog entries without replacing native tools.

func (*ToolAdapter) Description

func (a *ToolAdapter) Description() string

func (*ToolAdapter) Execute

func (a *ToolAdapter) Execute(ctx context.Context, input json.RawMessage) (json.RawMessage, error)

func (*ToolAdapter) Name

func (a *ToolAdapter) Name() string

func (*ToolAdapter) Schema

func (a *ToolAdapter) Schema() json.RawMessage

type ToolExecutionError

type ToolExecutionError struct {
	Server string
	Tool   string
	Result Result
}

ToolExecutionError is an MCP tools/call result marked isError. The original content is retained for diagnostics while Go callers receive an actual failure.

func (*ToolExecutionError) Error

func (err *ToolExecutionError) Error() string

type ToolWithServer

type ToolWithServer struct {
	Server string
	Tool   Tool
}

ToolWithServer pairs an MCP tool definition with the server name it lives on.

Directories

Path Synopsis
Package testfixture supplies a controlled local MCP subprocess for acceptance tests.
Package testfixture supplies a controlled local MCP subprocess for acceptance tests.

Jump to

Keyboard shortcuts

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