mcp

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package mcp provides MCP (Model Context Protocol) server functionality. This file implements the AgentTracker, which manages spawned agent processes with resource limits, timeout enforcement, and output capture.

Package mcp implements an MCP (Model Context Protocol) server that exposes grut's git and file operations as tools for AI agent integration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSensitivePath

func IsSensitivePath(path string) error

IsSensitivePath reports whether the given path (relative to the repo root) points to a file that should never be read or written through MCP or chat file operations. This blocks access to credential stores, secret keys, and internal git metadata while still allowing benign dot-files such as .gitignore and .gitattributes.

Types

type AgentInfo

type AgentInfo struct {
	StartedAt time.Time
	EndedAt   time.Time
	Command   string
	Args      []string
	PID       int
	Status    AgentStatus
	ExitCode  int
	Duration  time.Duration
}

AgentInfo holds metadata about a tracked agent process.

type AgentStatus

type AgentStatus int

AgentStatus represents the lifecycle state of a tracked agent.

const (
	// AgentRunning indicates the agent process is still executing.
	AgentRunning AgentStatus = iota
	// AgentExited indicates the agent process exited successfully (code 0).
	AgentExited
	// AgentFailed indicates the agent process exited with a non-zero code.
	AgentFailed
)

func (AgentStatus) String

func (s AgentStatus) String() string

String returns a human-readable label for the agent status.

type AgentTracker

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

AgentTracker manages spawned agent processes with concurrent access safety, resource limits (max concurrent), timeout enforcement, and output capture.

func NewAgentTracker

func NewAgentTracker(maxProcesses, timeoutSeconds int) *AgentTracker

NewAgentTracker creates a new AgentTracker with the given limits. maxProcesses <= 0 defaults to 5; timeoutSeconds <= 0 defaults to 1800.

func (*AgentTracker) Kill

func (t *AgentTracker) Kill(pid int) error

Kill terminates a specific agent by PID.

func (*AgentTracker) KillAll

func (t *AgentTracker) KillAll()

KillAll terminates all running agents. Called on application shutdown.

func (*AgentTracker) List

func (t *AgentTracker) List() []AgentInfo

List returns info for all tracked agents, in no particular order.

func (*AgentTracker) Output

func (t *AgentTracker) Output(pid int) (stdout, stderr []string)

Output returns captured stdout and stderr lines for the given PID. For running agents, only previously-parsed output is returned because the underlying bytes.Buffer is concurrently written to by the exec package and reading it here without additional synchronisation would be a data race.

func (*AgentTracker) RunningCount

func (t *AgentTracker) RunningCount() int

RunningCount returns the number of currently running agents.

func (*AgentTracker) Spawn

func (t *AgentTracker) Spawn(ctx context.Context, command string, args []string) (int, error)

Spawn starts a new agent process and tracks it. Returns the assigned PID (or internal ID in test mode) and an error if limits are exceeded.

type AuditLogger

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

AuditLogger records MCP tool invocations to a structured log file.

func NewAuditLogger

func NewAuditLogger(cfg config.MCPSecurityConfig) (*AuditLogger, error)

NewAuditLogger creates an audit logger based on the MCP security config. If audit logging is disabled, the returned logger is a no-op.

func (*AuditLogger) Close

func (al *AuditLogger) Close() error

Close releases the underlying log file, if any.

func (*AuditLogger) Log

func (al *AuditLogger) Log(tool string, params map[string]any, status string, duration time.Duration)

Log records a tool invocation. Parameters are sanitized to avoid logging sensitive content (e.g. file contents are redacted).

type PathJail

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

PathJail restricts file operations to within the git repository root. It resolves symlinks and rejects traversal attempts before any I/O occurs.

func NewPathJail

func NewPathJail(root string, followSymlinks bool) (*PathJail, error)

NewPathJail creates a PathJail anchored at root. The root path is resolved to an absolute path and symlinks are evaluated to establish the canonical boundary.

func (*PathJail) Root

func (j *PathJail) Root() string

Root returns the canonical root path of the jail.

func (*PathJail) Validate

func (j *PathJail) Validate(path string) (string, error)

Validate checks that path is within the jail boundary. It returns the resolved absolute path on success or an error if the path would escape the repository root.

type RateLimiter

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

RateLimiter implements per-category token bucket rate limiting. Categories are "read" and "write" with independently configurable rates expressed as calls per minute.

func NewRateLimiter

func NewRateLimiter(readPerMin, writePerMin int) *RateLimiter

NewRateLimiter creates a rate limiter with separate read and write rates (calls per minute).

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(category string) bool

Allow checks whether a call in the given category is permitted. Returns true if the call is allowed (and consumes a token), false if the rate limit has been exceeded.

type Server

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

Server wraps an MCP protocol server with grut git and file tools, rate limiting, path jailing, and audit logging.

func NewServer

func NewServer(gitClient git.GitClient, repoRoot string, cfg *config.Config) (*Server, error)

NewServer creates a fully configured MCP server with all git and file tools registered. The server is ready to serve over stdin/stdout.

func (*Server) MCPServer

func (s *Server) MCPServer() *mcpserver.MCPServer

MCPServer returns the underlying MCPServer for testing.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve starts the MCP server on stdin/stdout (headless mode). It blocks until the context is cancelled or the transport closes.

Jump to

Keyboard shortcuts

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