Powered by AppSignal & Oban Pro

MCP v2 And MCP Apps

notebooks/mcp-v2-and-apps.livemd

MCP v2 And MCP Apps

Run in Livebook

This chapter carries structured messages of the 2026-07-28 MCP revision through the guard intact, then verifies the resource metadata that accompanies an MCP App. A wrapper that flattens a message into text loses the distinctions the signature protects: false, 0, nil, and a missing field are different operations, and so are an interim MCP result and a completed one. SigilGuard supplies the security checks; protocol negotiation, JSON Schema evaluation, OAuth, and browser sandbox construction remain host work.

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)

Carry The Negotiated Revision

The host negotiates MCP and validates required client capabilities. It then passes the selected revision into SigilGuard. The helper can also read the standard per-request metadata used below.

alias SigilGuard.MCP.{Protocol, SecurityPayload}

request = %{
  "jsonrpc" => "2.0",
  "id" => "mcp-v2-talk",
  "method" => "tools/call",
  "params" => %{
    "name" => "payments/create",
    "arguments" => %{
      "amount" => 1250,
      "approved" => false,
      "note" => nil,
      "tags" => ["conference", "demo"]
    },
    "_meta" => %{
      "io.modelcontextprotocol/protocolVersion" => "2026-07-28",
      "io.modelcontextprotocol/clientCapabilities" => %{"sampling" => %{}}
    }
  }
}

"2026-07-28" = Protocol.version(request)
true = Protocol.modern?(request)
"2026-07-28" = Protocol.current_version()

%{selected: Protocol.version(request), modern?: Protocol.modern?(request)}

One Message, Two Projections

SecurityPayload.for_gate/3 produces a text view for pattern scanning and an exact structured binding for confirmation and attestation digests. The scanner may read the words; authority binds the data structure.

projection = SecurityPayload.for_gate(request, :request)
arguments = projection.binding["payload"]["arguments"]

false = arguments["approved"]
nil = arguments["note"]
1250 = arguments["amount"]
true = projection.text =~ "conference"

%{
  scanner_view: projection.text,
  binding: projection.binding,
  extracted_tool: projection.tool
}

Transport-only correlation and progress data are excluded from the binding, but client capabilities and unknown behavior-changing metadata stay. Guard metadata is also excluded so attaching an attestation cannot change the action digest it attests.

decorated =
  request
  |> Map.put("_agent_trust", %{"transport" => "not user input"})
  |> put_in(
    ["params", "_meta", "progressToken"],
    "display-only-progress"
  )

base_binding = SecurityPayload.request(request)
decorated_binding = SecurityPayload.request(decorated)

true = base_binding == decorated_binding
%{stable_binding: true, digest_input: base_binding}

Multi-Round-Trip Results Stay Multi-Round-Trip

Modern MCP can return input_required and ask the client for another protocol exchange. SigilGuard does not silently rewrite that into a completed response, and its security projection binds inputRequests, inputResponses, and opaque requestState.

interim_result = %{
  "resultType" => "input_required",
  "content" => [%{"type" => "text", "text" => "Approval is required"}],
  "inputRequests" => %{
    "approval" => %{"method" => "elicitation/create", "params" => %{"mode" => "form"}}
  },
  "inputResponses" => %{},
  "requestState" => %{"cursor" => 4, "nonce" => "state-for-demo"}
}

unchanged = Protocol.ensure_result_type(interim_result, request)
result_binding = SecurityPayload.result(unchanged, protocol_version: "2026-07-28")

true = Protocol.input_required?(unchanged)
"input_required" = Protocol.result_type(unchanged)
%{"cursor" => 4, "nonce" => "state-for-demo"} = result_binding["payload"]["requestState"]

%{
  result_type: Protocol.result_type(unchanged),
  input_request_names: unchanged["inputRequests"] |> Map.keys(),
  bound_state: result_binding["payload"]["requestState"]
}

A result without a discriminator gets complete only for the revision that defines the helper semantics. An unknown future revision is not guessed to be compatible.

plain_result = %{"content" => [%{"type" => "text", "text" => "done"}]}

modern_complete = Protocol.ensure_result_type(plain_result, request)
future_untouched = Protocol.ensure_result_type(plain_result, "future", protocol_version: "2030-01-01")

"complete" = modern_complete["resultType"]
false = Map.has_key?(future_untouched, "resultType")

%{modern: modern_complete, unknown_future: future_untouched}

Verify An MCP App Resource Before Rendering

An MCP App adds browser-facing authority. The server returns HTML and declares the network origins and browser permissions it expects. The host compares that claim with its reviewed allowlists before any renderer sees the bytes.

The digest below pins the exact HTML. It does not claim the HTML is harmless; HTML validation and iframe isolation are separate host controls.

alias SigilGuard.MCP.AppResource

html = "<!doctype html><title>Repository review</title><main>Verified review UI</main>"

