diff --git a/gateway/TROUBLESHOOTING.md b/gateway/TROUBLESHOOTING.md index 49d653099..4271de27c 100644 --- a/gateway/TROUBLESHOOTING.md +++ b/gateway/TROUBLESHOOTING.md @@ -99,7 +99,7 @@ You reused an `Idempotency-Key` with different payload. Use a fresh UUID for eac - `idempotency_key_required` (`-32003`; REST `400`): governed calls need an `idempotency_key` in `mcpContext`. - `Tool not found: ` (`-32001`; REST `404`): confirm the tool name exists in `/mcp/tools.json`. - `Missing wallet_id in mcpContext` (`-32602`): A wallet bound key may omit `wallet_id`; a key without a bound wallet must supply it in `params.mcpContext`. -- REST `422 Unprocessable Entity`: the body failed schema validation (for example `name` is missing). The REST body spells the context `mcp_context`, not `mcpContext`. +- REST `422 Unprocessable Entity`: the body failed schema validation (for example `name` is missing). The REST body accepts `mcp_context` or `mcpContext` for the billing context. Supplying both fields returns `422`, even if their values match or are null. ### `delivery_uncertain` receipt The gateway claimed a send but has no trustworthy terminal result. Delivery and the downstream effect may or may not have occurred; this receipt does not prove upstream acceptance or execution. Under the configured conservative policy, the charge stands and replaying the same key returns the uncertain outcome without redispatching. Do not retry automatically. Reconcile against authoritative downstream state before making a new attempt. See [docs/partner-first-tool-runbook.md](docs/partner-first-tool-runbook.md). diff --git a/gateway/app/routers/mcp.py b/gateway/app/routers/mcp.py index a12a21e44..16c1d24b4 100644 --- a/gateway/app/routers/mcp.py +++ b/gateway/app/routers/mcp.py @@ -30,7 +30,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request, Query, Response from fastapi.responses import JSONResponse -from pydantic import BaseModel, Field +from pydantic import AliasChoices, BaseModel, Field, model_validator from sqlalchemy.exc import IntegrityError from ..audit.lightweight import record_audit @@ -644,7 +644,18 @@ class ToolCallRequest(BaseModel): arguments: dict[str, Any] = Field( default_factory=dict, description="Tool arguments" ) - mcp_context: McpContext | None = Field(None, description="Billing context") + mcp_context: McpContext | None = Field( + None, + validation_alias=AliasChoices("mcp_context", "mcpContext"), + description="Billing context", + ) + + @model_validator(mode="before") + @classmethod + def reject_duplicate_context(cls, data: Any) -> Any: + if isinstance(data, dict) and "mcp_context" in data and "mcpContext" in data: + raise ValueError("mcp_context and mcpContext cannot both be supplied") + return data class ToolCallResponse(BaseModel): diff --git a/gateway/tests/test_mcp_idempotency_key_validation.py b/gateway/tests/test_mcp_idempotency_key_validation.py index 0c0868258..15da17ee7 100644 --- a/gateway/tests/test_mcp_idempotency_key_validation.py +++ b/gateway/tests/test_mcp_idempotency_key_validation.py @@ -692,8 +692,9 @@ async def test_legacy_context_null_key_reads_as_absent( # --------------------------------------------------------------------------- # +@pytest.mark.parametrize("context_key", ["mcp_context", "mcpContext"]) async def test_rest_invoke_refuses_blank_or_conflicting_keys_before_any_effect( - client, governed_legacy, counted_tool + client, governed_legacy, counted_tool, context_key ): provisioned, permit = governed_legacy before = await _snapshot(client, provisioned, counted_tool) @@ -707,7 +708,7 @@ async def test_rest_invoke_refuses_blank_or_conflicting_keys_before_any_effect( json={ "name": counted_tool.name, "arguments": {"text": "one"}, - "mcp_context": context, + context_key: context, }, headers=provisioned["agent_headers"], ) @@ -722,7 +723,7 @@ async def test_rest_invoke_refuses_blank_or_conflicting_keys_before_any_effect( json={ "name": counted_tool.name, "arguments": {"text": "one"}, - "mcp_context": context, + context_key: context, }, headers={ **provisioned["agent_headers"], @@ -752,7 +753,7 @@ async def test_rest_invoke_refuses_blank_or_conflicting_keys_before_any_effect( json={ "name": counted_tool.name, "arguments": {"text": "one"}, - "mcp_context": context, + context_key: context, }, headers=headers, ) @@ -762,7 +763,7 @@ async def test_rest_invoke_refuses_blank_or_conflicting_keys_before_any_effect( json={ "name": counted_tool.name, "arguments": {"text": "one"}, - "mcp_context": context, + context_key: context, }, headers=headers, ) @@ -772,14 +773,15 @@ async def test_rest_invoke_refuses_blank_or_conflicting_keys_before_any_effect( assert counted_tool.effects == ["one"] +@pytest.mark.parametrize("context_key", ["mcp_context", "mcpContext"]) async def test_rest_invoke_changed_excluded_argument_conflicts( - client, governed_legacy, counted_tool + client, governed_legacy, counted_tool, context_key ): """Dropping a field from the key does not drop it from the gateway request.""" provisioned, permit = governed_legacy key = request_key(counted_tool.name, {"text": "one"}, exclude=["text"]) assert key == request_key(counted_tool.name, {"text": "two"}, exclude=["text"]) - headers = {**provisioned["agent_headers"], "Idempotency-Key": key} + headers = provisioned["agent_headers"] context = { "wallet_id": provisioned["agent_wallet_id"], "permit_id": permit["permit_id"], @@ -788,12 +790,30 @@ async def test_rest_invoke_changed_excluded_argument_conflicts( body = { "name": counted_tool.name, "arguments": {"text": "one"}, - "mcp_context": context, + context_key: context, } first = await client.post( f"/mcp/tools/{counted_tool.name}/invoke", json=body, headers=headers ) assert first.status_code == 200, first.text + receipt = first.json()["receipt"] + assert receipt["wallet_id"] == context["wallet_id"] + assert receipt["permit_id"] == context["permit_id"] + after_first = await _snapshot(client, provisioned, counted_tool) + + # Both spellings normalize to the same request for replay hashing. + other_key = "mcpContext" if context_key == "mcp_context" else "mcp_context" + replay = await client.post( + f"/mcp/tools/{counted_tool.name}/invoke", + json={ + "name": counted_tool.name, + "arguments": {"text": "one"}, + other_key: context, + }, + headers=headers, + ) + assert replay.status_code == 200, replay.text + assert replay.json()["receipt"]["receipt_id"] == receipt["receipt_id"] changed = await client.post( f"/mcp/tools/{counted_tool.name}/invoke", @@ -804,3 +824,40 @@ async def test_rest_invoke_changed_excluded_argument_conflicts( assert changed.json()["detail"] == {"error": "idempotency_key_reused"} assert changed.headers["X-Request-ID"] assert counted_tool.effects == ["one"] + assert await _snapshot(client, provisioned, counted_tool) == after_first + + +@pytest.mark.parametrize( + "context_case", ["same", "different", "snake-null", "camel-null", "both-null"] +) +async def test_rest_invoke_rejects_both_context_spellings_before_any_effect( + client, governed_legacy, counted_tool, context_case +): + provisioned, permit = governed_legacy + before = await _snapshot(client, provisioned, counted_tool) + context = { + "wallet_id": provisioned["agent_wallet_id"], + "permit_id": permit["permit_id"], + "idempotency_key": "rest-context-alias", + } + body = { + "name": counted_tool.name, + "arguments": {"text": "one"}, + "mcp_context": context, + "mcpContext": {**context}, + } + if context_case == "different": + body["mcpContext"]["idempotency_key"] = "rest-context-alias-other" + if context_case in {"snake-null", "both-null"}: + body["mcp_context"] = None + if context_case in {"camel-null", "both-null"}: + body["mcpContext"] = None + + response = await client.post( + f"/mcp/tools/{counted_tool.name}/invoke", + json=body, + headers=provisioned["agent_headers"], + ) + assert response.status_code == 422, response.text + assert "mcp_context and mcpContext cannot both be supplied" in response.text + assert await _snapshot(client, provisioned, counted_tool) == before diff --git a/gateway/tests/test_mcp_trust.py b/gateway/tests/test_mcp_trust.py index 3ef8e6adb..156443979 100644 --- a/gateway/tests/test_mcp_trust.py +++ b/gateway/tests/test_mcp_trust.py @@ -127,7 +127,7 @@ def trust_echo(message: str = "ok") -> dict: @pytest.mark.anyio -@pytest.mark.parametrize("transport", ["jsonrpc", "http"]) +@pytest.mark.parametrize("transport", ["jsonrpc", "http", "http_camel"]) @pytest.mark.parametrize( "wallet_case", ["absent", "null", "empty", "matching", "foreign", "invalid"] ) @@ -183,9 +183,10 @@ async def test_legacy_mcp_wallet_defaults_only_when_omitted( headers=provisioned["agent_headers"], ) else: + context_key = "mcpContext" if transport == "http_camel" else "mcp_context" response = await client.post( f"/mcp/tools/{tool_name}/invoke", - json={"name": tool_name, "arguments": {}, "mcp_context": context}, + json={"name": tool_name, "arguments": {}, context_key: context}, headers=provisioned["agent_headers"], )