Powered by AppSignal & Oban Pro

Hermes Integration Stub

notebooks/hermes-integration.livemd

Hermes Integration Stub

Run in Livebook

This notebook guards a request immediately before the callback that owns side effects, and the result immediately before it returns to model context, using a small Hermes-shaped stub. Supervision, transport, routing, and protocol negotiation remain the framework's job. The notebook does not install hermes_mcp, so it stays offline and does not pretend a stub validates a moving framework API; the versioned compile record and production wiring live in guides/integrations/hermes-mcp.md.

Setup

repo_root = Path.expand("..", __DIR__)
local_checkout? = File.regular?(Path.join(repo_root, "mix.exs"))

{sigil_guard_dep, install_opts} =
  if local_checkout? do
    {
      {:sigil_guard, path: repo_root, env: :prod},
      [
        config_path: Path.join(repo_root, "config/config.exs"),
        lockfile: Path.join(repo_root, "mix.lock")
      ]
    }
  else
    {{:sigil_guard, "~> 1.0"}, []}
  end

Mix.install([sigil_guard_dep], install_opts)

One Gate For Every Transport

The same gateway works for HTTP MCP, stdio MCP, and an in-process tool. The host supplies a canonical server/resource identifier; SigilGuard never tries to infer authority from a socket or process name.

alias SigilGuard.TransportExamples

http_context =
  TransportExamples.http_mcp_context(
    "https://mcp.example.test/mcp",
    "repo_file_write"
  )

stdio_context =
  TransportExamples.stdio_mcp_context("stdio:repo-tools", "repo_file_write")

in_process_context =
  TransportExamples.in_process_context("local:release-runner", "repo_file_write")

%{
  http: %{server: http_context.mcp_server, resource: http_context.resource_uri},
  stdio: %{server: stdio_context.mcp_server, resource: stdio_context.resource_uri},
  in_process: %{
    server: in_process_context.mcp_server,
    resource: in_process_context.resource_uri
  }
}

Hermes-Shaped Frame Stub

defmodule HermesNotebookStub.Frame do
  defstruct assigns: %{}, private: %{}
end

frame = struct(HermesNotebookStub.Frame, assigns: %{actor: "did:web:alice"})

SigilGuard Interceptor

defmodule HermesNotebookStub.Interceptor do
  alias SigilGuard.{Decision, ToolGateway}

  @digest String.duplicate("a", 64)

  def before_tool_call(tool_name, arguments, actor, opts \\ []) do
    request = %{
      "jsonrpc" => "2.0",
      "id" => "stub-call",
      "method" => "tools/call",
      "params" => %{"name" => tool_name, "arguments" => arguments}
    }

    case ToolGateway.guard_request(request, request_context(tool_name, actor), opts) do
      %Decision{action: :allow} ->
        {:cont, arguments}

      %Decision{} = denied ->
        {:halt, denial_response(denied, request["id"])}
    end
  end

  def after_tool_call(tool_name, result, actor, opts \\ []) do
    case ToolGateway.guard_result(result, result_context(tool_name, actor), opts) do
      %Decision{action: :allow} ->
        {:cont, result, :allow}

      %Decision{} = denied ->
        {:halt, denial_response(denied, "stub-call")}
    end
  end

  defp denial_response(%Decision{action: :redact} = decision, id) do
    denial_response(%{decision | action: :block, verdict: :blocked, effect: nil,
      sanitized_text: nil, reason: "structured redaction requires a host transform"}, id)
  end

  defp denial_response(decision, id), do: ToolGateway.response_for_decision(decision, id)

  defp request_context(tool_name, actor) do
    [
      phase: :tool_request,
      source: :model,
      origin: :model,
      sink: :tool,
      actor: actor,
      action: tool_name,
      trust_zone: :semi_trusted,
      trust_level: :medium,
      tool: %{name: tool_name, side_effects: ["read"], manifest_digest: @digest},
      action_digest: @digest,
      payload_digest: @digest,
      context_digest: @digest,
      sandbox: %{isolation_level: :container}
    ]
  end

  defp result_context(tool_name, actor) do
    [
      phase: :tool_result,
      source: :tool,
      origin: :tool,
      sink: :model,
      actor: actor,
      action: tool_name,
      trust_zone: :semi_trusted,
      trust_level: :medium,
      tool: %{name: tool_name, side_effects: ["read"], manifest_digest: @digest},
      action_digest: @digest,
      payload_digest: @digest,
      context_digest: @digest
    ]
  end
end

Hermes-Shaped Tool Component

defmodule HermesNotebookStub.GuardedEcho do
  alias HermesNotebookStub.Interceptor

  def execute(arguments, frame, opts \\ []) do
    actor = frame.assigns[:actor] || "anonymous"

    with {:cont, guarded_args} <-
           Interceptor.before_tool_call(
             "echo",
             arguments,
             actor,
             Keyword.get(opts, :request_opts, [])
           ),
         result <- %{text: Map.get(guarded_args, :text, Map.get(guarded_args, "text", ""))},
         {:cont, guarded_result, guard_action} <-
           Interceptor.after_tool_call(
             "echo",
             result,
             actor,
             Keyword.get(opts, :result_opts, [])
           ) do
      {:ok, guarded_result, %{frame | private: Map.put(frame.private, :guard_action, guard_action)}}
    else
      {:halt, error} -> {:error, error, frame}
    end
  end
