upstream

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 13 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 upstream turns a tool call into an HTTP request to the API behind writgate, and the HTTP response back into an MCP tool result (docs/SPEC.md section 3).

A call happens in three steps so the audit log can sit between them: Prepare validates the arguments and builds the request without sending it (the audit "intent" is written from it), Send performs it, and Result maps the response (the audit "outcome" is written alongside).

Index

Constants

View Source
const IdempotencyHeader = "Idempotency-Key"

IdempotencyHeader is the header recovery uses to find a call's result.

View Source
const MaxResponseBytes = 10 << 20

MaxResponseBytes caps how much of an upstream response is read.

View Source
const Redacted = "[redacted]"

Redacted stands in for a sensitive value wherever a request or response is written down.

Variables

This section is empty.

Functions

func Result

func Result(p *Prepared, resp *Response, sendErr error) *mcp.CallToolResult

Result maps an upstream response, or the error from Send, to a tool result.

Types

type Client

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

Client sends tool calls to one upstream API.

func New

func New(baseURL string, opts Options) (*Client, error)

New returns a Client for the API at baseURL, e.g. "https://api.example.com/v1".

func (*Client) Call

func (c *Client) Call(ctx context.Context, t *tools.Tool, rawArgs json.RawMessage) *mcp.CallToolResult

Call is the whole call for a server with no audit log: Prepare, Send, Result.

func (*Client) Prepare

func (c *Client) Prepare(t *tools.Tool, rawArgs json.RawMessage) (*Prepared, *mcp.CallToolResult)

Prepare validates the arguments against the tool's input schema and builds the request. On invalid arguments it returns an error result and nothing is sent.

func (*Client) Send

func (c *Client) Send(ctx context.Context, p *Prepared) (*Response, error)

Send performs a prepared request. An error means no response came back (the API was unreachable, the request timed out or was cancelled), so the call's result is unknown.

type Options

type Options struct {
	// APIKey is sent as "Authorization: Bearer <key>". It comes from
	// writgate's environment and never from the agent.
	APIKey string
	// Timeout bounds each upstream request. Zero means 30 seconds.
	Timeout time.Duration
	// UserAgent is sent with every request.
	UserAgent string
}

Options configures a Client.

type Prepared

type Prepared struct {
	Tool           *tools.Tool
	Args           map[string]any
	Method         string
	Path           string // the resolved path, e.g. "/charges/ch_1/refunds"
	URL            string // full URL, without credentials
	SafeURL        string // URL with the values of sensitive arguments replaced by Redacted
	Header         http.Header
	Body           []byte // nil when there is no body
	IdempotencyKey string // empty when the call has none
}

Prepared is a validated request that has not been sent. It holds no credentials. URL can still hold sensitive argument values; SafeURL is the one to write down.

type Response

type Response struct {
	Status    int
	Header    http.Header
	Body      []byte
	Truncated bool // the body was longer than MaxResponseBytes
	Duration  time.Duration
}

Response is what came back from the upstream API.

Jump to

Keyboard shortcuts

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