@michaleffffff/mcp-trading-server 2.8.2 → 2.9.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.
- package/dist/server.js +2 -2
- package/dist/tools/accountTransfer.js +15 -5
- package/dist/tools/closePosition.js +17 -12
- package/dist/tools/executeTrade.js +21 -16
- package/dist/tools/getMarketList.js +1 -1
- package/dist/tools/getUserTradingFeeRate.js +3 -3
- package/dist/tools/setTpSl.js +2 -2
- package/dist/utils/units.js +9 -1
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -81,7 +81,7 @@ function zodSchemaToJsonSchema(zodSchema) {
|
|
|
81
81
|
};
|
|
82
82
|
}
|
|
83
83
|
// ─── MCP Server ───
|
|
84
|
-
const server = new Server({ name: "myx-mcp-trading-server", version: "2.
|
|
84
|
+
const server = new Server({ name: "myx-mcp-trading-server", version: "2.9.0" }, { capabilities: { tools: {}, resources: {}, prompts: {} } });
|
|
85
85
|
// List tools
|
|
86
86
|
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
|
87
87
|
return {
|
|
@@ -181,7 +181,7 @@ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
|
|
|
181
181
|
async function main() {
|
|
182
182
|
const transport = new StdioServerTransport();
|
|
183
183
|
await server.connect(transport);
|
|
184
|
-
logger.info("🚀 MYX Trading MCP Server v2.
|
|
184
|
+
logger.info("🚀 MYX Trading MCP Server v2.9.0 running (stdio, pure on-chain, prod ready)");
|
|
185
185
|
}
|
|
186
186
|
main().catch((err) => {
|
|
187
187
|
logger.error("Fatal Server Startup Error", err);
|
|
@@ -6,7 +6,7 @@ export const accountDepositTool = {
|
|
|
6
6
|
name: "account_deposit",
|
|
7
7
|
description: "Deposit funds from wallet into the MYX trading account.",
|
|
8
8
|
schema: {
|
|
9
|
-
amount: z.string().
|
|
9
|
+
amount: z.union([z.string(), z.number()]).describe("Amount to deposit (human-readable or raw units)"),
|
|
10
10
|
tokenAddress: z.string().describe("Token address"),
|
|
11
11
|
},
|
|
12
12
|
handler: async (args) => {
|
|
@@ -14,8 +14,13 @@ export const accountDepositTool = {
|
|
|
14
14
|
const { client, signer } = await resolveClient();
|
|
15
15
|
const chainId = getChainId();
|
|
16
16
|
const tokenAddress = normalizeAddress(args.tokenAddress, "tokenAddress");
|
|
17
|
+
// For deposit, we default to quote decimals (6) as it's the most common use case.
|
|
18
|
+
// ensureUnits handles 'raw:' prefix if absolute precision is needed.
|
|
19
|
+
const { ensureUnits } = await import("../utils/units.js");
|
|
20
|
+
const { getQuoteDecimals } = await import("../auth/resolveClient.js");
|
|
21
|
+
const amount = ensureUnits(args.amount, getQuoteDecimals(), "amount");
|
|
17
22
|
const raw = await client.account.deposit({
|
|
18
|
-
amount
|
|
23
|
+
amount,
|
|
19
24
|
tokenAddress,
|
|
20
25
|
chainId,
|
|
21
26
|
});
|
|
@@ -32,17 +37,22 @@ export const accountWithdrawTool = {
|
|
|
32
37
|
description: "Withdraw funds from MYX trading account back to wallet.",
|
|
33
38
|
schema: {
|
|
34
39
|
poolId: z.string().describe("Pool ID to withdraw from"),
|
|
35
|
-
amount: z.string().
|
|
36
|
-
isQuoteToken: z.boolean().describe("Whether to withdraw as quote token"),
|
|
40
|
+
amount: z.union([z.string(), z.number()]).describe("Amount to withdraw (human-readable or raw units)"),
|
|
41
|
+
isQuoteToken: z.coerce.boolean().describe("Whether to withdraw as quote token"),
|
|
37
42
|
},
|
|
38
43
|
handler: async (args) => {
|
|
39
44
|
try {
|
|
40
45
|
const { client, address, signer } = await resolveClient();
|
|
41
46
|
const chainId = getChainId();
|
|
47
|
+
const { ensureUnits } = await import("../utils/units.js");
|
|
48
|
+
const { getQuoteDecimals } = await import("../auth/resolveClient.js");
|
|
49
|
+
// Assuming 18 decimals for base and quoteDecimals for quote
|
|
50
|
+
const decimals = args.isQuoteToken ? getQuoteDecimals() : 18;
|
|
51
|
+
const amount = ensureUnits(args.amount, decimals, "amount");
|
|
42
52
|
const raw = await client.account.withdraw({
|
|
43
53
|
chainId,
|
|
44
54
|
receiver: address,
|
|
45
|
-
amount
|
|
55
|
+
amount,
|
|
46
56
|
poolId: args.poolId,
|
|
47
57
|
isQuoteToken: args.isQuoteToken,
|
|
48
58
|
});
|
|
@@ -2,32 +2,37 @@ import { z } from "zod";
|
|
|
2
2
|
import { resolveClient } from "../auth/resolveClient.js";
|
|
3
3
|
import { closePosition as closePos } from "../services/tradeService.js";
|
|
4
4
|
import { finalizeMutationResult } from "../utils/mutationResult.js";
|
|
5
|
-
import {
|
|
5
|
+
import { SLIPPAGE_PCT_4DP_DESC } from "../utils/slippage.js";
|
|
6
6
|
import { verifyTradeOutcome } from "../utils/verification.js";
|
|
7
|
+
import { mapDirection, mapOrderType, mapTriggerType } from "../utils/mappings.js";
|
|
7
8
|
export const closePositionTool = {
|
|
8
9
|
name: "close_position",
|
|
9
10
|
description: "Create a decrease order using SDK-native parameters.",
|
|
10
11
|
schema: {
|
|
11
12
|
poolId: z.string().describe("Pool ID"),
|
|
12
13
|
positionId: z.string().describe("Position ID to close"),
|
|
13
|
-
orderType: z.
|
|
14
|
-
triggerType: z.
|
|
15
|
-
direction: z.
|
|
16
|
-
collateralAmount: z.union([z.string(), z.number()]).describe("Collateral amount (human
|
|
17
|
-
size: z.union([z.string(), z.number()]).describe("Position size (human
|
|
18
|
-
price: z.union([z.string(), z.number()]).describe("Price (human
|
|
19
|
-
timeInForce: z.coerce.number().int().describe("TimeInForce
|
|
14
|
+
orderType: z.union([z.number(), z.string()]).describe("Order type: 0/MARKET or 1/LIMIT"),
|
|
15
|
+
triggerType: z.union([z.number(), z.string()]).optional().describe("Trigger type: 0/NONE, 1/GTE, 2/LTE"),
|
|
16
|
+
direction: z.union([z.number(), z.string()]).describe("Position direction: 0/LONG or 1/SHORT"),
|
|
17
|
+
collateralAmount: z.union([z.string(), z.number()]).describe("Collateral amount (human or raw units)"),
|
|
18
|
+
size: z.union([z.string(), z.number()]).describe("Position size (human or raw units)"),
|
|
19
|
+
price: z.union([z.string(), z.number()]).describe("Price (human or 30-dec raw units)"),
|
|
20
|
+
timeInForce: z.coerce.number().int().describe("TimeInForce: 0=GTC, 1=IOC, 2=FOK"),
|
|
20
21
|
postOnly: z.coerce.boolean().describe("Post-only flag"),
|
|
21
|
-
slippagePct: z.coerce.string().
|
|
22
|
-
message: "slippagePct must be an integer in [0, 10000] with 4-decimal precision (1 = 0.01%).",
|
|
23
|
-
}).describe(SLIPPAGE_PCT_4DP_DESC),
|
|
22
|
+
slippagePct: z.coerce.string().describe(SLIPPAGE_PCT_4DP_DESC),
|
|
24
23
|
executionFeeToken: z.string().describe("Execution fee token address"),
|
|
25
24
|
leverage: z.coerce.number().describe("Leverage"),
|
|
26
25
|
},
|
|
27
26
|
handler: async (args) => {
|
|
28
27
|
try {
|
|
29
28
|
const { client, address, signer } = await resolveClient();
|
|
30
|
-
const
|
|
29
|
+
const mappedArgs = {
|
|
30
|
+
...args,
|
|
31
|
+
direction: mapDirection(args.direction),
|
|
32
|
+
orderType: mapOrderType(args.orderType),
|
|
33
|
+
triggerType: args.triggerType !== undefined ? mapTriggerType(args.triggerType) : undefined,
|
|
34
|
+
};
|
|
35
|
+
const raw = await closePos(client, address, mappedArgs);
|
|
31
36
|
const data = await finalizeMutationResult(raw, signer, "close_position");
|
|
32
37
|
const txHash = data.confirmation?.txHash;
|
|
33
38
|
let verification = null;
|
|
@@ -2,8 +2,9 @@ import { z } from "zod";
|
|
|
2
2
|
import { resolveClient } from "../auth/resolveClient.js";
|
|
3
3
|
import { openPosition } from "../services/tradeService.js";
|
|
4
4
|
import { finalizeMutationResult } from "../utils/mutationResult.js";
|
|
5
|
-
import {
|
|
5
|
+
import { SLIPPAGE_PCT_4DP_DESC } from "../utils/slippage.js";
|
|
6
6
|
import { verifyTradeOutcome } from "../utils/verification.js";
|
|
7
|
+
import { mapDirection, mapOrderType, mapTriggerType } from "../utils/mappings.js";
|
|
7
8
|
const POSITION_ID_RE = /^$|^0x[0-9a-fA-F]{64}$/;
|
|
8
9
|
export const executeTradeTool = {
|
|
9
10
|
name: "execute_trade",
|
|
@@ -13,30 +14,34 @@ export const executeTradeTool = {
|
|
|
13
14
|
positionId: z.string().refine((value) => POSITION_ID_RE.test(value), {
|
|
14
15
|
message: "positionId must be empty string for new position, or a bytes32 hex string.",
|
|
15
16
|
}).describe("Position ID: Use empty string '' for NEW positions, or valid hex for INCREASING existing ones."),
|
|
16
|
-
orderType: z.
|
|
17
|
-
triggerType: z.
|
|
18
|
-
direction: z.
|
|
19
|
-
collateralAmount: z.
|
|
20
|
-
size: z.
|
|
21
|
-
price: z.
|
|
17
|
+
orderType: z.union([z.number(), z.string()]).describe("Market/Limit/Stop. e.g. 0 or 'MARKET'."),
|
|
18
|
+
triggerType: z.union([z.number(), z.string()]).optional().describe("0=None (Market), 1=GTE, 2=LTE. e.g. 'GTE'."),
|
|
19
|
+
direction: z.union([z.number(), z.string()]).describe("0/LONG/BUY or 1/SHORT/SELL."),
|
|
20
|
+
collateralAmount: z.union([z.string(), z.number()]).describe("Collateral. e.g. '100' or 'raw:100000000' (6 decimals for USDC)."),
|
|
21
|
+
size: z.union([z.string(), z.number()]).describe("Notional size in base tokens. e.g. '0.5' BTC or 'raw:50000000'."),
|
|
22
|
+
price: z.union([z.string(), z.number()]).describe("Execution or Limit price. e.g. '65000' or 'raw:...'"),
|
|
22
23
|
timeInForce: z.coerce.number().int().describe("0=GTC, 1=IOC, 2=FOK"),
|
|
23
24
|
postOnly: z.coerce.boolean().describe("If true, order only executes as Maker."),
|
|
24
|
-
slippagePct: z.coerce.string().
|
|
25
|
-
message: "slippagePct must be an integer in [0, 10000] with 4-decimal precision (1 = 0.01%).",
|
|
26
|
-
}).describe(`${SLIPPAGE_PCT_4DP_DESC}. Standard is 100 (1%).`),
|
|
25
|
+
slippagePct: z.coerce.string().describe(`${SLIPPAGE_PCT_4DP_DESC}. Standard is 100 (1%).`),
|
|
27
26
|
executionFeeToken: z.string().describe("Address of token to pay gas/execution fees (typically USDC)."),
|
|
28
27
|
leverage: z.coerce.number().describe("Leverage multiplier, e.g., 10 for 10x."),
|
|
29
|
-
tpSize: z.
|
|
30
|
-
tpPrice: z.
|
|
31
|
-
slSize: z.
|
|
32
|
-
slPrice: z.
|
|
33
|
-
tradingFee: z.
|
|
28
|
+
tpSize: z.union([z.string(), z.number()]).optional().describe("Take Profit size. Use '0' to disable."),
|
|
29
|
+
tpPrice: z.union([z.string(), z.number()]).optional().describe("Take Profit trigger price."),
|
|
30
|
+
slSize: z.union([z.string(), z.number()]).optional().describe("Stop Loss size. Use '0' to disable."),
|
|
31
|
+
slPrice: z.union([z.string(), z.number()]).optional().describe("Stop Loss trigger price."),
|
|
32
|
+
tradingFee: z.union([z.string(), z.number()]).describe("Estimated fee in raw units. Fetch via get_user_trading_fee_rate."),
|
|
34
33
|
marketId: z.string().describe("Specific Market Config Hash. Fetch via get_market_list."),
|
|
35
34
|
},
|
|
36
35
|
handler: async (args) => {
|
|
37
36
|
try {
|
|
38
37
|
const { client, address, signer } = await resolveClient();
|
|
39
|
-
const
|
|
38
|
+
const mappedArgs = {
|
|
39
|
+
...args,
|
|
40
|
+
direction: mapDirection(args.direction),
|
|
41
|
+
orderType: mapOrderType(args.orderType),
|
|
42
|
+
triggerType: args.triggerType !== undefined ? mapTriggerType(args.triggerType) : undefined,
|
|
43
|
+
};
|
|
44
|
+
const raw = await openPosition(client, address, mappedArgs);
|
|
40
45
|
const data = await finalizeMutationResult(raw, signer, "execute_trade");
|
|
41
46
|
const txHash = data.confirmation?.txHash;
|
|
42
47
|
let verification = null;
|
|
@@ -5,7 +5,7 @@ export const getMarketListTool = {
|
|
|
5
5
|
name: "get_market_list",
|
|
6
6
|
description: "Get tradable markets/pools (state=2 Active). Supports configurable result limit; backend may still enforce its own cap.",
|
|
7
7
|
schema: {
|
|
8
|
-
limit: z.number().int().positive().optional().describe("Max results to request (default 1000)."),
|
|
8
|
+
limit: z.coerce.number().int().positive().optional().describe("Max results to request (default 1000)."),
|
|
9
9
|
},
|
|
10
10
|
handler: async (args) => {
|
|
11
11
|
try {
|
|
@@ -4,9 +4,9 @@ export const getUserTradingFeeRateTool = {
|
|
|
4
4
|
name: "get_user_trading_fee_rate",
|
|
5
5
|
description: "Get maker/taker fee rates for a given assetClass and riskTier.",
|
|
6
6
|
schema: {
|
|
7
|
-
assetClass: z.number().int().nonnegative().describe("Asset class ID"),
|
|
8
|
-
riskTier: z.number().int().nonnegative().describe("Risk tier"),
|
|
9
|
-
chainId: z.number().int().positive().optional().describe("Optional chainId override"),
|
|
7
|
+
assetClass: z.coerce.number().int().nonnegative().describe("Asset class ID"),
|
|
8
|
+
riskTier: z.coerce.number().int().nonnegative().describe("Risk tier"),
|
|
9
|
+
chainId: z.coerce.number().int().positive().optional().describe("Optional chainId override"),
|
|
10
10
|
},
|
|
11
11
|
handler: async (args) => {
|
|
12
12
|
try {
|
package/dist/tools/setTpSl.js
CHANGED
|
@@ -13,8 +13,8 @@ export const setTpSlTool = {
|
|
|
13
13
|
direction: z.any().describe("0=LONG, 1=SHORT, or strings like 'BUY'/'SELL'/'LONG'/'SHORT'."),
|
|
14
14
|
leverage: z.coerce.number().describe("Leverage multiplier, e.g., 10."),
|
|
15
15
|
executionFeeToken: z.string().describe("Address of token to pay gas/execution fees."),
|
|
16
|
-
tpTriggerType: z.union([z.number(), z.string()]).optional().describe("0
|
|
17
|
-
slTriggerType: z.union([z.number(), z.string()]).optional().describe("0
|
|
16
|
+
tpTriggerType: z.union([z.number(), z.string()]).optional().describe("0/NONE, 1/GTE, 2/LTE. e.g. 'GTE'."),
|
|
17
|
+
slTriggerType: z.union([z.number(), z.string()]).optional().describe("0/NONE, 1/GTE, 2/LTE. e.g. 'LTE'."),
|
|
18
18
|
slippagePct: z.coerce.string().refine(isValidSlippagePct4dp, {
|
|
19
19
|
message: "slippagePct must be an integer in [0, 10000] with 4-decimal precision (1 = 0.01%).",
|
|
20
20
|
}).describe(`${SLIPPAGE_PCT_4DP_DESC}. Standard is 100 (1%).`),
|
package/dist/utils/units.js
CHANGED
|
@@ -20,7 +20,7 @@ function normalizeDecimal(input) {
|
|
|
20
20
|
return `${sign}${intPart || "0"}.${fracPart}`;
|
|
21
21
|
}
|
|
22
22
|
export function ensureUnits(value, decimals, label = "value") {
|
|
23
|
-
|
|
23
|
+
let str = String(value).trim();
|
|
24
24
|
if (!str)
|
|
25
25
|
throw new Error(`${label} is required.`);
|
|
26
26
|
if (RAW_PREFIX_RE.test(str)) {
|
|
@@ -31,6 +31,14 @@ export function ensureUnits(value, decimals, label = "value") {
|
|
|
31
31
|
}
|
|
32
32
|
if (!DECIMAL_RE.test(str))
|
|
33
33
|
throw new Error(`${label} must be a numeric string.`);
|
|
34
|
+
// Truncate decimals if they exceed the allowed precision to prevent parseUnits throwing
|
|
35
|
+
if (str.includes(".")) {
|
|
36
|
+
const parts = str.split(".");
|
|
37
|
+
if (parts[1].length > decimals) {
|
|
38
|
+
console.warn(`[ensureUnits] Truncating ${label} precision: ${str} -> ${parts[0]}.${parts[1].slice(0, decimals)}`);
|
|
39
|
+
str = `${parts[0]}.${parts[1].slice(0, decimals)}`;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
34
42
|
// If it's already a very large integer (e.g. > 12 digits or > decimals digits),
|
|
35
43
|
// assume it's already in the smallest unit (Wei/Raw).
|
|
36
44
|
if (!str.includes(".") && (str.length > 12 || str.length > decimals)) {
|