Skip to content

MCP client: record what read_resource returns on a wrapped session as an observed input (#667) - #689

Closed
Mr45401 wants to merge 4 commits into
Cygnux-Labs:mainfrom
Mr45401:fix/667-mcp-observe-read-resource
Closed

Mr45401 wants to merge 4 commits into
Cygnux-Labs:mainfrom
Mr45401:fix/667-mcp-observe-read-resource

Conversation

@Mr45401

@Mr45401 Mr45401 commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Closes #667

Summary

  • Add async def read_resource(self, uri, *a, **kw) to TracekitSession in tracekit/integrations/mcp.py so resource reads on a wrapped ClientSession are recorded via the observe RPC (source="mcp:<server>/resource", trust="untrusted", value=result.model_dump(mode="json", by_alias=True, exclude_none=True)) before returning.
  • Fail closed on unrecorded resources: on SignerUnavailable, return the resource with a warning only when fail_open(self.fail_modes, self.CLASS) is true, and otherwise raise; RPCError always propagates.
  • Include "read_resource" in autowire._mcp's patched method tuple in tracekit/autowire.py so tracekit.instrument() covers read_resource alongside call_tool, get_prompt, and list_*.
  • Add unit tests in tests/test_contract_mcp.py covering observe payload recording, autowire._mcp patching, fail-closed withholding (SignerUnavailable by default and RPCError even under fail_modes={"mcp": "open"}), and fail-open warning + return (SignerUnavailable under fail_modes={"mcp": "open"}), plus ClientSession.read_resource cleanup in tests/test_instrument.py, tests/test_agent_framework.py, and tests/test_pydantic_ai.py.
  • Document read_resource recording and fail-closed/fail-open semantics in docs/integrations/mcp.md.

Verification

  • pytest tests/test_contract_mcp.py tests/test_instrument.py tests/test_docs.py tests/test_readme.py tests/test_agent_framework.py tests/test_pydantic_ai.py tests/test_mcp_gateway.py -v: 93 passed, 89 skipped, 13 subtests passed
  • python -m pyflakes tracekit tracekit_sdk.py tests eval examples contrib integrations: 0 warnings/errors

Note: AI assistance was used for code navigation and test verification.

… an observed input (Cygnux-Labs#667)

Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com>

@LoopGlitch26 LoopGlitch26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Two things:

  1. A resource that can't be recorded should be withheld, not returned. The MCP gateway fails closed here (_observe in tracekit/mcp_gateway.py, L238-249), and #685 just made the same change for the list methods: on SignerUnavailable, return the result only when fail_open(self.fail_modes, self.CLASS) says so, and otherwise raise, as HeldCalls._gate does (tracekit/integrations/held.py L47-48); an RPCError raises either way. The test that expects a warning and the result then becomes two: fail-closed raises, fail-open returns with the warning.

  2. #685 changes the same autowire code. It replaces _Unpatched's per-method wrappers with one table of the methods instrument() patches. Whichever merges first, the other rebases. If #685 lands first, read_resource becomes one more name in its tuple in _mcp.

@LoopGlitch26

Copy link
Copy Markdown
Member

#692 is merged, so main's lock file now names the 1.6.0 signer packages. Please merge main into this branch (or rebase); the two red checks, release-dry-run and the offline evals, should then pass. I'll approve the CI runs after your push if they need it.

Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com>
Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com>
@Mr45401

Mr45401 commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @LoopGlitch26. Addressed in cb55d554:

  1. Merged main (including After 1.6.0: lock the published signer packages; CI runs the 1.6.0 verifier; the 1.6.0 format row is frozen #692 and MCP client: observe list_tools, list_resources, and list_prompts #685) and placed "read_resource" in _mcp's method tuple in tracekit/autowire.py.
  2. Withhold unrecorded resources by default: TracekitSession.read_resource now catches SignerUnavailable and returns the resource with a warning only when fail_open(self.fail_modes, self.CLASS) is true (otherwise raising), while RPCError always raises regardless of fail_modes. Updated the module docstring and docs/integrations/mcp.md to match.
  3. Tests: Split the failure coverage in tests/test_contract_mcp.py into test_an_unrecorded_resource_is_withheld_when_the_run_fails_closed (default SignerUnavailable and RPCError under both default and fail_modes={"mcp": "open"}) and test_an_unrecorded_resource_is_returned_when_the_run_fails_open (SignerUnavailable under fail_modes={"mcp": "open"}).

@LoopGlitch26 LoopGlitch26 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both points are in at cb55d55: closed raises, open returns the resource with a warning, a refusal raises either way, and read_resource is one more name in _mcp's tuple. CI is green.

One thing before it goes to the owner: read_resource repeats _list's body line for line (tracekit/integrations/mcp.py L70-84 on main): fetch, the unnamed-server warning, observe, the fail-mode branch. Let one helper take the method and the source suffix, so read_resource and the three lists are one line each and the fail-closed path lives in one place. The tests you added stay as they are.

@LoopGlitch26

Copy link
Copy Markdown
Member

I made the helper change myself and carried this in #776, since a push to your branch was refused. Your four commits are in it unchanged, with your authorship and sign-offs. On top of them:

  1. A merge of main (Integrations: MCP big lists and input-required rounds, LangChain parallel asks, tracekit last, gateway listing errors #753 changed the same docstring and tests). main's two new MCP test classes restore the methods instrument() patches, so read_resource is in their lists too.
  2. _observed(method, kind) holds the fetch, the warning, observe and the fail-mode branch, and read_resource and the three lists are one line each. A resource over half a signer frame is now recorded as its digest, as Integrations: MCP big lists and input-required rounds, LangChain parallel asks, tracekit last, gateway listing errors #753 does for a list's page; a new test covers it.

Your tests are unchanged. This PR closes when #776 merges.

LoopGlitch26 added a commit that referenced this pull request Nov 13, 2026
…ies #689, closes #667) (#776)

* 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>

* MCP client: withhold an unrecorded resource unless the run fails open

Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com>

* 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>

---------

Signed-off-by: Mr45401 <185895283+Mr45401@users.noreply.github.com>
Signed-off-by: LoopGlitch26 <53336715+LoopGlitch26@users.noreply.github.com>
Co-authored-by: Mr45401 <185895283+Mr45401@users.noreply.github.com>
@LoopGlitch26

Copy link
Copy Markdown
Member

Merged in #776 with your commits. The MCP client now records what read_resource returns as an observed input, through the same helper as the list methods. It ships in the next release. Thank you.

If you'd like another one: #724 is a real bug. Browser Use 1.0 removed the seam the integration hooked in 0.13, so on 1.0 Tracekit gates none of its tool calls.

And a follow-up to the request on #675: if you've been running Tracekit on your coding agent, tracekit report --share prints counts only (no commands, paths or content). Posting it with the field-report form (https://github.com/Cygnux-Labs/Tracekit/issues/new?template=field-report.yml) helps us more than anything else. If you haven't tried it yet, pip install -U tracekit-ai then tracekit audit --since 7d shows, read-only, what it would have held last week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP client: record what read_resource returns on a wrapped session as an observed input

2 participants