html_sha256 =
  html
  |> then(&:crypto.hash(:sha256, &1))
  |> Base.encode16(case: :lower)

app_resource = %{
  "uri" => "ui://repo/review",
  "mimeType" => "text/html;profile=mcp-app",
  "text" => html,
  "_meta" => %{
    "ui" => %{
      "csp" => %{
        "connectDomains" => ["https://api.example.test"],
        "resourceDomains" => ["https://cdn.example.test"]
      },
      "permissions" => %{"clipboardWrite" => %{}},
      "domain" => "review.example.test",
      "prefersBorder" => true
    }
  }
}

app_opts = [
  expected_uri: "ui://repo/review",
  expected_sha256: html_sha256,
  allowed_connect_domains: ["https://api.example.test"],
  allowed_resource_domains: ["https://cdn.example.test"],
  allowed_permissions: ["clipboardWrite"],
  allowed_app_domains: ["review.example.test"]
]

{:ok, verified_app} = AppResource.verify(app_resource, app_opts)

%{
  uri: verified_app.uri,
  digest: verified_app.sha256,
  permissions: verified_app.permissions,
  csp: verified_app.csp,
  prefers_border: verified_app.prefers_border
}

On stage: add one unreviewed origin. A declared CSP is input to policy, not permission granted by the server to itself.

expanded_resource =
  put_in(
    app_resource,
    ["_meta", "ui", "csp", "connectDomains"],
    ["https://api.example.test", "https://collector.attacker.test"]
  )

{:error, :domain_not_allowed} = AppResource.verify(expanded_resource, app_opts)

tampered_html = Map.put(app_resource, "text", html <> "<!-- changed -->")
{:error, :resource_digest_mismatch} = AppResource.verify(tampered_html, app_opts)

%{unreviewed_origin: :blocked, changed_html: :blocked}

Constrain Which Caller May See The App Tool

The capability manifest can bind a ui:// resource and distinguish model and app callers. For a manifest whose ui.visibility is ["app"], the gateway refuses a call from another origin with :app_visibility_denied, and a call from an App on another MCP server with :app_server_mismatch. Manifest pinning itself is covered in the Agent Trust Gateway chapter.

app_tool_manifest = %{
  "allowed_sink_zones" => ["semi_trusted", "trusted"],
  "allowed_source_zones" => ["semi_trusted", "trusted"],
  "annotations" => %{"readOnlyHint" => true, "title" => "Submit Review"},
  "description" => "Record the reviewer's decision from the review UI.",
  "expires_at" => "2027-01-01T00:00:00.000Z",
  "input_schema" => %{
    "$schema" => "https://json-schema.org/draft/2020-12/schema",
    "additionalProperties" => false,
    "properties" => %{"decision" => %{"type" => "string"}},
    "required" => ["decision"],
    "type" => "object"
  },
  "input_sensitivity" => "internal",
  "issuer_keyid" =>
    "sha256:65b60673d6ed884bf01c2c222d82ada0740f29ac3355d6a925c81f17f47a27b8",
  "manifest_format" => "sigil_guard_capability_manifest/v2",
  "name" => "review_submit",
  "network_access" => "none",
  "output_sensitivity" => "internal",
  "reversibility" => "reversible",
  "sandbox" => %{"required" => false},
  "scopes" => ["repo:review"],
  "server" => "repo-mcp",
  "side_effects" => ["read"],
  "suspicious_params" => [],
  "ui" => %{"resource_uri" => "ui://repo/review", "visibility" => ["app"]},
  "version" => "1.0.0"
}

app_call = %{
  "jsonrpc" => "2.0",
  "id" => "app-call-1",
  "method" => "tools/call",
  "params" => %{"name" => "review_submit", "arguments" => %{"decision" => "approve"}}
}

guard_as = fn origin, server ->
  SigilGuard.ToolGateway.guard_request(
    app_call,
    [
      phase: :tool_request,
      origin: origin,
      sink: :tool,
      trust_level: :high,
      mcp_server: server,
      tool: "review_submit"
    ],
    manifests: %{"review_submit" => app_tool_manifest},
    require_manifest: true,
    now: ~U[2026-08-19 10:00:00.000Z]
  )
end

from_app = guard_as.(:app, "repo-mcp")
from_model = guard_as.(:model, "repo-mcp")
from_other_server = guard_as.(:app, "other-mcp")

:allowed = from_app.verdict
:app_visibility_denied = from_model.audit_metadata.deny_reason
:app_server_mismatch = from_other_server.audit_metadata.deny_reason

%{
  app_on_its_server: from_app.verdict,
  model_caller: from_model.audit_metadata.deny_reason,
  app_from_another_server: from_other_server.audit_metadata.deny_reason,
  browser_enforcement: "host-owned"
}

Next: Runtime, Streaming, And Telemetry.