Purpose
heimdall-mcp is a transparent proxy used to wrap any MCP (Model Context Protocol) server. It forwards JSON-RPC messages between the MCP client and the real server, while intercepting every request/response pair and converting it into a span with timing information, attributes, and input/output bodies for observability analysis. The proxy always exposes a stdio interface to the MCP client and communicates with the real server using the correct transport.
Main capabilities
- Tracing and observability: Intercepts all JSON-RPC messages, measures latency, stores trace data in SQLite, PostgreSQL, or MySQL, and supports exporting OpenTelemetry spans (viewable through the Jaeger UI).
- Policy configuration: Through the project root directory's
heimdall.config.ts(also supports.js、.mjs、.cjs、.json) defines the tools, prompts, and resources each MCP server is allowed to expose. When local configuration and global configuration are merged, security-first semantics are used: the global configuration sets a baseline that the team cannot relax, and the local configuration can only add restrictions, not reduce them. - Parameter policy: Adds a second layer of enforcement on top of name-level tool rules, constraining the parameters allowed for each call. Supports
isPath: trueperforming containment checks on directory root patterns (usingpath.resolve+fs.realpathSyncto prevent../traversal and symlink escape), with non-directory root patterns falling back to regex matching. Supportswarn_only: truefor gradual rollout, only observing violations without blocking. - Resource locks: Configured per server with
locksblocks, keyed by tool name, to prevent concurrenttools/callcalls from competing on the same resource. The lock mode is an exclusive write lock, with at most one holder at a time for the same resource key. Supports TTL expiration as a fallback mechanism for stuck holders. If a resource key looks like a filesystem path (absolute path,~、./、../or drive letter), it is normalized (expanded, resolved to an absolute path, symlinks followed), ensuring that the same real file locks consistently no matter how it is referenced. The default lock storage is a local SQLite file~/.config/heimdall/locks.db, and it also supports using Postgres and MySQL for multi-machine lock coordination via--lock-store. - Host policy:
hostsis an independent top-level field used to configure lock policies for host-native tools (such as Claude Code'sWrite、Edit、MultiEdit、NotebookEdit). These tools are not MCP servers and are not intercepted by the JSON-RPC proxy. ExportCLAUDE_CODE_DEFAULT_HOST_POLICYas a ready-made host policy, with each tool locking on thefile_pathparameter and a TTL of 30 seconds.Bashis intentionally excluded because its arbitrary shell commands have no single stable resource parameter to lock on. - Hook scripts:
bin/hooks/claude-pretooluse.jsis Claude Code's PreToolUse hook script. On each call it readstool_name/tool_inputfrom stdin, reloads the configuration, matches lock rules, and attempts to acquire an exclusive lock. If the lock is free, the call is allowed; if it is held by another holder, it is rejected with a readable reason. The hook always exits 0, and silently allows on any unexpected error (fail open).src/plugins/opencode-heimdall.tsis an OpenCode plugin that implements thetool.execute.beforehook, with similar logic but matchinghosts['opencode']. The OpenCode plugin runs in-process, with no subprocess and no stdin/stdout.
Usage
Four usage modes are provided:
- CLI wrapping a local subprocess (stdio): wraps a local MCP server subprocess.
- CLI wrapping a remote HTTP server: wraps a remote HTTP MCP server.
- CLI wrapping a remote SSE server: wraps a remote SSE MCP server.
- Library mode: for developers to integrate in code.
CLI commands include start、init、health. You can use heimdall-mcp init --hooks claude-code to automatically register Claude Code hooks, or heimdall-mcp init --hooks opencode to register the OpenCode plugin. The installer writes using atomic temp files plus rename, is idempotent, and does not blindly overwrite files. The OpenCode installer uses jsonc-parser for precise text editing, preserving comments and trailing commas.
Applicable Scenarios
- Development scenarios that need to monitor and trace MCP server call latency and input/output.
- When multiple AI coding agents (such as two Claude Code instances) operate on the same repository in parallel, prevent simultaneous writes to the same file from silently overwriting changes.
- Serialize database migration tools in parallel sessions to avoid overlapping migrations corrupting state.
- Avoid duplicate/competing calls to rate-limited external APIs.
- Coordinate resource locks across multiple machines and multiple agent instances via shared Postgres/MySQL lock storage.
Limitations
- The lock mode is an exclusive write lock, with no configurable shared/concurrent read mode.
LockStoreinterface definesLockMode = 'read' | 'write', butLockInterceptoris hardcoded toacquire()in all'write',LockRuleSchemacalls, there is nomodefield, and the'read'mode is currently not configurable or reachable from user configuration. - TTL is not a per-call timeout, but a fallback mechanism for stuck holders. Locks are released only through TTL expiration; there is no PostToolUse companion hook to release the lock immediately after the tool call completes.
- Locks acquired by Claude Code hooks are released only through TTL expiration (default 30 seconds or the rule-configured ttl), and are not released immediately after the tool completes; this is a known limitation.
- The OpenCode plugin has no built-in default policy (there is no
CLAUDE_CODE_DEFAULT_HOST_POLICYcorresponding toOPENCODE_DEFAULT_HOST_POLICY), and allows all tool calls untilhosts.opencode.locksis explicitly configured. Bashis intentionally excluded fromCLAUDE_CODE_DEFAULT_HOST_POLICY, with no recommended lock rules.- If neither local nor global configuration sets
hosts['claude-code'], thenCLAUDE_CODE_DEFAULT_HOST_POLICYis automatically applied as the baseline; any user-providedhosts['claude-code']value completely replaces the default value. - For the full investigation and unresolved edge cases regarding the PreToolUse hook contract and OpenCode hooks, see
SPIKE_CLAUDE_HOOKS.mdandSPIKE_OPENCODE_HOOKS.mdrespectively.
Storage and Tracing
Three storage backends are supported: SQLite, PostgreSQL, and MySQL. Tracing data can be exported to the Jaeger UI via OTLP: start Jaeger, add --otlpto the configuration, then open the Jaeger UI. Custom interceptors are also supported.