mcp

package
v0.0.0-...-77aa4db Latest Latest
Warning

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

Go to latest
Published: May 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package mcp adapts external Model Context Protocol servers into the harness's local Tool interface. Stdio (subprocess) and HTTP transports are both supported; the wrapping bridge in register.go is the same.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client wraps an MCP session and the transport it was opened on. One Client corresponds to one connected server; Close terminates it.

func NewHTTPClient

func NewHTTPClient(ctx context.Context, name, endpoint string, headers map[string]string) (*Client, error)

NewHTTPClient connects to a remote MCP server over the Streamable HTTP transport. Headers (auth tokens, API keys) are injected via a RoundTripper so they ride every request the session makes.

func NewStdioClient

func NewStdioClient(ctx context.Context, name, command string, args ...string) (*Client, error)

NewStdioClient launches command/args as a subprocess and connects over its stdin/stdout. The Connect call blocks until the server has responded to the initialize handshake or the context is canceled.

func Register

func Register(ctx context.Context, cfg *Config, registry *tool.Registry, progress ProgressFunc) []*Client

Register connects to every server in cfg and registers their tools into the supplied Registry. Returns the open clients so main can defer Close. Server failures are logged to stderr and skipped — losing one MCP server shouldn't kill the harness. Pass a non-nil progress to receive per-server lifecycle events; pass nil if you don't care.

func (*Client) CallTool

func (c *Client) CallTool(ctx context.Context, name string, arguments any) (string, bool, error)

CallTool dispatches a tool invocation to the remote server. The result's content blocks (the SDK supports text, image, audio, etc.) are concatenated to a single string — our Tool interface returns one string. Anything that isn't text is summarized rather than dropped silently.

func (*Client) Close

func (c *Client) Close() error

Close shuts down the session. For stdio transports this terminates the subprocess; for HTTP it tears down the persistent connection. Safe to call multiple times.

func (*Client) ListTools

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

ListTools returns every tool the server exposes. Called once at startup per server, results cached in our local registry.

type Config

type Config struct {
	Servers []ServerConfig `json:"servers"`
}

Config is the top-level JSON shape.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads a JSON config file. Missing file returns (nil, nil) — MCP is opt-in; absence is not an error.

type MCPTool

type MCPTool struct {
	Client *Client
	Def    api.ToolDef
	// contains filtered or unexported fields
}

MCPTool wraps one remote tool so the agent's local Registry can dispatch to it. The agent loop sees a Tool like any other.

func (*MCPTool) Definition

func (t *MCPTool) Definition() api.ToolDef

func (*MCPTool) Execute

func (t *MCPTool) Execute(ctx context.Context, input string) (string, bool)

type ProgressFunc

type ProgressFunc func(server string, status ProgressStatus, total int)

ProgressFunc reports background progress while Register is iterating the server list. server is the current server's name (empty on Begin/Done); total is the constant count of servers in the config.

type ProgressStatus

type ProgressStatus int

ProgressStatus is one phase of MCP server bring-up. A ProgressFunc gets called once per phase per server, plus ProgressBegin/ProgressDone bracketing the whole batch, so the TUI can render a loading indicator.

const (
	ProgressBegin      ProgressStatus = iota // sent once before any server work
	ProgressConnecting                       // about to dial the named server
	ProgressConnected                        // server connected + tools listed
	ProgressFailed                           // server skipped due to error
	ProgressDone                             // all servers processed
)

type ServerConfig

type ServerConfig struct {
	Name      string            `json:"name"`
	Transport string            `json:"transport"` // "stdio" or "http"
	Command   string            `json:"command,omitempty"`
	Args      []string          `json:"args,omitempty"`
	URL       string            `json:"url,omitempty"`
	Headers   map[string]string `json:"headers,omitempty"`
}

ServerConfig describes one MCP server to connect to. Either Command (with optional Args) for stdio, or URL (with optional Headers) for HTTP. The loader expands ${VAR} in args, header values, and the URL.

Jump to

Keyboard shortcuts

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