From a357cfe4a6c1fb7e9f181c6e8eaeb6c600706199 Mon Sep 17 00:00:00 2001 From: MTEnt <29636429+MTEnt@users.noreply.github.com> Date: Thu, 20 Aug 2026 13:48:52 +0700 Subject: [PATCH 1/2] fix: fail closed for unannotated MCP tools --- .changeset/code-mode-destructive-defaults.md | 6 ++ .../src/core/sandbox/scripts/mcp_client.py | 12 ++- .../sandbox/scripts/mcpClientPolicy.test.ts | 89 +++++++++++++++++++ .../sandbox/local/scripts/mcp_client_local.py | 16 ++-- .../local/scripts/mcpClientPolicy.test.ts | 61 +++++++++++++ 5 files changed, 178 insertions(+), 6 deletions(-) create mode 100644 .changeset/code-mode-destructive-defaults.md create mode 100644 packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts create mode 100644 packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts diff --git a/.changeset/code-mode-destructive-defaults.md b/.changeset/code-mode-destructive-defaults.md new file mode 100644 index 0000000000..0af534632d --- /dev/null +++ b/.changeset/code-mode-destructive-defaults.md @@ -0,0 +1,6 @@ +--- +'@truefoundry/trueforge-core': patch +'@truefoundry/trueforge': patch +--- + +Fail closed when Code Mode receives MCP tools with missing or incomplete annotations, and accept both MCP Python SDK hint casings. diff --git a/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py b/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py index b942acde5d..9890ae66cb 100644 --- a/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py +++ b/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py @@ -224,8 +224,18 @@ async def _get_tool(server: str, tool_name: str) -> Tool | None: def _is_destructive(tool: Tool) -> bool: annotations = tool.annotations if annotations is None: + return True + + read_only = getattr(annotations, "read_only_hint", None) + if read_only is None: + read_only = getattr(annotations, "readOnlyHint", None) + if read_only is True: return False - return bool(annotations.destructiveHint) or (not annotations.readOnlyHint and annotations.readOnlyHint is not None) + + destructive = getattr(annotations, "destructive_hint", None) + if destructive is None: + destructive = getattr(annotations, "destructiveHint", None) + return destructive is not False async def _ensure_non_destructive(server: str, tool_name: str) -> None: tool = await _get_tool(server, tool_name) diff --git a/packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts b/packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts new file mode 100644 index 0000000000..12e929735e --- /dev/null +++ b/packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts @@ -0,0 +1,89 @@ +import { spawnSync } from 'node:child_process'; +import { resolve } from 'node:path'; + +const policyCheck = String.raw` +import runpy +import sys +import types + +pydantic = types.ModuleType("pydantic") + +class BaseModel: + pass + +class ValidationError(Exception): + pass + +pydantic.BaseModel = BaseModel +pydantic.ValidationError = ValidationError + +mcp = types.ModuleType("mcp") +mcp_types = types.ModuleType("mcp.types") + +class StubTool: + def __init__(self, annotations=None): + self.annotations = annotations + +mcp_types.CallToolResult = object +mcp_types.TextContent = object +mcp_types.Tool = StubTool +mcp.types = mcp_types + +sys.modules["pydantic"] = pydantic +sys.modules["mcp"] = mcp +sys.modules["mcp.types"] = mcp_types + +module = runpy.run_path(sys.argv[1], run_name="mcp_client_policy_test") +is_destructive = module["_is_destructive"] + +cases = [ + ("missing annotations", None, True), + ("empty annotations", types.SimpleNamespace(), True), + ("camel read-only", types.SimpleNamespace(readOnlyHint=True), False), + ("snake read-only", types.SimpleNamespace(read_only_hint=True), False), + ( + "camel additive write", + types.SimpleNamespace(readOnlyHint=False, destructiveHint=False), + False, + ), + ( + "snake additive write", + types.SimpleNamespace(read_only_hint=False, destructive_hint=False), + False, + ), + ("camel unspecified write", types.SimpleNamespace(readOnlyHint=False), True), + ("snake unspecified write", types.SimpleNamespace(read_only_hint=False), True), + ("explicit destructive", types.SimpleNamespace(destructiveHint=True), True), +] + +for label, annotations, expected in cases: + actual = is_destructive(StubTool(annotations)) + if actual is not expected: + raise AssertionError(f"{label}: expected {expected}, got {actual}") +`; + +describe('Code Mode MCP destructive-tool policy', () => { + it('fails closed and accepts MCP Python SDK hint casing', () => { + const python = process.platform === 'win32' ? 'python' : 'python3'; + const scriptPath = resolve(__dirname, '../../../../src/core/sandbox/scripts/mcp_client.py'); + const result = spawnSync(python, ['-c', policyCheck, scriptPath], { + encoding: 'utf8', + env: { + ...process.env, + TFY_MCP_SERVERS: '', + TFY_NATS_SUBJECT_PREFIX: '', + TFY_NATS_URL: '', + }, + }); + + expect({ + error: result.error?.message, + status: result.status, + stderr: result.stderr, + }).toEqual({ + error: undefined, + status: 0, + stderr: '', + }); + }); +}); diff --git a/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py b/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py index 0a1eb1af48..abce6661a9 100644 --- a/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py +++ b/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py @@ -177,13 +177,19 @@ async def _get_tool(server: str, tool_name: str) -> dict[str, Any] | None: def _is_destructive(tool: dict[str, Any]) -> bool: annotations = tool.get("annotations") - if annotations is None: - return False if not isinstance(annotations, dict): + return True + + read_only = annotations.get("read_only_hint") + if read_only is None: + read_only = annotations.get("readOnlyHint") + if read_only is True: return False - destructive = annotations.get("destructiveHint") - read_only = annotations.get("readOnlyHint") - return bool(destructive) or (not read_only and read_only is not None) + + destructive = annotations.get("destructive_hint") + if destructive is None: + destructive = annotations.get("destructiveHint") + return destructive is not False async def _ensure_non_destructive(server: str, tool_name: str) -> None: diff --git a/packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts b/packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts new file mode 100644 index 0000000000..d73c8b4b27 --- /dev/null +++ b/packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts @@ -0,0 +1,61 @@ +import { spawnSync } from 'node:child_process'; +import { resolve } from 'node:path'; + +const policyCheck = String.raw` +import runpy +import sys + +module = runpy.run_path(sys.argv[1], run_name="mcp_client_local_policy_test") +is_destructive = module["_is_destructive"] + +cases = [ + ("missing annotations", {}, True), + ("null annotations", {"annotations": None}, True), + ("malformed annotations", {"annotations": []}, True), + ("empty annotations", {"annotations": {}}, True), + ("camel read-only", {"annotations": {"readOnlyHint": True}}, False), + ("snake read-only", {"annotations": {"read_only_hint": True}}, False), + ( + "camel additive write", + {"annotations": {"readOnlyHint": False, "destructiveHint": False}}, + False, + ), + ( + "snake additive write", + {"annotations": {"read_only_hint": False, "destructive_hint": False}}, + False, + ), + ("camel unspecified write", {"annotations": {"readOnlyHint": False}}, True), + ("snake unspecified write", {"annotations": {"read_only_hint": False}}, True), + ("explicit destructive", {"annotations": {"destructiveHint": True}}, True), +] + +for label, tool, expected in cases: + actual = is_destructive(tool) + if actual is not expected: + raise AssertionError(f"{label}: expected {expected}, got {actual}") +`; + +describe('local Code Mode MCP destructive-tool policy', () => { + it('fails closed and accepts both hint casings', () => { + const python = process.platform === 'win32' ? 'python' : 'python3'; + const scriptPath = resolve(__dirname, '../../../../../src/sandbox/local/scripts/mcp_client_local.py'); + const result = spawnSync(python, ['-c', policyCheck, scriptPath], { + encoding: 'utf8', + env: { + ...process.env, + TFY_MCP_SERVERS: '', + }, + }); + + expect({ + error: result.error?.message, + status: result.status, + stderr: result.stderr, + }).toEqual({ + error: undefined, + status: 0, + stderr: '', + }); + }); +}); From 32f9c864684c861fea7dea1de16ed751838e0f81 Mon Sep 17 00:00:00 2001 From: MTEnt <29636429+MTEnt@users.noreply.github.com> Date: Wed, 26 Aug 2026 19:30:58 +0700 Subject: [PATCH 2/2] fix: preserve MCP tool annotation behavior --- .changeset/code-mode-destructive-defaults.md | 2 +- .../src/core/sandbox/scripts/mcp_client.py | 17 ++-- .../sandbox/scripts/mcpClientPolicy.test.ts | 89 ------------------- .../sandbox/local/scripts/mcp_client_local.py | 15 ++-- .../local/scripts/mcpClientPolicy.test.ts | 61 ------------- 5 files changed, 13 insertions(+), 171 deletions(-) delete mode 100644 packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts delete mode 100644 packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts diff --git a/.changeset/code-mode-destructive-defaults.md b/.changeset/code-mode-destructive-defaults.md index 0af534632d..5046e8a885 100644 --- a/.changeset/code-mode-destructive-defaults.md +++ b/.changeset/code-mode-destructive-defaults.md @@ -3,4 +3,4 @@ '@truefoundry/trueforge': patch --- -Fail closed when Code Mode receives MCP tools with missing or incomplete annotations, and accept both MCP Python SDK hint casings. +Prefer MCP Python SDK 2.0 snake_case tool annotation fields, with camelCase fallback for older SDKs, without changing destructive-tool detection behavior. diff --git a/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py b/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py index 9890ae66cb..35dc8edf84 100644 --- a/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py +++ b/packages/trueforge-core/src/core/sandbox/scripts/mcp_client.py @@ -224,18 +224,15 @@ async def _get_tool(server: str, tool_name: str) -> Tool | None: def _is_destructive(tool: Tool) -> bool: annotations = tool.annotations if annotations is None: - return True - - read_only = getattr(annotations, "read_only_hint", None) - if read_only is None: - read_only = getattr(annotations, "readOnlyHint", None) - if read_only is True: return False - destructive = getattr(annotations, "destructive_hint", None) - if destructive is None: - destructive = getattr(annotations, "destructiveHint", None) - return destructive is not False + destructive = getattr( + annotations, "destructive_hint", getattr(annotations, "destructiveHint", None) + ) + read_only = getattr( + annotations, "read_only_hint", getattr(annotations, "readOnlyHint", None) + ) + return bool(destructive) or (not read_only and read_only is not None) async def _ensure_non_destructive(server: str, tool_name: str) -> None: tool = await _get_tool(server, tool_name) diff --git a/packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts b/packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts deleted file mode 100644 index 12e929735e..0000000000 --- a/packages/trueforge-core/tests/core/sandbox/scripts/mcpClientPolicy.test.ts +++ /dev/null @@ -1,89 +0,0 @@ -import { spawnSync } from 'node:child_process'; -import { resolve } from 'node:path'; - -const policyCheck = String.raw` -import runpy -import sys -import types - -pydantic = types.ModuleType("pydantic") - -class BaseModel: - pass - -class ValidationError(Exception): - pass - -pydantic.BaseModel = BaseModel -pydantic.ValidationError = ValidationError - -mcp = types.ModuleType("mcp") -mcp_types = types.ModuleType("mcp.types") - -class StubTool: - def __init__(self, annotations=None): - self.annotations = annotations - -mcp_types.CallToolResult = object -mcp_types.TextContent = object -mcp_types.Tool = StubTool -mcp.types = mcp_types - -sys.modules["pydantic"] = pydantic -sys.modules["mcp"] = mcp -sys.modules["mcp.types"] = mcp_types - -module = runpy.run_path(sys.argv[1], run_name="mcp_client_policy_test") -is_destructive = module["_is_destructive"] - -cases = [ - ("missing annotations", None, True), - ("empty annotations", types.SimpleNamespace(), True), - ("camel read-only", types.SimpleNamespace(readOnlyHint=True), False), - ("snake read-only", types.SimpleNamespace(read_only_hint=True), False), - ( - "camel additive write", - types.SimpleNamespace(readOnlyHint=False, destructiveHint=False), - False, - ), - ( - "snake additive write", - types.SimpleNamespace(read_only_hint=False, destructive_hint=False), - False, - ), - ("camel unspecified write", types.SimpleNamespace(readOnlyHint=False), True), - ("snake unspecified write", types.SimpleNamespace(read_only_hint=False), True), - ("explicit destructive", types.SimpleNamespace(destructiveHint=True), True), -] - -for label, annotations, expected in cases: - actual = is_destructive(StubTool(annotations)) - if actual is not expected: - raise AssertionError(f"{label}: expected {expected}, got {actual}") -`; - -describe('Code Mode MCP destructive-tool policy', () => { - it('fails closed and accepts MCP Python SDK hint casing', () => { - const python = process.platform === 'win32' ? 'python' : 'python3'; - const scriptPath = resolve(__dirname, '../../../../src/core/sandbox/scripts/mcp_client.py'); - const result = spawnSync(python, ['-c', policyCheck, scriptPath], { - encoding: 'utf8', - env: { - ...process.env, - TFY_MCP_SERVERS: '', - TFY_NATS_SUBJECT_PREFIX: '', - TFY_NATS_URL: '', - }, - }); - - expect({ - error: result.error?.message, - status: result.status, - stderr: result.stderr, - }).toEqual({ - error: undefined, - status: 0, - stderr: '', - }); - }); -}); diff --git a/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py b/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py index abce6661a9..9de816a544 100644 --- a/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py +++ b/packages/trueforge/src/sandbox/local/scripts/mcp_client_local.py @@ -177,19 +177,14 @@ async def _get_tool(server: str, tool_name: str) -> dict[str, Any] | None: def _is_destructive(tool: dict[str, Any]) -> bool: annotations = tool.get("annotations") + if annotations is None: + return False if not isinstance(annotations, dict): - return True - - read_only = annotations.get("read_only_hint") - if read_only is None: - read_only = annotations.get("readOnlyHint") - if read_only is True: return False - destructive = annotations.get("destructive_hint") - if destructive is None: - destructive = annotations.get("destructiveHint") - return destructive is not False + read_only = annotations.get("read_only_hint", annotations.get("readOnlyHint")) + destructive = annotations.get("destructive_hint", annotations.get("destructiveHint")) + return bool(destructive) or (not read_only and read_only is not None) async def _ensure_non_destructive(server: str, tool_name: str) -> None: diff --git a/packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts b/packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts deleted file mode 100644 index d73c8b4b27..0000000000 --- a/packages/trueforge/tests/unit/sandbox/local/scripts/mcpClientPolicy.test.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { spawnSync } from 'node:child_process'; -import { resolve } from 'node:path'; - -const policyCheck = String.raw` -import runpy -import sys - -module = runpy.run_path(sys.argv[1], run_name="mcp_client_local_policy_test") -is_destructive = module["_is_destructive"] - -cases = [ - ("missing annotations", {}, True), - ("null annotations", {"annotations": None}, True), - ("malformed annotations", {"annotations": []}, True), - ("empty annotations", {"annotations": {}}, True), - ("camel read-only", {"annotations": {"readOnlyHint": True}}, False), - ("snake read-only", {"annotations": {"read_only_hint": True}}, False), - ( - "camel additive write", - {"annotations": {"readOnlyHint": False, "destructiveHint": False}}, - False, - ), - ( - "snake additive write", - {"annotations": {"read_only_hint": False, "destructive_hint": False}}, - False, - ), - ("camel unspecified write", {"annotations": {"readOnlyHint": False}}, True), - ("snake unspecified write", {"annotations": {"read_only_hint": False}}, True), - ("explicit destructive", {"annotations": {"destructiveHint": True}}, True), -] - -for label, tool, expected in cases: - actual = is_destructive(tool) - if actual is not expected: - raise AssertionError(f"{label}: expected {expected}, got {actual}") -`; - -describe('local Code Mode MCP destructive-tool policy', () => { - it('fails closed and accepts both hint casings', () => { - const python = process.platform === 'win32' ? 'python' : 'python3'; - const scriptPath = resolve(__dirname, '../../../../../src/sandbox/local/scripts/mcp_client_local.py'); - const result = spawnSync(python, ['-c', policyCheck, scriptPath], { - encoding: 'utf8', - env: { - ...process.env, - TFY_MCP_SERVERS: '', - }, - }); - - expect({ - error: result.error?.message, - status: result.status, - stderr: result.stderr, - }).toEqual({ - error: undefined, - status: 0, - stderr: '', - }); - }); -});