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 ¶
- func NewTool(id, server string, t *mcp.Tool, session *mcp.ClientSession) tool.ToolDef
- type Manager
- func (m *Manager) Close() error
- func (m *Manager) Connect(ctx context.Context) ([]tool.ToolDef, error)
- func (m *Manager) SetServerEnabled(ctx context.Context, name string, sc config.MCPServerConfig, enabled bool) (added []tool.ToolDef, removed []string, err error)
- func (m *Manager) Statuses() []ServerStatus
- func (m *Manager) Tools() []tool.ToolDef
- type ServerStatus
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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
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
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
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.
type ServerStatus ¶ added in v0.34.0
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).