Skip to main content

CLI Reference

Complete reference for the thoughtgate command-line interface.

thoughtgate wrap​

Wraps an MCP agent: discovers its config file, rewrites server commands to route through ThoughtGate shim proxies, starts the governance service, launches the agent, and restores the original config on exit.

Usage​

thoughtgate wrap [OPTIONS] -- <COMMAND>...

Arguments​

ArgumentDescription
<COMMAND>...Agent command and arguments (after --). Required.

Options​

FlagTypeDefaultDescription
--agent-type <TYPE>enumauto-detectOverride auto-detected agent type. Values: claude-code, claude-desktop, cursor, vscode, windsurf, zed, custom.
--config-path <PATH>pathauto-discoverOverride auto-detected agent config file path.
--profile <PROFILE>enumproductionConfiguration profile. Values: production, development.
--thoughtgate-config <PATH>paththoughtgate.yamlPath to ThoughtGate governance config file.
--governance-port <PORT>u160Port for the governance HTTP service. 0 = OS-assigned ephemeral port.
--no-restoreflagfalseDon't restore original agent config on exit.
--dry-runflagfalsePrint rewritten config diff without writing. Useful for previewing changes.
--verboseflagfalseEnable debug logging.

Examples​

# Basic wrap (auto-detects Claude Code, uses thoughtgate.yaml)
thoughtgate wrap -- claude-code

# Dry run — preview config changes without writing
thoughtgate wrap --dry-run -- claude-code

# Development profile (log-only, no blocking)
thoughtgate wrap --profile development -- claude-code

# Explicit agent type with custom config path
thoughtgate wrap --agent-type cursor --config-path ~/.cursor/mcp.json -- cursor

# Custom ThoughtGate config
thoughtgate wrap --thoughtgate-config /etc/thoughtgate/production.yaml -- claude-code

# Verbose logging for debugging
thoughtgate wrap --verbose -- claude-code

thoughtgate shim​

Per-server stdio shim proxy. Not intended for direct invocation — this subcommand is injected into agent config files by thoughtgate wrap.

Usage​

thoughtgate shim --server-id <ID> --governance-endpoint <URL> --profile <PROFILE> -- <COMMAND>...

Options​

FlagTypeDescription
--server-id <ID>stringServer identifier (from config rewrite).
--governance-endpoint <URL>stringThoughtGate governance service URL.
--profile <PROFILE>enumConfiguration profile (production or development).
<COMMAND>...argsOriginal server command and arguments (after --).

Environment Variables​

Set by ThoughtGate​

These are injected into the subprocess environment by thoughtgate wrap:

VariableDescription
THOUGHTGATE_ACTIVE=1Indicates the process is managed by ThoughtGate. Used for double-wrap detection.
THOUGHTGATE_SERVER_IDServer identifier for the current shim. Injected into agent config env block.
THOUGHTGATE_GOVERNANCE_ENDPOINTURL of the governance HTTP service (e.g., http://127.0.0.1:54321).
note

These variables are scrubbed from the agent subprocess environment to prevent leakage to child processes.

Read by ThoughtGate​

VariableDefaultDescription
THOUGHTGATE_CONFIG—Path to YAML config (sidecar mode only; CLI uses --thoughtgate-config).
THOUGHTGATE_OUTBOUND_PORT7467Proxy port (sidecar mode only).
THOUGHTGATE_ADMIN_PORT7469Admin port (sidecar mode only).
THOUGHTGATE_SLACK_BOT_TOKEN—Slack Bot OAuth token for approval workflows.
THOUGHTGATE_SLACK_CHANNEL#approvalsDefault Slack channel for approvals.
THOUGHTGATE_APPROVAL_TIMEOUT_SECS300Default approval timeout in seconds.
THOUGHTGATE_REQUEST_TIMEOUT_SECS300Default upstream request timeout in seconds.
THOUGHTGATE_MAX_BATCH_SIZE100Maximum Slack batch polling size.
THOUGHTGATE_ENVIRONMENTproductionDeployment environment label (for telemetry).
THOUGHTGATE_TELEMETRY_ENABLEDfalseEnable OTLP trace export.

Exit Codes​

CodeMeaning
0Agent exited successfully, config restored.
1ThoughtGate error (config parse failure, governance service failed to start).
2CLI argument error.
OtherPropagated from the wrapped agent process.

Signal Handling​

ThoughtGate installs signal handlers for graceful shutdown:

SignalBehavior
SIGTERMRestore original config, shut down governance service, forward to agent.
SIGINT (Ctrl+C)Same as SIGTERM.
PanicPanic hook restores config before unwinding.

On shutdown, ThoughtGate:

  1. Restores the original agent config from .thoughtgate-backup
  2. Sends shutdown signal to all shim processes
  3. Waits for shim graceful shutdown (stdin close → SIGTERM → SIGKILL)
  4. Exits with the agent's exit code

Agent Auto-Detection​

When --agent-type is not specified, ThoughtGate detects the agent from the command basename:

BasenameDetected Type
claude, claude-codeClaude Code
claude-desktop, */Claude.app/*Claude Desktop
cursorCursor
code, code-insidersVS Code
windsurfWindsurf
zedZed

If detection fails, ThoughtGate exits with an error suggesting --agent-type.

See Supported Agents for config file locations and formats.