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 ¶
- type Client
- func (c *Client) CallTool(ctx context.Context, name string, args json.RawMessage) (Result, error)
- func (c *Client) Close() error
- func (c *Client) Connected() bool
- func (c *Client) LastError() error
- func (c *Client) ListResources(ctx context.Context) ([]Resource, error)
- func (c *Client) ListTools(ctx context.Context) ([]Tool, error)
- func (c *Client) Name() string
- func (c *Client) Start(ctx context.Context) error
- func (c *Client) Tools() []Tool
- type ContentItem
- type Manager
- func (m *Manager) Call(ctx context.Context, server, tool string, args json.RawMessage) (Result, error)
- func (m *Manager) Counts() (connected int, failed int)
- func (m *Manager) RescanTools(has func(name string) bool, register func(adapter *ToolAdapter) error) int
- func (m *Manager) SetPolicy(p *mcpshield.Policy)
- func (m *Manager) Start(ctx context.Context) error
- func (m *Manager) Status() []ServerStatus
- func (m *Manager) Stop()
- func (m *Manager) Tools() []ToolWithServer
- type Resource
- type Result
- type ServerStatus
- type Tool
- type ToolAdapter
- type ToolExecutionError
- type ToolWithServer
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 ¶
NewClient prepares a client; call Start to spawn the subprocess and run the JSON-RPC handshake.
func (*Client) Close ¶
Close kills the subprocess and tears down pipes. Waits for background goroutines to drain before returning.
func (*Client) ListResources ¶
ListResources queries the server's resources catalog. Optional capability; returns an empty slice on -32601 (method not found).
type ContentItem ¶
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 ¶
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 ¶
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) Start ¶
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 ¶
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 ¶
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 ¶
ToolWithServer pairs an MCP tool definition with the server name it lives on.
Source Files
¶
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. |