From f65dd8e8bd3a2c1fc05cc9727f8861ba5d49c6cc Mon Sep 17 00:00:00 2001 From: Mr45401 <185895283+Mr45401@users.noreply.github.com> Date: Mon, 9 Nov 2026 18:15:21 +0500 Subject: [PATCH 1/3] MCP client: record what read_resource returns on a wrapped session as an observed input (#667) Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com> --- docs/integrations/mcp.md | 1 + tests/test_agent_framework.py | 2 ++ tests/test_contract_mcp.py | 40 ++++++++++++++++++++++++++++++++--- tests/test_instrument.py | 2 ++ tests/test_pydantic_ai.py | 2 ++ tracekit/autowire.py | 20 ++++++++++++------ tracekit/integrations/mcp.py | 26 ++++++++++++++++++----- 7 files changed, 79 insertions(+), 14 deletions(-) diff --git a/docs/integrations/mcp.md b/docs/integrations/mcp.md index e45b26c5b..c5f4aff74 100644 --- a/docs/integrations/mcp.md +++ b/docs/integrations/mcp.md @@ -38,6 +38,7 @@ Every other attribute is the wrapped session's. Only calls made through the wrap | before the call is sent | `decide` | a fresh `tool_call_id`, the tool as `mcp:/` (`mcp:/prompt:` for `get_prompt`), `tool_class_hint: mcp`, the arguments as given (`parsed`) | | right before it is sent | `approval_consume` | the same call and arguments, and the approval id when the call waited for one | | after the server answers | `complete` | `status` `ok`, or `error` for a result with `isError: true`; a `sha256:` commitment to the result (not the result), or the exception type and message | +| after `read_resource` answers | `observe` | `source` `mcp:/resource`, `trust: untrusted` and the result as `value` (recorded as its commitment and indexed for provenance; a failure to record warns and returns the result unchanged) | `` is the name in the server's initialize result, with every character outside `A-Za-z0-9_.-` replaced by `_` (`unknown` when the server gave none). The policy's `tools` map should map `mcp:*/*` to the `mcp` class (the coding diff --git a/tests/test_agent_framework.py b/tests/test_agent_framework.py index db1837829..2bdd6d8e4 100644 --- a/tests/test_agent_framework.py +++ b/tests/test_agent_framework.py @@ -443,6 +443,7 @@ def list_files(path: str) -> str: return "README.md" self.addCleanup(setattr, ClientSession, "call_tool", ClientSession.call_tool) self.addCleanup(setattr, ClientSession, "get_prompt", ClientSession.get_prompt) + self.addCleanup(setattr, ClientSession, "read_resource", ClientSession.read_resource) self.addCleanup(setattr, ClientSession, "initialize", ClientSession.initialize) async def main(r): @@ -453,6 +454,7 @@ async def main(r): return (await a.run("list them")).text with mock.patch.object(ClientSession, "call_tool", ClientSession.call_tool), \ mock.patch.object(ClientSession, "get_prompt", ClientSession.get_prompt), \ + mock.patch.object(ClientSession, "read_resource", ClientSession.read_resource), \ mock.patch("tracekit.integrations.mcp.TracekitSession") as gate: autowire._mcp(mock.Mock()) # what instrument() wires: every ClientSession decides its calls for r, runs in ((run(), True), (run("deny", ["D1"]), False)): diff --git a/tests/test_contract_mcp.py b/tests/test_contract_mcp.py index a23812f0f..cd1b6055d 100644 --- a/tests/test_contract_mcp.py +++ b/tests/test_contract_mcp.py @@ -144,21 +144,27 @@ async def get_prompt(self, name, arguments=None): raise MCPError(-32603, "boom") return GetPromptResult(messages=[PromptMessage(role="user", content=TextContent(type="text", text="hi"))]) + async def read_resource(self, uri, *a, **kw): + from mcp.types import ReadResourceResult, TextResourceContents + self.calls += 1 + return ReadResourceResult(contents=[TextResourceContents(uri=uri, text=f"body of {uri}")]) + class _StubSession(_Mcp1Session): server_info = type("Info", (), {"name": "stub"})() class _Signer: - """A signer that fails the way it is told: `decide` and `complete` raise `errors[method]` when set.""" + """A signer that fails the way it is told: `decide`, `complete` and `observe` raise `errors[method]` when set.""" def __init__(self, decision="allow", **errors): - self.decision, self.errors, self.tools, self.completed = decision, errors, [], [] + self.decision, self.errors, self.tools, self.completed, self.observed = decision, errors, [], [], [] def __getattr__(self, method): def call(req): self.tools += [req["tool"]] if method == "decide" else [] self.completed += [req] if method == "complete" else [] + self.observed += [req] if method == "observe" else [] if method in self.errors: raise self.errors[method] return {"decide": {"decision": self.decision, "decision_id": "dec-1", "rule_ids": ["r1"]}, @@ -251,7 +257,35 @@ def test_get_prompt_is_decided_and_completed_as_a_prompt_call(self): run = mock.Mock(client=signer, registered=self.RUN) with mock.patch("mcp.ClientSession", _StubSession), \ mock.patch.object(_StubSession, "call_tool", _StubSession.call_tool), \ - mock.patch.object(_StubSession, "get_prompt", _StubSession.get_prompt): + mock.patch.object(_StubSession, "get_prompt", _StubSession.get_prompt), \ + mock.patch.object(_StubSession, "read_resource", _StubSession.read_resource): autowire._mcp(run) out = asyncio.run(_StubSession().get_prompt("triage", {"issue": "1"})) self.assertEqual((out.messages[0].content.text, signer.tools), ("hi", ["mcp:stub/prompt:triage"])) + + def test_read_resource_is_recorded_as_an_untrusted_observed_input(self): + from tracekit.signer.rpc_schema import RPCError + session, signer = _StubSession(), _Signer() + out = asyncio.run(TracekitSession(session, signer, self.RUN).read_resource("note://inbox")) + self.assertEqual((out.contents[0].text, session.calls), ("body of note://inbox", 1)) + self.assertEqual([(r["source"], r["trust"], r["value"]["contents"][0]["text"]) for r in signer.observed], + [("mcp:stub/resource", "untrusted", "body of note://inbox")]) + + session = _StubSession() + with self.assertWarns(UserWarning): + out = asyncio.run(TracekitSession( + session, _Signer(observe=RPCError("unavailable", "storage down")), + self.RUN).read_resource("note://inbox")) + self.assertEqual((out.contents[0].text, session.calls), ("body of note://inbox", 1)) + + from tracekit import autowire + signer = _Signer() + run = mock.Mock(client=signer, registered=self.RUN) + with mock.patch("mcp.ClientSession", _StubSession), \ + mock.patch.object(_StubSession, "call_tool", _StubSession.call_tool), \ + mock.patch.object(_StubSession, "get_prompt", _StubSession.get_prompt), \ + mock.patch.object(_StubSession, "read_resource", _StubSession.read_resource): + autowire._mcp(run) + out = asyncio.run(_StubSession().read_resource("note://inbox")) + self.assertEqual((out.contents[0].text, [(r["source"], r["trust"]) for r in signer.observed]), + ("body of note://inbox", [("mcp:stub/resource", "untrusted")])) diff --git a/tests/test_instrument.py b/tests/test_instrument.py index f662170eb..09db44d8f 100644 --- a/tests/test_instrument.py +++ b/tests/test_instrument.py @@ -220,6 +220,7 @@ async def call_tool(gate, *a, **kw): run = mock.Mock(registered={"run_id": "r", "run_token": "t"}) with mock.patch.object(ClientSession, "call_tool", ClientSession.call_tool), \ mock.patch.object(ClientSession, "get_prompt", ClientSession.get_prompt), \ + mock.patch.object(ClientSession, "read_resource", ClientSession.read_resource), \ mock.patch("tracekit.integrations.mcp.TracekitSession.call_tool", call_tool): autowire._mcp(run) a, b = object.__new__(ClientSession), object.__new__(ClientSession) @@ -239,6 +240,7 @@ async def call_tool(gate, *a, **kw): run = mock.Mock(registered={"run_id": "r", "run_token": "t"}) with mock.patch.object(ClientSession, "call_tool", ClientSession.call_tool), \ mock.patch.object(ClientSession, "get_prompt", ClientSession.get_prompt), \ + mock.patch.object(ClientSession, "read_resource", ClientSession.read_resource), \ mock.patch("tracekit.integrations.mcp.TracekitSession.call_tool", call_tool): autowire._mcp(run) refs = [] diff --git a/tests/test_pydantic_ai.py b/tests/test_pydantic_ai.py index 112606fa2..675f9480c 100644 --- a/tests/test_pydantic_ai.py +++ b/tests/test_pydantic_ai.py @@ -336,6 +336,8 @@ def test_a_call_the_capability_decided_is_not_decided_again_by_the_wired_mcp_ses from mcp import ClientSession original = mock.AsyncMock(return_value="result") self.addCleanup(setattr, ClientSession, "call_tool", ClientSession.call_tool) + self.addCleanup(setattr, ClientSession, "get_prompt", ClientSession.get_prompt) + self.addCleanup(setattr, ClientSession, "read_resource", ClientSession.read_resource) self.addCleanup(setattr, ClientSession, "initialize", ClientSession.initialize) ClientSession.call_tool = original process = mock.Mock() diff --git a/tracekit/autowire.py b/tracekit/autowire.py index 24b22b70e..d487a598e 100644 --- a/tracekit/autowire.py +++ b/tracekit/autowire.py @@ -248,10 +248,12 @@ def hooked(self, *a, **kw): class _Unpatched: - """A ClientSession as TracekitSession sees it: its call_tool and get_prompt are the ones instrument() replaced.""" + """A ClientSession as TracekitSession sees it: its call_tool, get_prompt and read_resource are the ones + instrument() replaced.""" - def __init__(self, session, call_tool, get_prompt=None): - self._session, self._call_tool, self._get_prompt = session, call_tool, get_prompt + def __init__(self, session, call_tool, get_prompt=None, read_resource=None): + self._session, self._call_tool, self._get_prompt, self._read_resource = ( + session, call_tool, get_prompt, read_resource) def __getattr__(self, name): return getattr(self._session, name) @@ -262,19 +264,23 @@ def call_tool(self, *a, **kw): def get_prompt(self, *a, **kw): return self._get_prompt(self._session, *a, **kw) + def read_resource(self, *a, **kw): + return self._read_resource(self._session, *a, **kw) + def _mcp(run): from mcp import ClientSession from .integrations.mcp import TracekitSession - call_tool, get_prompt = ClientSession.call_tool, getattr(ClientSession, "get_prompt", None) + call_tool = ClientSession.call_tool + get_prompt, read_resource = getattr(ClientSession, "get_prompt", None), getattr(ClientSession, "read_resource", None) gates, stream, servers = weakref.WeakKeyDictionary(), {}, weakref.WeakKeyDictionary() def gate(session): if session not in gates: # every session shares one signer stream: a run allows 64 (quotas.streams_per_run) # the gate holds its session weakly: a strong one would keep every session (the dict's key) alive - g = gates[session] = TracekitSession(_Unpatched(weakref.proxy(session), call_tool, get_prompt), - run.client, run.registered, server=servers.get(session)) + unpatched = _Unpatched(weakref.proxy(session), call_tool, get_prompt, read_resource) + g = gates[session] = TracekitSession(unpatched, run.client, run.registered, server=servers.get(session)) g.stream, g._seq, g._lock = stream.setdefault("s", (g.stream, g._seq, g._lock)) return gates[session] @@ -289,6 +295,8 @@ async def gated(self, *a, **kw): ClientSession.call_tool = wrap("call_tool", call_tool) if get_prompt is not None: ClientSession.get_prompt = wrap("get_prompt", get_prompt) + if read_resource is not None: + ClientSession.read_resource = wrap("read_resource", read_resource) if hasattr(ClientSession, "server_info"): return initialize = ClientSession.initialize # mcp 1.x: only initialize()'s result names the server diff --git a/tracekit/integrations/mcp.py b/tracekit/integrations/mcp.py index a84a52705..8ce124630 100644 --- a/tracekit/integrations/mcp.py +++ b/tracekit/integrations/mcp.py @@ -1,5 +1,5 @@ """MCP client (python `mcp` package): every `call_tool` and `get_prompt` on a ClientSession is decided by the signer -before it is sent to the server and recorded after. +before it is sent to the server and recorded after, and `read_resource` records what it returns as an observed input. from tracekit.integrations.mcp import TracekitSession run = signer.register_run({"request_id": "r1", "agent": {"name": "my-agent"}}) @@ -13,8 +13,9 @@ up to `approval_wait_s` for a person to decide in the signer; with `approval_wait_s=0` the call is refused instead. Every call that is not denied is sent only after the signer's `approval_consume` agrees, with the arguments it returns for an approval with a T2 executor. A result with `isError: true` completes as an error; an exception from the session -propagates unchanged and completes as an error. Only `call_tool` and `get_prompt` calls made through the wrapped session -are seen. +propagates unchanged and completes as an error. `read_resource` records the result with `observe` (`source` +`mcp:/resource`, `trust` `untrusted`); if the signer cannot record it, a warning is issued and the result is +returned unchanged. Only `call_tool`, `get_prompt` and `read_resource` calls made through the wrapped session are seen. """ import re import uuid @@ -48,13 +49,28 @@ async def call_tool(self, name, arguments=None, *a, **kw): async def get_prompt(self, name, arguments=None, *a, **kw): return await self._call("get_prompt", "prompt:", name, arguments, *a, **kw) - async def _call(self, method, prefix, name, arguments, *a, **kw): + async def read_resource(self, uri, *a, **kw): + """Read a resource and record its result as an untrusted observed input; a failure to record warns and leaves + the result as it was.""" + result = await self._session.read_resource(uri, *a, **kw) + server = self._server_name() + try: + await self._rpc("observe", source=f"mcp:{server}/resource", trust="untrusted", + value=result.model_dump(mode="json", by_alias=True, exclude_none=True)) + except Exception as e: + warnings.warn(f"tracekit: resource {uri} of mcp:{server} not recorded: {e}", stacklevel=2) + return result + + def _server_name(self): server = self._server or getattr(getattr(self._session, "server_info", None), "name", None) if not server: warnings.warn("MCP: the session doesn't name its server, so its calls are recorded as mcp:unknown/: " "pass TracekitSession(..., server=), or call tracekit.instrument() before the " "session's initialize() (mcp 1.x)", stacklevel=3) - tool = f"mcp:{_UNSAFE.sub('_', server or '') or 'unknown'}/{prefix}{name}" + return _UNSAFE.sub("_", server or "") or "unknown" + + async def _call(self, method, prefix, name, arguments, *a, **kw): + tool = f"mcp:{self._server_name()}/{prefix}{name}" args, call_id = arguments or {}, uuid.uuid4().hex why, d, args = await self._gate(call_id, tool, args) if why is not None: From cb55d554d8006111440ae68a20b8c6a338ca3ca4 Mon Sep 17 00:00:00 2001 From: Mr45401 <185895283+Mr45401@users.noreply.github.com> Date: Thu, 12 Nov 2026 07:44:17 +0500 Subject: [PATCH 2/3] MCP client: withhold an unrecorded resource unless the run fails open Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com> --- docs/integrations/mcp.md | 2 +- tests/test_contract_mcp.py | 34 ++++++++++++++++++++++++++-------- tracekit/integrations/mcp.py | 19 ++++++++++--------- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/docs/integrations/mcp.md b/docs/integrations/mcp.md index c923fcfb6..454afdd94 100644 --- a/docs/integrations/mcp.md +++ b/docs/integrations/mcp.md @@ -38,7 +38,7 @@ Every other attribute is the wrapped session's. Only calls made through the wrap | before the call is sent | `decide` | a fresh `tool_call_id`, the tool as `mcp:/` (`mcp:/prompt:` for `get_prompt`), `tool_class_hint: mcp`, the arguments as given (`parsed`) | | right before it is sent | `approval_consume` | the same call and arguments, and the approval id when the call waited for one | | after the server answers | `complete` | `status` `ok`, or `error` for a result with `isError: true`; a `sha256:` commitment to the result (not the result), or the exception type and message | -| after `read_resource` answers | `observe` | `source` `mcp:/resource`, `trust: untrusted` and the result as `value` (recorded as its commitment and indexed for provenance; a failure to record warns and returns the result unchanged) | +| after `read_resource` answers | `observe` | `source` `mcp:/resource`, `trust: untrusted` and the result as `value` (recorded as its commitment and indexed for provenance; withheld if the signer cannot record it, unless the run fails open for `mcp` and the signer is unreachable, when it warns and returns the result; a refusal is withheld either way) | `list_tools`, `list_resources` and `list_prompts` are recorded as untrusted observed input (`observe`, source `mcp:/tools`, `/resources` or `/prompts`): the model reads the descriptions the server wrote, and the page the caller asked for is what is recorded. A page the signer cannot record is withheld, unless the run fails open for `mcp`, when an unreachable signer returns it with a warning; a refusal is withheld either way. diff --git a/tests/test_contract_mcp.py b/tests/test_contract_mcp.py index dc48b5404..64f26106d 100644 --- a/tests/test_contract_mcp.py +++ b/tests/test_contract_mcp.py @@ -266,20 +266,12 @@ def test_get_prompt_is_decided_and_completed_as_a_prompt_call(self): self.assertEqual((out.messages[0].content.text, signer.tools), ("hi", ["mcp:stub/prompt:triage"])) def test_read_resource_is_recorded_as_an_untrusted_observed_input(self): - from tracekit.signer.rpc_schema import RPCError session, signer = _StubSession(), _Signer() out = asyncio.run(TracekitSession(session, signer, self.RUN).read_resource("note://inbox")) self.assertEqual((out.contents[0].text, session.calls), ("body of note://inbox", 1)) self.assertEqual([(r["source"], r["trust"], r["value"]["contents"][0]["text"]) for r in signer.observed], [("mcp:stub/resource", "untrusted", "body of note://inbox")]) - session = _StubSession() - with self.assertWarns(UserWarning): - out = asyncio.run(TracekitSession( - session, _Signer(observe=RPCError("unavailable", "storage down")), - self.RUN).read_resource("note://inbox")) - self.assertEqual((out.contents[0].text, session.calls), ("body of note://inbox", 1)) - from tracekit import autowire signer = _Signer() run = mock.Mock(client=signer, registered=self.RUN) @@ -292,6 +284,32 @@ def test_read_resource_is_recorded_as_an_untrusted_observed_input(self): self.assertEqual((out.contents[0].text, [(r["source"], r["trust"]) for r in signer.observed]), ("body of note://inbox", [("mcp:stub/resource", "untrusted")])) + def test_an_unrecorded_resource_is_withheld_when_the_run_fails_closed(self): + """The default fail mode is closed, and a signer refusal is withheld even when the run fails open.""" + from tracekit.sdk.client import SignerUnavailable + from tracekit.signer.rpc_schema import RPCError + session = _StubSession() + with self.assertRaises(SignerUnavailable): + asyncio.run(TracekitSession(session, _Signer(observe=SignerUnavailable("down")), + self.RUN).read_resource("note://inbox")) + self.assertEqual(session.calls, 1) # fetched from the server, not returned + with self.assertRaises(RPCError): + asyncio.run(TracekitSession(_StubSession(), _Signer(observe=RPCError("unavailable", "storage down")), + self.RUN).read_resource("note://inbox")) + with self.assertRaises(RPCError): + asyncio.run(TracekitSession(_StubSession(), _Signer(observe=RPCError("unavailable", "storage down")), + {**self.RUN, "fail_modes": {"mcp": "open"}}).read_resource("note://inbox")) + + def test_an_unrecorded_resource_is_returned_when_the_run_fails_open(self): + from tracekit.sdk.client import SignerUnavailable + session = _StubSession() + with self.assertWarns(UserWarning) as cm: + gate = TracekitSession(session, _Signer(observe=SignerUnavailable("down")), + {**self.RUN, "fail_modes": {"mcp": "open"}}) + out = asyncio.run(gate.read_resource("note://inbox")) + self.assertEqual((out.contents[0].text, session.calls), ("body of note://inbox", 1)) + self.assertIn("not recorded", str(cm.warning)) + class _ListingSession(_StubSession): """List methods return one page, the one `params.cursor` names (the first when the caller asks for none).""" diff --git a/tracekit/integrations/mcp.py b/tracekit/integrations/mcp.py index 5a896fc38..c717d21b9 100644 --- a/tracekit/integrations/mcp.py +++ b/tracekit/integrations/mcp.py @@ -13,12 +13,11 @@ up to `approval_wait_s` for a person to decide in the signer; with `approval_wait_s=0` the call is refused instead. Every call that is not denied is sent only after the signer's `approval_consume` agrees, with the arguments it returns for an approval with a T2 executor. A result with `isError: true` completes as an error; an exception from the session -propagates unchanged and completes as an error. `read_resource` records the result with `observe` (`source` -`mcp:/resource`, `trust` `untrusted`); if the signer cannot record it, a warning is issued and the result is -returned unchanged. `list_tools`, `list_resources` and `list_prompts` are recorded as -untrusted observed input (`mcp:/tools`, `/resources`, `/prompts`) and returned once recorded. A list the -signer cannot reach is withheld unless the run fails open for this class, when it is returned with a warning; a -refusal is withheld either way. Only those calls made through the wrapped session are seen. +propagates unchanged and completes as an error. `read_resource`, `list_tools`, `list_resources` and `list_prompts` +are recorded with `observe` as untrusted input (`source` `mcp:/resource`, `/tools`, `/resources`, +`/prompts`) and returned once recorded. A resource or list the signer cannot reach is withheld unless the run fails +open for this class, when it is returned with a warning; a refusal is withheld either way. Only those calls made +through the wrapped session are seen. """ import re import uuid @@ -56,8 +55,8 @@ async def get_prompt(self, name, arguments=None, *a, **kw): return await self._call("get_prompt", "prompt:", name, arguments, *a, **kw) async def read_resource(self, uri, *a, **kw): - """Read a resource and record its result as an untrusted observed input; a failure to record warns and leaves - the result as it was.""" + """Read a resource and return it once recorded as untrusted input. An unreachable signer returns it only when + the run fails open for this class; a refusal is withheld either way.""" result = await self._session.read_resource(uri, *a, **kw) raw, server = self._server_name() if not raw: @@ -65,7 +64,9 @@ async def read_resource(self, uri, *a, **kw): try: await self._rpc("observe", source=f"mcp:{server}/resource", trust="untrusted", value=result.model_dump(mode="json", by_alias=True, exclude_none=True)) - except Exception as e: + except SignerUnavailable as e: # unreachable: the run's fail mode for this class, as _gate + if not fail_open(self.fail_modes, self.CLASS): + raise warnings.warn(f"tracekit: resource {uri} of mcp:{server} not recorded: {e}", stacklevel=2) return result From d1b48a0fcc9eb1c21b4a549b8b504ff8d5af64de Mon Sep 17 00:00:00 2001 From: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com> Date: Thu, 12 Nov 2026 23:02:56 +0530 Subject: [PATCH 3/3] MCP client: read_resource and the lists share one recording helper read_resource repeated _list's body (fetch, unnamed-server warning, observe, fail-mode branch). One helper, _observed, now takes the session method and the source suffix, so the fail-closed path lives in one place. A resource over half a signer frame is now recorded as its digest, as a list's page is, rather than sent whole and refused by the signer. Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com> --- docs/integrations/mcp.md | 2 +- tests/test_contract_mcp.py | 13 +++++++++++++ tracekit/integrations/mcp.py | 36 ++++++++++++------------------------ 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/docs/integrations/mcp.md b/docs/integrations/mcp.md index 9bc12ef2b..85e9e509b 100644 --- a/docs/integrations/mcp.md +++ b/docs/integrations/mcp.md @@ -38,7 +38,7 @@ Every other attribute is the wrapped session's. Only calls made through the wrap | before the call is sent | `decide` | a fresh `tool_call_id`, the tool as `mcp:/` (`mcp:/prompt:` for `get_prompt`), `tool_class_hint: mcp`, the arguments as given (`parsed`) | | right before it is sent | `approval_consume` | the same call and arguments, and the approval id when the call waited for one | | after the server answers | `complete` | `status` `ok`, or `error` for a result with `isError: true`; a `sha256:` commitment to the result (not the result), or the exception type and message | -| after `read_resource` answers | `observe` | `source` `mcp:/resource`, `trust: untrusted` and the result as `value` (recorded as its commitment and indexed for provenance; withheld if the signer cannot record it, unless the run fails open for `mcp` and the signer is unreachable, when it warns and returns the result; a refusal is withheld either way) | +| after `read_resource` answers | `observe` | `source` `mcp:/resource`, `trust: untrusted` and the result as `value` (recorded as its commitment and indexed for provenance; a result over half the signer's 1 MiB frame as its `sha256:` digest; withheld if the signer cannot record it, unless the run fails open for `mcp` and the signer is unreachable, when it warns and returns the result; a refusal is withheld either way) | `list_tools`, `list_resources` and `list_prompts` are recorded as untrusted observed input (`observe`, source `mcp:/tools`, `/resources` or `/prompts`): the model reads the descriptions the server wrote, and the page the caller asked for is what is recorded (a page over half the signer's 1 MiB frame as its `sha256:` digest, as the [MCP gateway](../mcp-gateway.md) records one). The list mcp's `call_tool` makes itself, to check a result against the tool's output schema, never reaches the model and isn't recorded. A page the signer cannot record is withheld, unless the run fails open for `mcp`, when an unreachable signer returns it with a warning; a refusal is withheld either way. A tool call or prompt that returns an `InputRequiredResult` (MCP 2.3) records each round's input requests as untrusted observed input (`observe`, source `mcp:/input_required`). When the server provides a `request_state` and the decision is `allow`, it is decided and completed once, under its initial decision, when the terminal result arrives. A round continues the call its own asyncio task left waiting, as `mcp.Client.call_tool` drives the rounds, so concurrent calls never share a decision even when the server returns the same `request_state` to both; a waiting call whose task calls a tool or gets a prompt on the session first is never completed ([not covered](#not-covered)). Rounds from a server that returns no `request_state`, or under a decision that isn't `allow` (a flag, or an ask, whose approval the signer consumes only once), are decided and completed as separate calls per round. diff --git a/tests/test_contract_mcp.py b/tests/test_contract_mcp.py index 34b361e12..bda04db37 100644 --- a/tests/test_contract_mcp.py +++ b/tests/test_contract_mcp.py @@ -312,6 +312,19 @@ def test_an_unrecorded_resource_is_returned_when_the_run_fails_open(self): self.assertEqual((out.contents[0].text, session.calls), ("body of note://inbox", 1)) self.assertIn("not recorded", str(cm.warning)) + def test_a_resource_over_half_a_frame_is_recorded_as_its_digest(self): + from mcp.types import ReadResourceResult, TextResourceContents + from tracekit.format.canon import event_hash + + class Big(_StubSession): + async def read_resource(self, uri, *a, **kw): + return ReadResourceResult(contents=[TextResourceContents(uri=uri, text="x" * (1 << 20))]) + signer = _Signer() + out = asyncio.run(TracekitSession(Big(), signer, self.RUN).read_resource("note://inbox")) + self.assertEqual(len(out.contents[0].text), 1 << 20) + self.assertEqual([r["value"] for r in signer.observed], + [event_hash(out.model_dump(mode="json", by_alias=True, exclude_none=True))]) + class _ListingSession(_StubSession): """List methods return one page, the one `params.cursor` names (the first when the caller asks for none).""" diff --git a/tracekit/integrations/mcp.py b/tracekit/integrations/mcp.py index deee28e62..d489a4ad8 100644 --- a/tracekit/integrations/mcp.py +++ b/tracekit/integrations/mcp.py @@ -20,7 +20,7 @@ flag or an ask), are decided as calls of their own. A result with `isError: true` completes as an error; an exception from the session propagates unchanged and completes as an error. `read_resource`, `list_tools`, `list_resources` and `list_prompts` are recorded as untrusted observed input -(`mcp:/resource`, `/tools`, `/resources`, `/prompts`; a list's page over half a signer frame as its digest) and +(`mcp:/resource`, `/tools`, `/resources`, `/prompts`; a result over half a signer frame as its digest) and returned once recorded. A resource or list the signer cannot reach is withheld unless the run fails open for this class, when it is returned with a warning; a refusal is withheld either way. The list mcp itself asks for inside `call_tool` (to check a result against the tool's output schema) never reaches the model and isn't recorded. Only @@ -69,49 +69,37 @@ async def get_prompt(self, name, arguments=None, *a, **kw): return await self._call("get_prompt", "prompt:", name, arguments, *a, **kw) async def read_resource(self, uri, *a, **kw): - """Read a resource and return it once recorded as untrusted input. An unreachable signer returns it only when - the run fails open for this class; a refusal is withheld either way.""" - result = await self._session.read_resource(uri, *a, **kw) - raw, server = self._server_name() - if not raw: - warnings.warn(_UNNAMED, stacklevel=3) - try: - await self._rpc("observe", source=f"mcp:{server}/resource", trust="untrusted", - value=result.model_dump(mode="json", by_alias=True, exclude_none=True)) - except SignerUnavailable as e: # unreachable: the run's fail mode for this class, as _gate - if not fail_open(self.fail_modes, self.CLASS): - raise - warnings.warn(f"tracekit: resource {uri} of mcp:{server} not recorded: {e}", stacklevel=2) - return result + return await self._observed("read_resource", "resource", uri, *a, **kw) async def list_tools(self, *a, **kw): - return await self._list("tools", *a, **kw) + return await self._observed("list_tools", "tools", *a, **kw) async def list_resources(self, *a, **kw): - return await self._list("resources", *a, **kw) + return await self._observed("list_resources", "resources", *a, **kw) async def list_prompts(self, *a, **kw): - return await self._list("prompts", *a, **kw) + return await self._observed("list_prompts", "prompts", *a, **kw) def _server_name(self): """(the name the session gave, or None; the `mcp:` component, `unknown` when it gave none).""" raw = self._server or getattr(getattr(self._session, "server_info", None), "name", None) return raw, _UNSAFE.sub("_", raw or "") or "unknown" - async def _list(self, kind, *a, **kw): - """The page `list_` returned, once recorded as untrusted input the model reads. An unreachable signer - returns it only when the run fails open for this class; a refusal is withheld either way.""" - out = await getattr(self._session, "list_" + kind)(*a, **kw) + async def _observed(self, method, kind, *a, **kw): + """What the session's `method` returned (a resource, a list's page), once recorded as untrusted input the model + reads (`mcp:/`). An unreachable signer returns it only when the run fails open for this class; a + refusal is withheld either way.""" + out = await getattr(self._session, method)(*a, **kw) raw, server = self._server_name() if not raw: - warnings.warn(_UNNAMED, stacklevel=4) # list_* -> _list -> warn: the caller's line, as _call's warning + warnings.warn(_UNNAMED, stacklevel=4) # list_* -> _observed -> warn: the caller's line, as _call's warning try: await self._rpc("observe", source=f"mcp:{server}/{kind}", trust="untrusted", value=result_field(True, out.model_dump(mode="json", by_alias=True, exclude_none=True))) except SignerUnavailable as e: # unreachable: the run's fail mode for this class, as _gate if not fail_open(self.fail_modes, self.CLASS): raise - warnings.warn(f"tracekit: MCP {kind} list not recorded: {e}", stacklevel=2) + warnings.warn(f"tracekit: MCP {kind} not recorded: {e}", stacklevel=2) return out async def _send(self, method, *a, **kw):