mcp

package
v0.0.0-...-9fe12d6 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Daemon lifecycle: a per-project unix socket serving MCP over each accepted connection, with an idle reaper and auto-shutdown. Contract in docs/spec/code-intel-surfaces.md.

Client side of `atomic code mcp`: connect-or-start the singleton daemon under an flock, then pipe stdio to its socket. Daemon mode is the same `mcp` verb with --daemon, an unadvertised flag existing only for the auto-start path.

Package mcp serves the code-intelligence index as MCP tools.

Small repos get only explore/search/node; graph-traversal tools are withheld below tinyRepoThreshold, where a whole-repo read is cheaper than a traversal.

Index

Constants

View Source
const (
	ConnIdleTTL   = 30 * time.Minute
	ServerIdleTTL = 30 * time.Minute
	ReapTick      = 60 * time.Second
	// SyncInterval re-syncs the served graph against the working tree; 0 disables.
	SyncInterval = 10 * time.Second
)

Exported so tests can assert the literal values.

Variables

This section is empty.

Functions

func ApplyCeiling

func ApplyCeiling(output string, ceiling int) string

ApplyCeiling truncates at the last section boundary in the back half, so the output ends on a whole section rather than mid-sentence. Exported for tests.

func DaemonArgv

func DaemonArgv(sourceRoot, dbPath string, opts WatchOptions) []string

DaemonArgv spawns through the registered `code mcp` verb rather than a separate internal one, so the spawned command can never drift out of the Cobra tree. Exported so tests can assert the exact argv.

func DefaultSpawn

func DefaultSpawn(sourceRoot, dbPath string, opts WatchOptions) error

DefaultSpawn starts the daemon detached with no stdio, so the parent can exit immediately. Explicit paths keep the daemon from re-resolving scope from cwd.

func EnsureRunning

func EnsureRunning(ctx context.Context, sourceRoot, dbPath string, spawn SpawnFunc) error

EnsureRunning starts the daemon if it is not already up, serialising concurrent starters behind an flock so a burst of clients spawns one daemon rather than a herd. Exported so tests can drive the auto-start path.

func GetExploreBudget

func GetExploreBudget(fileCount int) int

GetExploreBudget returns how many explore calls a repo of this size allows.

func IsLive

func IsLive(socketPath string) bool

IsLive dials the socket; a leftover socket file with no daemon behind it gives ECONNREFUSED and so reads as not live.

func LockPath deprecated

func LockPath(projectRoot string) string

LockPath assumes the db sits at the canonical position under projectRoot.

Deprecated: use LockPathFromDB, which works for realm members too.

func LockPathFromDB

func LockPathFromDB(dbPath string) string

LockPathFromDB mirrors SocketPathFromDB with a .mcp.lock extension.

func NewRegistry

func NewRegistry(now func() time.Time) *registry

func NewServer

func NewServer(eng *engine.Engine, fileCount int) *sdk.Server

NewServer builds a transport-agnostic server whose registered tool set depends on fileCount. Call RunStdio, or srv.Connect for another transport.

func RunAcceptLoop

func RunAcceptLoop(ctx context.Context, ln net.Listener, srv *sdk.Server, sockPath string) error

RunAcceptLoop serves connections with no reaper and no idle shutdown, so an e2e test can drive a real socket session without the daemon lifecycle.

func RunDaemon

func RunDaemon(ctx context.Context, sourceRoot, dbPath string, now func() time.Time, watchInterval time.Duration) error

RunDaemon binds the socket next to dbPath and serves until auto-shutdown.

Taking sourceRoot and dbPath explicitly is what makes the daemon cwd-independent: it consults neither the working directory nor the realm resolver, so it survives being spawned from a realm root or any non-git directory. Pass nil for now to use real time; watchInterval 0 disables sync.

func RunProxy

func RunProxy(ctx context.Context, sourceRoot, dbPath string, opts WatchOptions, spawn SpawnFunc, stdin io.Reader, stdout io.Writer) error

RunProxy pipes stdio to the daemon's socket, which is derived from dbPath so proxy and daemon always agree on the path. On client disconnect the proxy exits but the daemon stays up, so the next invocation reuses a warm engine.

func RunStdio

func RunStdio(ctx context.Context, projectRoot string) error

RunStdio backs `atomic code mcp`.

func SocketPath deprecated

func SocketPath(projectRoot string) string

SocketPath assumes the db sits at the canonical position under projectRoot.

Deprecated: use SocketPathFromDB, which works for realm members too.

func SocketPathFromDB

func SocketPathFromDB(dbPath string) string

SocketPathFromDB places the socket beside the db rather than in the source tree, so a realm member's socket lands in the realm's .atomic directory instead of inside the checked-out repo.

Types

type Daemon

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

Daemon is the per-project unix-socket MCP singleton. Start it with RunDaemon; NewTestDaemon builds one with short, injectable durations.

func NewTestDaemon

func NewTestDaemon(sockPath string, srv *sdk.Server, now func() time.Time, idleDuration, reapDuration, syncDuration time.Duration, syncFn SyncFunc) *Daemon

NewTestDaemon binds the listener before returning, so a caller can poll IsLive immediately instead of racing a goroutine that has yet to start. Nil now means real time; nil syncFn means a no-op.

func (*Daemon) RegistryCount

func (d *Daemon) RegistryCount() int

RegistryCount is a test seam: polling IsLive instead would open a connection and so perturb the very idle-shutdown behaviour under test.

func (*Daemon) Run

func (d *Daemon) Run(ctx context.Context) error

Run is the accept and lifecycle loop.

type ExploreOutputBudget

type ExploreOutputBudget struct {
	MaxOutputChars       int
	DefaultMaxFiles      int
	MaxCharsPerFile      int
	GapThreshold         int
	ExcludeLowValueFiles bool
}

ExploreOutputBudget caps one explore call's output. Every literal below is pinned by a table-driven test, including the invariant that MaxCharsPerFile never decreases as repos grow.

func GetExploreOutputBudget

func GetExploreOutputBudget(fileCount int) ExploreOutputBudget

GetExploreOutputBudget returns the output budget for the given file count.

type SpawnFunc

type SpawnFunc func(sourceRoot, dbPath string, opts WatchOptions) error

SpawnFunc starts the daemon when the socket is absent or dead. Production forks a detached subprocess; tests inject an in-process goroutine instead.

type SyncFunc

type SyncFunc func(ctx context.Context) error

SyncFunc is a field rather than a call to eng.Sync so tests can spy on it without standing up a real engine. RunDaemon wires it to eng.Sync.

type WatchOptions

type WatchOptions struct {
	// Disable turns the poller off entirely (--no-watch).
	Disable bool
	// Interval overrides SyncInterval when non-zero (--watch-interval).
	Interval time.Duration
}

WatchOptions carries the sync-poller flags forwarded to the daemon on spawn.

Jump to

Keyboard shortcuts

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