proxy

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: GPL-3.0 Imports: 28 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package proxy is the MCP passthrough: it terminates the connection from the host (downstream), keeps connections to the real MCP servers (upstream), and puts the policy evaluator and the audit log in between.

Everything that is not a tools/call is forwarded unchanged. tools/call is the only place agentgate has an opinion.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoSuchTool = errors.New("no such tool")

ErrNoSuchTool is returned by Trust for a tool the last refresh did not see.

View Source
var Version = "dev"

Version is stamped into the implementation agentgate advertises. It is overridden from main at build time.

Functions

func AnnotationsFromCatalog added in v0.4.0

func AnnotationsFromCatalog(raw []byte) (map[string]policy.Annotations, error)

AnnotationsFromCatalog reads a catalog snapshot from the audit log and returns each tool's annotations the way a live call would have seen them, keyed by exposed name. Replay uses it to evaluate annotations.* conditions.

Types

type ApprovalRequest

type ApprovalRequest struct {
	SessionID string          `json:"session_id"`
	Upstream  string          `json:"upstream"`
	Tool      string          `json:"tool"`
	ToolName  string          `json:"tool_name"`
	Args      json.RawMessage `json:"args,omitempty"`
	Decision  policy.Decision `json:"decision"`
	Timeout   time.Duration   `json:"-"`
}

ApprovalRequest describes a call that a rule marked "ask".

type Approver

type Approver interface {
	Approve(ctx context.Context, req ApprovalRequest) (Verdict, error)
}

Approver turns an "ask" into an allow or a deny. An error means the question could not be put to anyone, and the call is denied with that reason.

type Choice added in v0.3.0

type Choice string

Choice is what a person clicked.

const (
	// ChoiceAllow lets this one call through.
	ChoiceAllow Choice = "allow"
	// ChoiceAllowSession lets this call and every later call of the same tool
	// in the same session through.
	ChoiceAllowSession Choice = "allow-session"
	// ChoiceDeny rejects the call.
	ChoiceDeny Choice = "deny"
)

type DenyApprover

type DenyApprover struct {
	// Reason overrides the default explanation.
	Reason string
}

DenyApprover is the fallback when no one can be asked.

func (DenyApprover) Approve

Approve always denies.

type Event

type Event struct {
	Event     string          `json:"event"`
	At        time.Time       `json:"at"`
	SessionID string          `json:"session_id,omitempty"`
	Host      string          `json:"host,omitempty"`
	Upstream  string          `json:"upstream,omitempty"`
	Tool      string          `json:"tool,omitempty"`
	Decision  policy.Decision `json:"decision"`
	Args      json.RawMessage `json:"args,omitempty"`
	Shadow    bool            `json:"shadow,omitempty"`
	Message   string          `json:"message"`
}

Event is what a webhook receives. It is also what the log line says, so a Slack message and a grep of the log tell the same story.

type FanoutApprover added in v0.4.0

type FanoutApprover []Approver

FanoutApprover puts the question to every channel at once — the web UI, the terminal, the phone — and takes the first answer; the other channels are withdrawn. A channel that cannot ask at all (an error) is skipped.

func (FanoutApprover) Approve added in v0.4.0

func (f FanoutApprover) Approve(ctx context.Context, req ApprovalRequest) (Verdict, error)

Approve asks everyone and returns the first answer.

type Inbox

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

Inbox is the queue of pending approvals the web UI shows. It is also an Approver, so "ask" in a run with --ui waits for a click instead of a keystroke.

func NewInbox

func NewInbox() *Inbox

NewInbox returns an empty approval inbox.

func (*Inbox) Approve

func (i *Inbox) Approve(ctx context.Context, req ApprovalRequest) (Verdict, error)

Approve parks the call until someone answers in the UI or ctx expires.

func (*Inbox) Pending

func (i *Inbox) Pending() []PendingApproval

Pending lists the waiting approvals, oldest first.

func (*Inbox) Resolve

func (i *Inbox) Resolve(id string, choice Choice, who string) bool

Resolve answers a pending approval. It reports false when the id is unknown, which happens when the call already timed out.

type NtfyApprover added in v0.4.0

