SuperAPI AI Directory

heimdall-mcp

Transparent MCP proxy that intercepts JSON-RPC messages, measures latency, stores traces in SQLite, PostgreSQL or MySQL, and exports OpenTelemetry spans.

Open website

About this tool

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: true performing containment checks on directory root patterns (using path.resolve + fs.realpathSync to prevent ../ traversal and symlink escape), with non-directory root patterns falling back to regex matching. Supports warn_only: true for gradual rollout, only observing violations without blocking.
  • Resource locks: Configured per server with locks blocks, keyed by tool name, to prevent concurrent tools/call calls 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:hosts is an independent top-level field used to configure lock policies for host-native tools (such as Claude Code's Write、Edit、MultiEdit、NotebookEdit). These tools are not MCP servers and are not intercepted by the JSON-RPC proxy. Export CLAUDE_CODE_DEFAULT_HOST_POLICY as a ready-made host policy, with each tool locking on the file_path parameter and a TTL of 30 seconds.Bash is intentionally excluded because its arbitrary shell commands have no single stable resource parameter to lock on.
  • Hook scripts:bin/hooks/claude-pretooluse.js is Claude Code's PreToolUse hook script. On each call it reads tool_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.ts is an OpenCode plugin that implements the tool.execute.before hook, with similar logic but matching hosts['opencode']. The OpenCode plugin runs in-process, with no subprocess and no stdin/stdout.

Usage

Four usage modes are provided:

  1. CLI wrapping a local subprocess (stdio): wraps a local MCP server subprocess.
  2. CLI wrapping a remote HTTP server: wraps a remote HTTP MCP server.
  3. CLI wrapping a remote SSE server: wraps a remote SSE MCP server.
  4. 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.LockStore interface defines LockMode = 'read' | 'write', but LockInterceptor is hardcoded to acquire() in all 'write',LockRuleSchema calls, there is no mode field, 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_POLICY corresponding to OPENCODE_DEFAULT_HOST_POLICY), and allows all tool calls until hosts.opencode.locks is explicitly configured.
  • Bash is intentionally excluded from CLAUDE_CODE_DEFAULT_HOST_POLICY , with no recommended lock rules.
  • If neither local nor global configuration sets hosts['claude-code'], then CLAUDE_CODE_DEFAULT_HOST_POLICY is automatically applied as the baseline; any user-provided hosts['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.md and SPIKE_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.