mcp

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package mcp implements the embedded MCP server that exposes the Escape CLI commands as MCP tools over HTTP.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InjectAuthContext

func InjectAuthContext(ctx context.Context, req *http.Request) context.Context

InjectAuthContext derives a context that carries the credentials extracted from the incoming HTTP request so downstream tool handlers can access them.

func RegisterBuiltinTools

func RegisterBuiltinTools(server *mcpserver.MCPServer, specs []ToolSpec)

RegisterBuiltinTools registers the MCP-native helper tools (e.g. the tool discovery endpoint) that ship with the embedded server.

func RegisterCommandTools

func RegisterCommandTools(
	server *mcpserver.MCPServer,
	specs []ToolSpec,
	options CommandExecutionOptions,
)

RegisterCommandTools walks the supplied specs and registers one MCP handler per tool on the server. Each handler spawns a bounded CLI subprocess.

Types

type Auth

type Auth struct {
	APIKey        string
	Authorization string
}

Auth carries the credentials extracted from an incoming MCP request, forwarded to the CLI subprocess through sanitized environment variables.

func AuthFromContext

func AuthFromContext(ctx context.Context) (Auth, error)

AuthFromContext recovers the Auth value previously stored by InjectAuthContext and reports an error if the request lacks credentials.

type CommandExecutionOptions

type CommandExecutionOptions struct {
	PublicAPIURL string
}

CommandExecutionOptions carries the shared runtime inputs the tool handlers need that are not part of a specific tool spec.

type ExecutionOptions

type ExecutionOptions struct {
	Command        []string
	DisplayCommand []string
	Body           any
	Auth           Auth
	PublicAPIURL   string
}

ExecutionOptions carries the request-scoped inputs ExecuteCLICommand needs to spawn one CLI subprocess.

type ExecutionResult

type ExecutionResult struct {
	Stdout          string
	Stderr          string
	StdoutTruncated bool
	StderrTruncated bool
	ExitCode        int
	Payload         any
}

ExecutionResult is the captured stdout/stderr/exit-code plus the parsed JSON payload (when the CLI produced valid JSON on stdout).

func ExecuteCLICommand

func ExecuteCLICommand(ctx context.Context, options ExecutionOptions) (*ExecutionResult, error)

ExecuteCLICommand spawns the current escape-cli binary with the supplied command + arguments, forwards authentication through a sanitized environment, pipes the optional request body to stdin, and returns the structured result. The caller is responsible for binding a timeout on ctx.

type FlagBinding

type FlagBinding struct {
	Property string
	FlagName string
	Kind     string
}

FlagBinding maps an MCP tool property to a CLI flag on the underlying Cobra command, including the type the value must be rendered as.

type Server

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

Server is the embedded MCP server that exposes CLI commands over HTTP.

func NewServer

func NewServer(options ServerOptions) *Server

NewServer builds a non-started embedded MCP server from the supplied options.

func (*Server) Serve

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

Serve starts the embedded MCP server and blocks until the supplied context is cancelled. The shutdown path uses a detached context to give in-flight handlers a bounded drain window.

type ServerOptions

type ServerOptions struct {
	Version      string
	Port         int
	PublicAPIURL string
	Tools        []ToolSpec
}

ServerOptions configures the embedded HTTP-based MCP server.

type ToolSpec

type ToolSpec struct {
	Name           string
	Path           string
	Description    string
	Tool           mcpgo.Tool
	Command        []string
	PositionalArgs []string
	FlagBindings   []FlagBinding
	BodyProperty   string
	// AllowExtraArgs opts the tool into reading a free-form `args` string array
	// from the request payload and forwarding it verbatim to the subprocess.
	// Off by default so the Cobra-to-MCP mapping remains an explicit allowlist.
	AllowExtraArgs bool
}

ToolSpec fully describes one CLI-backed MCP tool: its MCP-level metadata plus the subprocess invocation plan (command path, positional + flag bindings, and optional stdin body property).

Jump to

Keyboard shortcuts

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