bridge

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package bridge runs a loopback HTTP MCP server that exposes user- supplied in-process tools to opencode. opencodesdk.Client adds the bridge to every session/new's mcpServers list when WithSDKTools was configured.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithSessionForTest

func ContextWithSessionForTest(ctx context.Context, s ToolSession) context.Context

ContextWithSessionForTest is a test-only helper that installs a ToolSession into ctx. Exposed so opencodesdk's Elicit test can fake out the session without reaching into unexported symbols. Not intended for production callers.

Types

type Bridge

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

Bridge is a running loopback HTTP MCP server.

func New

func New(tools []ToolDef, logger *slog.Logger, recorder InvocationRecorder) (*Bridge, error)

New constructs a bridge configured with the supplied tools. The bridge is not listening until Start is called. If recorder is non-nil it is invoked once per tool call with (tool, status).

func (*Bridge) Close

func (b *Bridge) Close(ctx context.Context) error

Close stops the HTTP server gracefully. Safe to call multiple times.

func (*Bridge) Start

func (b *Bridge) Start(_ context.Context) error

Start binds the HTTP server to 127.0.0.1:0 and begins serving. Safe to call at most once per Bridge.

func (*Bridge) Token

func (b *Bridge) Token() string

Token returns the bearer token clients must pass in the Authorization header. Only valid after New.

func (*Bridge) URL

func (b *Bridge) URL() string

URL returns the MCP endpoint URL (e.g. http://127.0.0.1:4321/mcp). Only valid after Start succeeds.

type HandlerFunc

type HandlerFunc func(ctx context.Context, input map[string]any) (*ToolOutput, error)

HandlerFunc is the tool-invocation handler the bridge invokes when opencode calls one of the registered tools. It receives the raw arguments as a map and returns a structured ToolOutput or an error.

The ctx passed to the handler carries a ToolSession under sessionContextKey, accessible via SessionFromContext. Tools that want to send an MCP elicitation (server → client prompt) back to opencode should pull the session from ctx and call Elicit.

type InvocationRecorder

type InvocationRecorder interface {
	RecordMCPBridge(ctx context.Context, tool, status string)
}

InvocationRecorder receives a single observation for each tool call routed through the bridge. status is one of "ok", "error", "app_error" (the tool returned IsError=true). Callers can implement this with observability.Observer.RecordMCPBridge.

type ToolDef

type ToolDef struct {
	Name        string
	Description string
	Schema      map[string]any
	Handler     HandlerFunc
	// Annotations, when non-nil, is attached to the underlying MCP
	// tool's Annotations field so opencode sees the hints on
	// tools/list. See opencodesdk.ToolAnnotations for the public
	// wrapper shape.
	Annotations *mcp.ToolAnnotations
}

ToolDef is the bridge-internal tool definition. opencodesdk maps its public Tool interface to this before passing to New.

type ToolOutput

type ToolOutput struct {
	Text       string
	Structured any
	IsError    bool
}

ToolOutput carries the bridge-friendly form of a tool's result. It decouples the bridge from opencodesdk's public Tool API so the bridge package has no cyclic dependencies.

type ToolSession

type ToolSession interface {
	// Elicit sends an MCP elicitation request to the connected
	// client (opencode). Returns the client's response (or an error
	// if the client does not support elicitation).
	Elicit(ctx context.Context, params *mcp.ElicitParams) (*mcp.ElicitResult, error)
}

ToolSession is the minimal surface a bridge-served tool needs for server-initiated interactions (currently: elicitation). It mirrors a subset of *mcp.ServerSession so the public opencodesdk package can expose a stable API without leaking the MCP-SDK types.

func SessionFromContext

func SessionFromContext(ctx context.Context) ToolSession

SessionFromContext returns the ToolSession bound to ctx by the bridge's tool dispatch, or nil when ctx does not carry one.

Jump to

Keyboard shortcuts

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