mcp

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package mcp wires user-configured Model Context Protocol servers into the agent loop.

At startup the host calls Build, which reads .agents/mcp.json, spawns each declared server (stdio child or Streamable HTTP client), wraps the resulting MCP toolsets via ADK's google.golang.org/adk/tool/mcptoolset, and returns:

  • the toolsets, so they can be passed to agent.New(WithToolsets…)
  • per-server records the host can render (e.g. a /mcp slash command).

Failures are non-fatal: a server whose process won't start surfaces in the per-server record with its error; the agent continues with whichever servers did connect.

Index

Constants

View Source
const (
	StatusOK    = "ok"
	StatusError = "error"
)

Status values surfaced via the per-server records.

View Source
const MCPFileName = "mcp.json"

MCPFileName is the project-local MCP config file inside .agents/.

Variables

This section is empty.

Functions

func DeclineHandler

func DeclineHandler(serverName string, send func(string)) func(context.Context, *mcp.ElicitRequest) (*mcp.ElicitResult, error)

DeclineHandler returns an MCP elicitation handler that declines every request and emits a one-line notice through send. Used as the fallback when no interactive elicitor is wired.

func InterpolateEnv

func InterpolateEnv(s string) string

InterpolateEnv replaces ${env:NAME} placeholders in s by looking each NAME up via os.Getenv. Unset values pass through as empty strings — same semantics shells use.

func InterpolateMap

func InterpolateMap(m map[string]string) map[string]string

InterpolateMap returns a copy of m with each value run through InterpolateEnv. Used for ServerSpec.Env and ServerSpec.Headers.

func SetImplementationName

func SetImplementationName(name string)

SetImplementationName overrides the name reported during the MCP client handshake. Useful for hosts that want to identify themselves to the server. Call before Build.

Types

type ElicitorFn

type ElicitorFn func(ctx context.Context, serverName string, req *mcp.ElicitRequest) (*mcp.ElicitResult, error)

ElicitorFn is the host-supplied bridge that turns a server's elicitation request into a user response. Interactive hosts plug in a function that opens a prompt and blocks; the headless path leaves it nil and falls back to DeclineHandler.

The implementation must respect ctx — if it returns ctx.Err the SDK translates that into a protocol-level cancel.

type Server

type Server struct {
	Name   string
	Status string
	Tools  []string // tool names exposed; populated lazily by Toolset
	Err    error    // non-nil when Status == StatusError
	// contains filtered or unexported fields
}

Server is one configured MCP server's runtime state.

func Build

func Build(ctx context.Context, agentsDir string, send func(string), gate *permissions.Gate, elicitor ElicitorFn) ([]*Server, []tool.Toolset, error)

Build reads .agents/mcp.json and starts every declared server in parallel. The send callback is plumbed into each server's elicitation handler (when no interactive elicitor is provided) so the host can surface elicitation requests in the right place.

gate (optional) gates each MCP tool call through the permission system so MCP tools are subject to the same ask/allow/yolo rules as built-in tools. Pass nil to skip gating.

elicitor (optional) is the interactive bridge for elicitation requests. Headless callers leave it nil and fall back to the decline-with-notice stub.

Servers that fail to start come back with Status==StatusError so they're visible without breaking the rest of the agent.

func (*Server) Close

func (s *Server) Close()

Close terminates any child process this server owns. For HTTP transports there's no process to kill — Close is a no-op.

Termination strategy: SIGTERM, give the process up to 3 seconds to exit gracefully, then SIGKILL.

func (*Server) Toolset

func (s *Server) Toolset() tool.Toolset

Toolset returns the MCP toolset, or nil for failed servers.

type ServerSpec

type ServerSpec struct {
	Transport string            `json:"transport"`         // "stdio" | "http"
	Command   string            `json:"command,omitempty"` // stdio
	Args      []string          `json:"args,omitempty"`    // stdio
	Env       map[string]string `json:"env,omitempty"`     // stdio
	URL       string            `json:"url,omitempty"`     // http
	Headers   map[string]string `json:"headers,omitempty"` // http
}

ServerSpec describes one MCP server. Either Command (stdio) or URL (Streamable HTTP) must be set; we intentionally don't support both.

func (ServerSpec) Validate

func (s ServerSpec) Validate(name string) error

Validate checks that the spec describes a single, complete transport.

type Servers

type Servers struct {
	Version int                   `json:"version"`
	Servers map[string]ServerSpec `json:"servers"`
}

Servers is the on-disk schema for .agents/mcp.json.

func Load

func Load(agentsDir string) (Servers, error)

Load reads <agentsDir>/mcp.json. A missing file is treated as "no servers configured" — not an error, since most projects never declare MCP servers.

Jump to

Keyboard shortcuts

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