type NtfyApprover struct {
	// Server is the ntfy base URL, "https://ntfy.sh" or your own.
	Server string
	// Topic is where questions are published. On a public server the topic
	// name is the password: make it long and random.
	Topic string
	// Token is an access token for servers with access control.
	Token string
	// Redact scrubs the arguments before they leave the machine.
	Redact func([]byte) []byte
	Client *http.Client
	Log    *slog.Logger
	// contains filtered or unexported fields
}

NtfyApprover puts a question on your phone. It publishes the pending call to an ntfy topic with Allow / Allow for session / Deny buttons; a button posts its answer to a second topic, <topic>-answers, which the approver reads over a long-lived subscription. Nothing listens on a port, so it works from a laptop behind any NAT, and the answer carries a one-time nonce, so it cannot be replayed or guessed.

func (*NtfyApprover) Approve added in v0.4.0

func (n *NtfyApprover) Approve(ctx context.Context, req ApprovalRequest) (Verdict, error)

Approve publishes the question and waits for a button, or ctx.

func (*NtfyApprover) Run added in v0.4.0

func (n *NtfyApprover) Run(ctx context.Context)

Run keeps the subscription to the answers topic open until ctx ends, reconnecting with backoff when the connection drops.

type Options

type Options struct {
	Config *config.Config
	Store  *audit.Store
	// Redactor scrubs arguments and results that leave the process through a
	// webhook, and results that reach the agent when redact_results is on.
	Redactor *audit.Redactor
	Logger   *slog.Logger
	// Approver decides "ask" calls. When nil, ask falls back to deny.
	Approver Approver
	// DownstreamTransport is recorded with the session ("stdio" or "http").
	DownstreamTransport string
	// Pinning says what the proxy may do with the lockfile. The zero value
	// follows the config.
	Pinning PinningMode
	// Telemetry receives a span for every call. Nil exports nothing.
	Telemetry *telemetry.Exporter
}

Options configure a Proxy.

type PendingApproval

type PendingApproval struct {
	ID        string
	Request   ApprovalRequest
	CreatedAt time.Time
	Expires   time.Time
}

PendingApproval is the read-only view the UI renders.

type PinningMode added in v0.4.0

type PinningMode int

PinningMode says what the proxy may do with the lockfile.

const (
	// PinningLive follows the config: tools are pinned on first sight,
	// drift is recorded and reported, and tools are held back as configured.
	PinningLive PinningMode = iota
	// PinningInspect compares and reports but never writes the lockfile and
	// never holds a tool back. `agentgate lock` and replay use it.
	PinningInspect
)

type Proxy

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

Proxy fronts one or more upstream MCP servers.

func New

func New(opts Options) (*Proxy, error)

New builds a proxy. Call Connect before serving.

func (*Proxy) Close

func (p *Proxy) Close() error

Close ends every downstream session and closes every upstream.

func (*Proxy) Config

func (p *Proxy) Config() *config.Config

Config returns the configuration currently in force.

func (*Proxy) Connect

func (p *Proxy) Connect(ctx context.Context) error

Connect dials every upstream and builds the merged catalog. Upstreams that fail to connect are reported but do not stop the others: a broken server should not take the whole gateway down.

func (*Proxy) Forward

func (p *Proxy) Forward(ctx context.Context, exposed string, args json.RawMessage) (*mcp.CallToolResult, error)

Forward sends a tools/call to the upstream that owns the tool, without consulting the policy. It exists for "agentgate replay", which evaluates the policy itself so that it can report what changed.

func (*Proxy) ForwardJSON

func (p *Proxy) ForwardJSON(ctx context.Context, exposed string, args json.RawMessage) (json.RawMessage, error)

ForwardJSON is Forward with the result marshalled, which is the shape the replay package needs.

func (*Proxy) Freeze

func (p *Proxy) Freeze(reason, by string) error

Freeze throws the kill switch: every call, in every session, on every gateway that shares this config, is denied until Unfreeze.

func (*Proxy) Frozen

func (p *Proxy) Frozen() bool

Frozen reports whether the kill switch is thrown.

func (*Proxy) HTTPHandler

func (p *Proxy) HTTPHandler() http.Handler

