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 ¶
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) Start ¶
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 ¶
Token returns the bearer token clients must pass in the Authorization header. Only valid after New.
func (*Bridge) URL ¶
URL returns the MCP endpoint URL (e.g. http://127.0.0.1:4321/mcp). Only valid after Start succeeds.
type HandlerFunc ¶
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 ¶
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 ¶
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.