mcp

package
v0.36.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package mcp connects ogcode to external Model Context Protocol servers declared in the project's ogcode.json under the "mcp" key. The Manager owns every connection's lifecycle — establishing them in parallel at startup, discovering the tools each server exposes, adapting those tools into ogcode's tool.ToolDef, and tearing down the subprocesses and HTTP sessions on Close.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewTool added in v0.29.0

func NewTool(id, server string, t *mcp.Tool, session *mcp.ClientSession) tool.ToolDef

NewTool is the exported constructor for testing: it wraps a live session and a discovered tool into a ToolDef the same way Manager does internally.

Types

type Manager added in v0.29.0

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

Manager owns all MCP server connections and the tools discovered from them.

func New added in v0.29.0

func New(ctx context.Context, cfg *config.Config) (*Manager, error)

New builds a Manager for the configured MCP servers but does NOT connect to any of them — connection is deferred to Connect. Constructing the Manager is cheap: it only scans the config to decide whether the shared OAuth callback receiver needs to be bound. If cfg is nil or has no MCP servers, New returns a zero-value Manager and no error.

Deferring connection keeps server startup from blocking on MCP servers — historically a slow or OAuth-requiring server could hold startup for up to authTimeout (5 min), during which no HTTP server was listening and the UI could not surface the OAuth prompt. Connect is instead launched after the HTTP server is up (server.go) or before the loop runs (run.go).

func (*Manager) Close added in v0.29.0

func (m *Manager) Close() error

Close terminates every server connection and subprocess exactly once. It is safe to call from multiple goroutines (e.g. a deferred Close racing with a shutdown handler). It also signals any in-flight Connect that it should not register further sessions, so a shutdown racing with a lazy connect cannot leak a freshly-dialled session that Close then misses. Calls to ListTools/CallTool on already-closed sessions return errors from the SDK; ogcode treats any post-Close tool result as a failure.

func (*Manager) Connect added in v0.29.0

func (m *Manager) Connect(ctx context.Context) ([]tool.ToolDef, error)

Connect dials every configured MCP server in parallel, discovers its tools, and adapts them into ogcode tool definitions. It is idempotent: the first call performs the connections; subsequent calls are no-ops. A non-nil error describes per-server failures (a joined error listing each) but never prevents the servers that did connect from exposing their tools — an unreachable or OAuth-rejected server simply contributes no tools.

Tools discovered here are appended to m.tools and also returned so the caller can register them into the host tool.Registry. Callers that registered tools from a previous Connect need not re-register on a later call (there will be none, by the idempotency contract).

func (*Manager) SetServerEnabled added in v0.34.0

func (m *Manager) SetServerEnabled(ctx context.Context, name string, sc config.MCPServerConfig, enabled bool) (added []tool.ToolDef, removed []string, err error)

SetServerEnabled brings one server online or takes it offline at runtime, the path behind the settings toggle. Enabling dials sc now and returns its adapted tools for the caller to register into the host tool.Registry; disabling closes the session, terminates any subprocess, and returns the tool ids to remove, so the tools stop reaching the agent's prompt without a restart. Both are no-ops (empty return) when the server is already in the requested state.

The dial happens outside the lock so it never blocks the agent loop's Tools() for the length of a connect; the lock is taken only to read and mutate the servers map.

func (*Manager) Statuses added in v0.34.0

func (m *Manager) Statuses() []ServerStatus

Statuses returns the runtime state of every server ogcode has connected or tried to. Safe to call concurrently.

func (*Manager) Tools added in v0.29.0

func (m *Manager) Tools() []tool.ToolDef

Tools returns the adapted MCP tool definitions discovered so far, across every connected server. It is empty before Connect runs or when no server exposed any tools. Safe to call concurrently with Connect and SetServerEnabled.

type ServerStatus added in v0.34.0

type ServerStatus struct {
	Name      string
	Connected bool
	ToolCount int
	Err       string
}

ServerStatus is the runtime state of one MCP server, for the settings screen. It covers only servers ogcode has attempted to connect; a disabled server is absent (the caller fills its row from config).

Jump to

Keyboard shortcuts

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