end

Allowed Tool Call

{:ok, echoed, allowed_frame} = HermesNotebookStub.GuardedEcho.execute(%{text: "hello"}, frame)

true = echoed === %{text: "hello"}

%{
  result: echoed,
  guard_action: allowed_frame.private.guard_action
}

Structured Redaction Requires A Host Transform

Scanner text cannot replace a result map. This generic interceptor refuses the redaction request; a host-specific transform must preserve the result schema and be revalidated before release.

{:halt, redaction_error} =
  HermesNotebookStub.Interceptor.after_tool_call(
    "echo",
    %{text: "temporary key AKIAIOSFODNN7EXAMPLE"},
    frame.assigns.actor,
    on_sensitive: :redact
  )

"blocked" = redaction_error["error"]["data"]["status"]
false = String.contains?(Jason.encode!(redaction_error), "AKIAIOSFODNN7EXAMPLE")
redaction_error

Denial Maps To JSON-RPC Error

{:error, error_response, _frame} =
  HermesNotebookStub.GuardedEcho.execute(%{text: "hello"}, frame,
    request_opts: [require_manifest: true, manifests: %{}]
  )

%{
  jsonrpc: error_response["jsonrpc"],
  code: error_response["error"]["code"],
  status: error_response["error"]["data"]["status"],
  reason: error_response["error"]["data"]["reason"]
}

The Adapter-Shaped Entry Points

SigilGuard.MCP.Gateway.guarded_request/3 returns {:ok, decision} or {:error, error_response, decision}, and guarded_result/3 returns {:ok, released, decision}. An adapter can match these tuples directly.

alias SigilGuard.MCP.Gateway

adapter_context = [
  phase: :tool_request,
  origin: :model,
  sink: :tool,
  actor: "did:web:alice",
  trust_level: :medium,
  tool: "echo"
]

echo_call = %{
  "jsonrpc" => "2.0",
  "id" => "stub-call-2",
  "method" => "tools/call",
  "params" => %{"name" => "echo", "arguments" => %{"text" => "hello"}}
}

{:ok, call_decision} = Gateway.guarded_request(echo_call, adapter_context)

{:error, unknown_tool_error, unknown_tool_decision} =
  Gateway.guarded_request(echo_call, adapter_context, manifests: %{}, require_manifest: true)

:allow = call_decision.action
"stub-call-2" = unknown_tool_error["id"]
:unknown_manifest = unknown_tool_decision.audit_metadata.deny_reason

echo_result = %{
  "jsonrpc" => "2.0",
  "id" => "stub-call-2",
  "result" => %{"content" => [%{"type" => "text", "text" => "hello"}]}
}

result_side = Keyword.merge(adapter_context, phase: :tool_result, origin: :tool, sink: :model)

{:ok, released, result_decision} = Gateway.guarded_result(echo_result, result_side)

:allow = result_decision.action
"hello" = get_in(released, ["result", "content", Access.at(0), "text"])

%{
  request: call_decision.action,
  unknown_tool: unknown_tool_error["error"]["data"]["status"],
  released: released
}

Gateway.stream_result/2, guarded_result_chunk/3 and finish_guarded_result_stream/2 apply the stream holdback to a result, so a credential split across two chunks reaches the client as [AWS_KEY].

guarded_stream = Gateway.stream_result(result_side, stream_window_bytes: 64)
filler = String.duplicate("log line ", 40)

{guarded_stream, {:ok, first_frame, _}} =
  Gateway.guarded_result_chunk(guarded_stream, filler <> "AKIAIOS")

{guarded_stream, {:ok, second_frame, _}} =
  Gateway.guarded_result_chunk(guarded_stream, "FODNN7EXAMPLE done")

{_guarded_stream, {:ok, last_frame, final_stream_decision}} =
  Gateway.finish_guarded_result_stream(guarded_stream)

frame_text = fn
  nil -> ""
  frame -> get_in(frame, ["result", "content", Access.at(0), "text"])
end

streamed = Enum.map_join([first_frame, second_frame, last_frame], frame_text)

:allowed = final_stream_decision.verdict
false = String.contains?(streamed, "AKIAIOSFODNN7EXAMPLE")
true = String.ends_with?(streamed, "[AWS_KEY] done")

%{frames: 3, final: final_stream_decision.verdict, tail: String.slice(streamed, -24, 24)}

Outcomes Of The Three Calls

%{
  allowed: allowed_frame.private.guard_action,
  redaction_refused: redaction_error["error"]["data"]["status"],
  denied_status: error_response["error"]["data"]["status"]
}

Next: Physical-Effect Requests.