HTTPHandler serves the Streamable HTTP transport for downstream hosts. Sessions idle for half a day are closed: a host that went away without saying so, and the throwaway session a server/discover runs on, would otherwise stay open for as long as the process runs.

func (*Proxy) Lookup

func (p *Proxy) Lookup(exposed string) (ToolBinding, bool)

Lookup resolves an exposed tool name to its binding.

func (*Proxy) Refresh

func (p *Proxy) Refresh(ctx context.Context) error

Refresh rebuilds the merged catalog from every reachable upstream and registers it on the downstream server. Adding and removing features on the server is what makes the SDK emit the matching list_changed notifications.

func (*Proxy) RunStdio

func (p *Proxy) RunStdio(ctx context.Context) error

RunStdio serves a single downstream connection on stdin/stdout and returns when the host disconnects or ctx is cancelled.

func (*Proxy) Server

func (p *Proxy) Server() *mcp.Server

Server exposes the downstream MCP server, for tests and for the HTTP handler.

func (*Proxy) SetPolicy

func (p *Proxy) SetPolicy(pol *policy.Policy)

SetPolicy swaps the policy in place, for hot reload from the web UI or a SIGHUP. Upstream connections are untouched.

func (*Proxy) ToolReports added in v0.4.0

func (p *Proxy) ToolReports() []ToolReport

ToolReports returns every tool's standing with the lockfile and the scan, as of the last refresh.

func (*Proxy) Tools

func (p *Proxy) Tools() []ToolBinding

Tools returns the current catalog, sorted by exposed name.

func (*Proxy) Trust added in v0.4.0

func (p *Proxy) Trust(ctx context.Context, tool string) ([]ToolReport, error)

Trust pins a tool as it is offered now, accepting whatever the scan found in it, writes the lockfile and refreshes the catalog so a tool that was held back reaches the host. tool is "upstream.tool" or the exposed name; "*" trusts everything that is not already pinned and clean.

func (*Proxy) Unfreeze

func (p *Proxy) Unfreeze() error

Unfreeze lifts the kill switch.

type TTYApprover

type TTYApprover struct {
	In  io.Reader
	Out io.Writer
	// contains filtered or unexported fields
}

TTYApprover asks on agentgate's own terminal. In stdio mode stdin and stdout belong to the MCP conversation, so this only works when agentgate is run with --http, or when a separate terminal is wired up.

func NewTTYApprover

func NewTTYApprover() *TTYApprover

NewTTYApprover returns an approver bound to the controlling terminal, or nil when there is none. In stdio mode /dev/tty is used so that the prompt does not land in the MCP stream.

func (*TTYApprover) Approve

func (t *TTYApprover) Approve(ctx context.Context, req ApprovalRequest) (Verdict, error)

Approve prints the call and waits for y, n, or a (allow for the session).

type ToolBinding

type ToolBinding struct {
	// Exposed is the name the host sees, prefix included.
	Exposed string
	// Name is the name the upstream server uses.
	Name string
	// Upstream is the configured upstream name.
	Upstream string
	// Annotations are the server's hints about the tool, for annotations.*
	// conditions.
	Annotations policy.Annotations
}

ToolBinding maps an exposed tool name back to its upstream.

type ToolReport added in v0.4.0

type ToolReport struct {
	pinning.Report
	// Exposed is the name the host sees; empty for a tool that was removed.
	Exposed string `json:"exposed,omitempty"`
	// PinnedNow is set when the tool was pinned on this sight, on first use.
	PinnedNow bool `json:"pinned_now,omitempty"`
	// Quarantined is set when the tool is hidden from the host. Reason says
	// why it is, or why it would be when the proxy only inspects.
	Quarantined bool   `json:"quarantined,omitempty"`
	Reason      string `json:"reason,omitempty"`
}

ToolReport is a tool's standing with the lockfile and the definition scan.

type Verdict added in v0.3.0

type Verdict struct {
	policy.Decision
	Session bool
}

Verdict is an approver's answer. Session is set when the human chose to allow the tool for the rest of the session, so that the same question is not asked again a minute later.

Jump to

Keyboard shortcuts

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