@t402/mcp 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1410,6 +1410,496 @@ function formatAutoPayResult(result) {
1410
1410
  return lines.join("\n");
1411
1411
  }
1412
1412
 
1413
+ // src/tools/ton-bridge.ts
1414
+ var TON_BRIDGE_TOOLS = {
1415
+ "ton/getBalance": {
1416
+ name: "ton/getBalance",
1417
+ description: "Get TON and Jetton balances for a wallet address on the TON blockchain.",
1418
+ inputSchema: {
1419
+ type: "object",
1420
+ properties: {
1421
+ address: {
1422
+ type: "string",
1423
+ description: "TON wallet address (friendly or raw format)"
1424
+ }
1425
+ },
1426
+ required: ["address"]
1427
+ }
1428
+ },
1429
+ "ton/transfer": {
1430
+ name: "ton/transfer",
1431
+ description: "Send TON to a recipient address on the TON blockchain.",
1432
+ inputSchema: {
1433
+ type: "object",
1434
+ properties: {
1435
+ to: {
1436
+ type: "string",
1437
+ description: "Recipient TON address"
1438
+ },
1439
+ amount: {
1440
+ type: "string",
1441
+ description: 'Amount of TON to send (e.g., "1.5")'
1442
+ },
1443
+ memo: {
1444
+ type: "string",
1445
+ description: "Optional memo/comment for the transfer"
1446
+ }
1447
+ },
1448
+ required: ["to", "amount"]
1449
+ }
1450
+ },
1451
+ "ton/getJettonBalance": {
1452
+ name: "ton/getJettonBalance",
1453
+ description: "Get the balance of a specific Jetton (TON token) for a wallet address.",
1454
+ inputSchema: {
1455
+ type: "object",
1456
+ properties: {
1457
+ address: {
1458
+ type: "string",
1459
+ description: "TON wallet address"
1460
+ },
1461
+ jettonMaster: {
1462
+ type: "string",
1463
+ description: "Jetton master contract address"
1464
+ }
1465
+ },
1466
+ required: ["address", "jettonMaster"]
1467
+ }
1468
+ },
1469
+ "ton/swapJettons": {
1470
+ name: "ton/swapJettons",
1471
+ description: "Swap Jettons (TON tokens) using DEX aggregation on the TON network. Returns a raw transaction JSON to be sent via sendTransaction.",
1472
+ inputSchema: {
1473
+ type: "object",
1474
+ properties: {
1475
+ from: {
1476
+ type: "string",
1477
+ description: 'Source Jetton master address (or "TON" for native TON)'
1478
+ },
1479
+ to: {
1480
+ type: "string",
1481
+ description: 'Destination Jetton master address (or "TON" for native TON)'
1482
+ },
1483
+ amount: {
1484
+ type: "string",
1485
+ description: 'Amount to swap in human-readable format (e.g., "1.5")'
1486
+ },
1487
+ slippage: {
1488
+ type: "string",
1489
+ description: 'Slippage tolerance (e.g., "0.5" for 0.5%)'
1490
+ }
1491
+ },
1492
+ required: ["from", "to", "amount"]
1493
+ }
1494
+ },
1495
+ "ton/getTransactionStatus": {
1496
+ name: "ton/getTransactionStatus",
1497
+ description: "Check the status and details of a TON transaction by its hash.",
1498
+ inputSchema: {
1499
+ type: "object",
1500
+ properties: {
1501
+ txHash: {
1502
+ type: "string",
1503
+ description: "Transaction hash (BOC hash or message hash)"
1504
+ }
1505
+ },
1506
+ required: ["txHash"]
1507
+ }
1508
+ }
1509
+ };
1510
+ async function executeTonBridgeTool(toolName, args, config) {
1511
+ if (config.demoMode) {
1512
+ return executeTonBridgeToolDemo(toolName, args);
1513
+ }
1514
+ if (config.tonMcpEndpoint) {
1515
+ return proxyToTonMcp(toolName, args, config.tonMcpEndpoint);
1516
+ }
1517
+ if (config.tonApiKey) {
1518
+ return executeTonBridgeToolDirect(toolName, args, config.tonApiKey);
1519
+ }
1520
+ return {
1521
+ content: [
1522
+ {
1523
+ type: "text",
1524
+ text: `Error: TON MCP bridge not configured. Set tonMcpEndpoint or tonApiKey.`
1525
+ }
1526
+ ],
1527
+ isError: true
1528
+ };
1529
+ }
1530
+ function executeTonBridgeToolDemo(toolName, args) {
1531
+ const responses = {
1532
+ "ton/getBalance": `[DEMO] TON Balance for ${args.address ?? "unknown"}:
1533
+ - TON: 5.25
1534
+ - USDT0: 100.00`,
1535
+ "ton/transfer": `[DEMO] Transfer sent:
1536
+ - To: ${args.to ?? "unknown"}
1537
+ - Amount: ${args.amount ?? "0"} TON
1538
+ - TX: demo-ton-tx-hash-${Date.now().toString(36)}`,
1539
+ "ton/getJettonBalance": `[DEMO] Jetton Balance:
1540
+ - Address: ${args.address ?? "unknown"}
1541
+ - Jetton: ${args.jettonMaster ?? "unknown"}
1542
+ - Balance: 50.00`,
1543
+ "ton/swapJettons": `[DEMO] Swap quote received:
1544
+ - From: ${args.from ?? "unknown"}
1545
+ - To: ${args.to ?? "unknown"}
1546
+ - Amount: ${args.amount ?? "0"}
1547
+ - Transaction: {"validUntil":${Math.floor(Date.now() / 1e3) + 300},"messages":[{"address":"EQDemo...","amount":"${args.amount ?? "0"}","payload":"base64..."}]}`,
1548
+ "ton/getTransactionStatus": `[DEMO] Transaction Status:
1549
+ - Hash: ${args.txHash ?? "unknown"}
1550
+ - Status: Confirmed
1551
+ - Block: 12345678`
1552
+ };
1553
+ return {
1554
+ content: [
1555
+ {
1556
+ type: "text",
1557
+ text: responses[toolName] ?? `[DEMO] Unknown TON tool: ${toolName}`
1558
+ }
1559
+ ]
1560
+ };
1561
+ }
1562
+ async function proxyToTonMcp(toolName, args, endpoint) {
1563
+ try {
1564
+ const response = await fetch(endpoint, {
1565
+ method: "POST",
1566
+ headers: { "Content-Type": "application/json" },
1567
+ body: JSON.stringify({
1568
+ jsonrpc: "2.0",
1569
+ id: 1,
1570
+ method: "tools/call",
1571
+ params: { name: toolName, arguments: args }
1572
+ })
1573
+ });
1574
+ if (!response.ok) {
1575
+ throw new Error(`TON MCP server returned ${response.status}`);
1576
+ }
1577
+ const result = await response.json();
1578
+ if (result.error) {
1579
+ throw new Error(result.error.message);
1580
+ }
1581
+ return result.result ?? { content: [{ type: "text", text: "No result" }] };
1582
+ } catch (error) {
1583
+ const message = error instanceof Error ? error.message : String(error);
1584
+ return {
1585
+ content: [{ type: "text", text: `Error proxying to @ton/mcp: ${message}` }],
1586
+ isError: true
1587
+ };
1588
+ }
1589
+ }
1590
+ async function executeTonBridgeToolDirect(toolName, args, apiKey) {
1591
+ const baseUrl = "https://toncenter.com/api/v2";
1592
+ try {
1593
+ switch (toolName) {
1594
+ case "ton/getBalance": {
1595
+ const response = await fetch(
1596
+ `${baseUrl}/getAddressBalance?address=${encodeURIComponent(String(args.address))}&api_key=${apiKey}`
1597
+ );
1598
+ const data = await response.json();
1599
+ const balanceNano = BigInt(data.result);
1600
+ const balanceTon = Number(balanceNano) / 1e9;
1601
+ return {
1602
+ content: [{ type: "text", text: `TON Balance: ${balanceTon.toFixed(4)} TON` }]
1603
+ };
1604
+ }
1605
+ case "ton/getTransactionStatus": {
1606
+ const response = await fetch(
1607
+ `${baseUrl}/getTransactions?hash=${encodeURIComponent(String(args.txHash))}&limit=1&api_key=${apiKey}`
1608
+ );
1609
+ const data = await response.json();
1610
+ if (data.result.length === 0) {
1611
+ return {
1612
+ content: [{ type: "text", text: "Transaction not found" }]
1613
+ };
1614
+ }
1615
+ return {
1616
+ content: [
1617
+ {
1618
+ type: "text",
1619
+ text: `Transaction found:
1620
+ ${JSON.stringify(data.result[0], null, 2)}`
1621
+ }
1622
+ ]
1623
+ };
1624
+ }
1625
+ default:
1626
+ return {
1627
+ content: [
1628
+ {
1629
+ type: "text",
1630
+ text: `Direct execution of ${toolName} is not supported. Configure tonMcpEndpoint to proxy to @ton/mcp.`
1631
+ }
1632
+ ],
1633
+ isError: true
1634
+ };
1635
+ }
1636
+ } catch (error) {
1637
+ const message = error instanceof Error ? error.message : String(error);
1638
+ return {
1639
+ content: [{ type: "text", text: `Error executing TON API call: ${message}` }],
1640
+ isError: true
1641
+ };
1642
+ }
1643
+ }
1644
+ function createTonBridgeToolSet(config) {
1645
+ return {
1646
+ definitions: TON_BRIDGE_TOOLS,
1647
+ async handleToolCall(name, args) {
1648
+ return executeTonBridgeTool(name, args, config);
1649
+ }
1650
+ };
1651
+ }
1652
+
1653
+ // src/tools/unified.ts
1654
+ import { z as z12 } from "zod";
1655
+ import { T402Protocol as T402Protocol2 } from "@t402/wdk-protocol";
1656
+ var smartPayInputSchema = z12.object({
1657
+ url: z12.string().url().describe("URL of the 402-protected resource"),
1658
+ maxBridgeFee: z12.string().regex(/^\d+(\.\d+)?$/).optional().describe("Maximum acceptable bridge fee in native token (optional)"),
1659
+ preferredNetwork: z12.string().optional().describe("Preferred network for payment (optional)")
1660
+ });
1661
+ var paymentPlanInputSchema = z12.object({
1662
+ paymentRequired: z12.object({
1663
+ scheme: z12.string().optional(),
1664
+ network: z12.string().optional(),
1665
+ maxAmountRequired: z12.string().optional(),
1666
+ resource: z12.string().optional(),
1667
+ description: z12.string().optional(),
1668
+ payTo: z12.string().optional(),
1669
+ maxDeadline: z12.number().optional()
1670
+ }).passthrough().describe("The 402 PaymentRequired response")
1671
+ });
1672
+ var UNIFIED_TOOL_DEFINITIONS = {
1673
+ "t402/smartPay": {
1674
+ name: "t402/smartPay",
1675
+ description: "Intelligent payment that automatically checks balance, bridges if needed, and pays. Handles the entire payment flow for 402-protected resources.",
1676
+ inputSchema: {
1677
+ type: "object",
1678
+ properties: {
1679
+ url: { type: "string", description: "URL of the 402-protected resource" },
1680
+ maxBridgeFee: {
1681
+ type: "string",
1682
+ description: "Maximum acceptable bridge fee in native token (optional)"
1683
+ },
1684
+ preferredNetwork: {
1685
+ type: "string",
1686
+ description: "Preferred network for payment (optional)"
1687
+ }
1688
+ },
1689
+ required: ["url"]
1690
+ }
1691
+ },
1692
+ "t402/paymentPlan": {
1693
+ name: "t402/paymentPlan",
1694
+ description: "Analyze a 402 response and create an optimal payment plan considering balances across all chains. Returns recommended network, bridge requirements, and balance overview.",
1695
+ inputSchema: {
1696
+ type: "object",
1697
+ properties: {
1698
+ paymentRequired: {
1699
+ type: "object",
1700
+ description: "The 402 PaymentRequired response"
1701
+ }
1702
+ },
1703
+ required: ["paymentRequired"]
1704
+ }
1705
+ }
1706
+ };
1707
+ async function executeSmartPay(input, wdk) {
1708
+ const steps = [];
1709
+ const chains6 = input.preferredNetwork ? [input.preferredNetwork] : ["ethereum", "arbitrum", "base"];
1710
+ steps.push({
1711
+ action: "check_balance",
1712
+ status: "success",
1713
+ detail: `Checking balances on ${chains6.join(", ")}`
1714
+ });
1715
+ const protocol = await T402Protocol2.create(wdk, { chains: chains6 });
1716
+ steps.push({
1717
+ action: "fetch",
1718
+ status: "success",
1719
+ detail: `Fetching ${input.url}`
1720
+ });
1721
+ const { response, receipt } = await protocol.fetch(input.url);
1722
+ if (receipt) {
1723
+ steps.push({
1724
+ action: "pay",
1725
+ status: "success",
1726
+ detail: `Payment made: ${receipt.amount} on ${receipt.network}`
1727
+ });
1728
+ }
1729
+ const contentType = response.headers.get("content-type") ?? void 0;
1730
+ let body = "";
1731
+ try {
1732
+ body = await response.text();
1733
+ if (body.length > 1e4) {
1734
+ body = body.slice(0, 1e4) + "\n... (truncated)";
1735
+ }
1736
+ } catch {
1737
+ body = "[Could not read response body]";
1738
+ }
1739
+ return {
1740
+ success: response.ok,
1741
+ statusCode: response.status,
1742
+ body,
1743
+ contentType,
1744
+ steps,
1745
+ payment: receipt ? {
1746
+ network: receipt.network,
1747
+ scheme: receipt.scheme,
1748
+ amount: receipt.amount,
1749
+ payTo: receipt.payTo
1750
+ } : void 0
1751
+ };
1752
+ }
1753
+ function executeSmartPayDemo(input) {
1754
+ const network = input.preferredNetwork ?? "arbitrum";
1755
+ return {
1756
+ success: true,
1757
+ statusCode: 200,
1758
+ body: `[Demo] Premium content from ${input.url}
1759
+
1760
+ This is simulated content returned after smart payment.`,
1761
+ contentType: "text/plain",
1762
+ steps: [
1763
+ { action: "check_balance", status: "success", detail: `Checked balances on ${network}` },
1764
+ { action: "check_price", status: "success", detail: "Resource costs 1.00 USDT0" },
1765
+ { action: "pay", status: "success", detail: `Paid 1000000 USDT0 on eip155:42161` },
1766
+ { action: "fetch", status: "success", detail: `Fetched ${input.url}` }
1767
+ ],
1768
+ payment: {
1769
+ network: "eip155:42161",
1770
+ scheme: "exact",
1771
+ amount: "1000000",
1772
+ payTo: "0xC88f67e776f16DcFBf42e6bDda1B82604448899B"
1773
+ }
1774
+ };
1775
+ }
1776
+ async function executePaymentPlan(input, wdk) {
1777
+ const targetNetwork = input.paymentRequired.network;
1778
+ const requiredAmount = input.paymentRequired.maxAmountRequired;
1779
+ const aggregated = await wdk.getAggregatedBalances();
1780
+ const balances = aggregated.chains.map((chain) => {
1781
+ const usdt0 = chain.tokens.find((t) => t.symbol === "USDT0");
1782
+ const usdc = chain.tokens.find((t) => t.symbol === "USDC");
1783
+ return {
1784
+ chain: chain.chain,
1785
+ usdt0: usdt0?.formatted ?? "0",
1786
+ usdc: usdc?.formatted ?? "0"
1787
+ };
1788
+ });
1789
+ const requiredBigint = requiredAmount ? BigInt(requiredAmount) : 0n;
1790
+ const bestChain = await wdk.findBestChainForPayment(requiredBigint);
1791
+ if (bestChain) {
1792
+ const needsBridge = targetNetwork ? !bestChain.chain.includes(targetNetwork) : false;
1793
+ return {
1794
+ viable: true,
1795
+ recommendedNetwork: bestChain.chain,
1796
+ availableBalance: bestChain.balance.toString(),
1797
+ bridgeRequired: needsBridge,
1798
+ bridgeDetails: needsBridge ? {
1799
+ fromChain: bestChain.chain,
1800
+ toChain: targetNetwork ?? bestChain.chain,
1801
+ amount: requiredAmount ?? "0",
1802
+ estimatedFee: "0.001"
1803
+ } : void 0,
1804
+ balances
1805
+ };
1806
+ }
1807
+ return {
1808
+ viable: false,
1809
+ bridgeRequired: false,
1810
+ balances,
1811
+ reason: "Insufficient balance across all chains"
1812
+ };
1813
+ }
1814
+ function executePaymentPlanDemo(_input) {
1815
+ return {
1816
+ viable: true,
1817
+ recommendedNetwork: "arbitrum",
1818
+ availableBalance: "500000000",
1819
+ bridgeRequired: false,
1820
+ balances: [
1821
+ { chain: "ethereum", usdt0: "100.00", usdc: "250.00" },
1822
+ { chain: "arbitrum", usdt0: "500.00", usdc: "0" },
1823
+ { chain: "base", usdt0: "50.00", usdc: "100.00" }
1824
+ ]
1825
+ };
1826
+ }
1827
+ function formatSmartPayResult(result) {
1828
+ const lines = ["## SmartPay Result", ""];
1829
+ if (result.success) {
1830
+ lines.push(`**Status:** Success (${result.statusCode})`);
1831
+ } else {
1832
+ lines.push(`**Status:** Failed (${result.statusCode})`);
1833
+ }
1834
+ if (result.steps.length > 0) {
1835
+ lines.push("");
1836
+ lines.push("### Steps");
1837
+ for (const step of result.steps) {
1838
+ const icon = step.status === "success" ? "[OK]" : step.status === "skipped" ? "[SKIP]" : "[FAIL]";
1839
+ lines.push(`- ${icon} **${step.action}**: ${step.detail}`);
1840
+ }
1841
+ }
1842
+ if (result.payment) {
1843
+ lines.push("");
1844
+ lines.push("### Payment Details");
1845
+ lines.push(`- **Network:** ${result.payment.network}`);
1846
+ lines.push(`- **Scheme:** ${result.payment.scheme}`);
1847
+ lines.push(`- **Amount:** ${result.payment.amount}`);
1848
+ lines.push(`- **Pay To:** \`${result.payment.payTo}\``);
1849
+ }
1850
+ if (result.error) {
1851
+ lines.push("");
1852
+ lines.push(`**Error:** ${result.error}`);
1853
+ }
1854
+ if (result.body) {
1855
+ lines.push("");
1856
+ lines.push("### Response Content");
1857
+ if (result.contentType) {
1858
+ lines.push(`_Content-Type: ${result.contentType}_`);
1859
+ }
1860
+ lines.push("");
1861
+ lines.push("```");
1862
+ lines.push(result.body);
1863
+ lines.push("```");
1864
+ }
1865
+ return lines.join("\n");
1866
+ }
1867
+ function formatPaymentPlanResult(result) {
1868
+ const lines = ["## Payment Plan", ""];
1869
+ if (result.viable) {
1870
+ lines.push(`**Viable:** Yes`);
1871
+ if (result.recommendedNetwork) {
1872
+ lines.push(`**Recommended Network:** ${result.recommendedNetwork}`);
1873
+ }
1874
+ if (result.availableBalance) {
1875
+ lines.push(`**Available Balance:** ${result.availableBalance}`);
1876
+ }
1877
+ } else {
1878
+ lines.push(`**Viable:** No`);
1879
+ if (result.reason) {
1880
+ lines.push(`**Reason:** ${result.reason}`);
1881
+ }
1882
+ }
1883
+ if (result.bridgeRequired && result.bridgeDetails) {
1884
+ lines.push("");
1885
+ lines.push("### Bridge Required");
1886
+ lines.push(`- **From:** ${result.bridgeDetails.fromChain}`);
1887
+ lines.push(`- **To:** ${result.bridgeDetails.toChain}`);
1888
+ lines.push(`- **Amount:** ${result.bridgeDetails.amount}`);
1889
+ lines.push(`- **Estimated Fee:** ${result.bridgeDetails.estimatedFee}`);
1890
+ }
1891
+ if (result.balances.length > 0) {
1892
+ lines.push("");
1893
+ lines.push("### Chain Balances");
1894
+ lines.push("| Chain | USDT0 | USDC |");
1895
+ lines.push("|-------|-------|------|");
1896
+ for (const b of result.balances) {
1897
+ lines.push(`| ${b.chain} | ${b.usdt0} | ${b.usdc} |`);
1898
+ }
1899
+ }
1900
+ return lines.join("\n");
1901
+ }
1902
+
1413
1903
  // src/tools/index.ts
1414
1904
  var TOOL_DEFINITIONS = {
1415
1905
  "t402/getBalance": {
@@ -1770,7 +2260,19 @@ export {
1770
2260
  executeAutoPay,
1771
2261
  executeAutoPayDemo,
1772
2262
  formatAutoPayResult,
2263
+ TON_BRIDGE_TOOLS,
2264
+ executeTonBridgeTool,
2265
+ createTonBridgeToolSet,
2266
+ smartPayInputSchema,
2267
+ paymentPlanInputSchema,
2268
+ UNIFIED_TOOL_DEFINITIONS,
2269
+ executeSmartPay,
2270
+ executeSmartPayDemo,
2271
+ executePaymentPlan,
2272
+ executePaymentPlanDemo,
2273
+ formatSmartPayResult,
2274
+ formatPaymentPlanResult,
1773
2275
  TOOL_DEFINITIONS,
1774
2276
  WDK_TOOL_DEFINITIONS
1775
2277
  };
1776
- //# sourceMappingURL=chunk-5UOBQKXW.mjs.map
2278
+ //# sourceMappingURL=chunk-RDQ7AMR4.mjs.map