Аркадий Стена e0ba324d0f
feat: add TVM exact-payment mechanism to Python SDK (#1944)
* feat: add TVM exact-payment mechanism to Python SDK

- add TVM exact client, server, and facilitator implementations
- add TVM signers, codecs, provider, and settlement helpers
- add unit and integration coverage for TON testnet/mainnet flows
- add Python examples and e2e wiring for TVM

* Enable TVM exact flows in Python e2e and tighten facilitator validation

Add TVM support to the Python e2e clients and servers, switch TVM usage to direct `ExactTvm*Scheme` registration after removing the old register helpers, and refresh docs/examples for testnet setup.
Also reject exact TVM settlements with excessively large attached TON amounts and cover the new validation with tests.

* fix: clarify minimum balance of the facilitator

* refactor(tvm): release settlement cache entry on confirmed settlement

  On-chain seqno advances after a successful relay, so any retry of the same settlement BOC will fail at the W5 contract's seqno check regardless of the dedup reservation. Drop the entry as soon as the confirmation worker records success, so the invariant becomes "every is_duplicate=False is paired with a release" and TTL pruning is left as a crash safety-net only.

* feat(tvm): add TonAPI provider support

* feat(tvm): update e2e tests  to make them compatible with new providers

* fix(tvm): update timeouts

* fix(tvm): increase http client timeout

* fix(tvm): align unit tests

* revert(e2e): remove .env-local config load

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-08 16:13:22 +02:00

124 lines
4.2 KiB
Python

"""MCP E2E Test Client with x402 Payment Support.
One-shot client that connects to an MCP server via SSE, calls a paid tool,
and outputs a structured JSON result for the e2e test framework to parse.
Uses the x402 SDK's MCP client wrapper for automatic payment handling.
"""
import asyncio
import json
import os
import sys
from dotenv import load_dotenv
load_dotenv()
# Get environment variables
server_url = os.getenv("RESOURCE_SERVER_URL", "")
endpoint_path = os.getenv("ENDPOINT_PATH", "") # tool name, e.g. "get_weather"
evm_private_key = os.getenv("EVM_PRIVATE_KEY", "")
tvm_private_key = os.getenv("TVM_PRIVATE_KEY", "")
if not server_url or not endpoint_path or not (evm_private_key or tvm_private_key):
result = {
"success": False,
"error": (
"Missing required environment variables: RESOURCE_SERVER_URL, ENDPOINT_PATH, "
"and one of EVM_PRIVATE_KEY or TVM_PRIVATE_KEY"
),
}
print(json.dumps(result))
sys.exit(1)
async def main() -> dict:
"""Run the MCP client and call the paid tool. Returns the e2e result dict."""
from eth_account import Account
from x402 import x402Client
from x402.mcp import create_x402_mcp_client
from x402.mechanisms.evm.exact import register_exact_evm_client
from x402.mechanisms.evm.signers import EthAccountSigner
from x402.mechanisms.tvm import (
TVM_MAINNET,
TVM_PROVIDER_TONAPI,
TVM_TESTNET,
WalletV5R1Config,
WalletV5R1MnemonicSigner,
)
from x402.mechanisms.tvm.exact import ExactTvmClientScheme
# Create x402 client with the configured payment schemes
client = x402Client()
if evm_private_key:
account = Account.from_key(evm_private_key)
evm_signer = EthAccountSigner(account)
register_exact_evm_client(client, evm_signer)
if tvm_private_key:
tvm_network = os.getenv("TVM_NETWORK", TVM_TESTNET)
if tvm_network not in {TVM_TESTNET, TVM_MAINNET}:
raise ValueError(f"Unsupported TVM network: {tvm_network}")
tvm_config = WalletV5R1Config.from_private_key(tvm_network, tvm_private_key)
tvm_provider = (os.getenv("TVM_PROVIDER") or "").strip().lower()
tvm_config.provider = tvm_provider or tvm_config.provider
tvm_config.api_key = (
os.getenv("TONAPI_API_KEY")
if tvm_provider == TVM_PROVIDER_TONAPI
else os.getenv("TONCENTER_API_KEY")
)
tvm_config.provider_base_url = (
os.getenv("TONAPI_BASE_URL")
if tvm_provider == TVM_PROVIDER_TONAPI
else os.getenv("TONCENTER_BASE_URL")
)
client.register(
tvm_network,
ExactTvmClientScheme(WalletV5R1MnemonicSigner(tvm_config)),
)
try:
async with create_x402_mcp_client(client, server_url, auto_payment=True) as mcp:
# Call the paid tool - payment is handled automatically
result = await mcp.call_tool(endpoint_path, {"city": "San Francisco"})
# Extract data from content
data = None
for item in result.content:
if hasattr(item, "text"):
try:
data = json.loads(item.text)
except (json.JSONDecodeError, TypeError):
data = {"text": item.text}
break
# Build payment response dict
payment_response = None
if result.payment_response:
pr = result.payment_response
if hasattr(pr, "model_dump"):
payment_response = pr.model_dump(by_alias=True)
elif isinstance(pr, dict):
payment_response = pr
return {
"success": not result.is_error,
"data": data,
"status_code": 200,
"payment_response": payment_response,
}
except Exception as e:
return {
"success": False,
"error": str(e),
"status_code": 500,
}
if __name__ == "__main__":
e2e_result = asyncio.run(main())
print(json.dumps(e2e_result))
sys.exit(0 if e2e_result.get("success") else 1)