@michaleffffff/mcp-trading-server 2.1.3 → 2.2.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 CHANGED
@@ -1,4 +1,36 @@
1
1
  #!/usr/bin/env node
2
+ /**
3
+ * Global stdout redirection for MCP JSON-RPC compatibility.
4
+ * Intercepts all process.stdout.write calls and redirects non-JSON strings to stderr.
5
+ */
6
+ const originalStdoutWrite = process.stdout.write.bind(process.stdout);
7
+ process.stdout.write = (chunk, encoding, callback) => {
8
+ const output = chunk.toString();
9
+ // JSON-RPC messages always start with { "jsonrpc": "2.0"
10
+ if (output.trim().startsWith('{') && output.includes('"jsonrpc"')) {
11
+ return originalStdoutWrite(chunk, encoding, callback);
12
+ }
13
+ // Everything else goes to stderr
14
+ return process.stderr.write(chunk, encoding, callback);
15
+ };
16
+ // --- Process Logic Protection ---
17
+ // Catch unhandled promise rejections
18
+ process.on('unhandledRejection', (reason, promise) => {
19
+ logger.error('Unhandled Rejection at:', promise);
20
+ logger.error('Reason:', reason);
21
+ });
22
+ // Catch uncaught exceptions
23
+ process.on('uncaughtException', (error) => {
24
+ logger.error('Uncaught Exception thrown:', error);
25
+ process.exit(1);
26
+ });
27
+ // Graceful shutdown on signals
28
+ const shutdown = () => {
29
+ logger.info("Termination signal received. Shutting down...");
30
+ process.exit(0);
31
+ };
32
+ process.on('SIGINT', shutdown);
33
+ process.on('SIGTERM', shutdown);
2
34
  import "dotenv/config";
3
35
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
4
36
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
@@ -48,7 +80,7 @@ function zodSchemaToJsonSchema(zodSchema) {
48
80
  };
49
81
  }
50
82
  // ─── MCP Server ───
51
- const server = new Server({ name: "myx-mcp-trading-server", version: "2.1.0" }, { capabilities: { tools: {}, resources: {}, prompts: {} } });
83
+ const server = new Server({ name: "myx-mcp-trading-server", version: "2.2.0" }, { capabilities: { tools: {}, resources: {}, prompts: {} } });
52
84
  // List tools
53
85
  server.setRequestHandler(ListToolsRequestSchema, async () => {
54
86
  return {
@@ -36,7 +36,7 @@ export async function openPosition(client, address, args) {
36
36
  const oracleData = await client.utils.getOraclePrice(args.poolId, chainId);
37
37
  calcPrice30 = ensureUnits(oracleData.price, 30, "oracle price");
38
38
  orderPrice30 = calcPrice30;
39
- console.log(`[Market Order] Fetched oracle price for size math: ${calcPrice30}`);
39
+ console.error(`[Market Order] Fetched oracle price for size math: ${calcPrice30}`);
40
40
  }
41
41
  // 保证金转为 quote token 精度
42
42
  const collateralWei = parseHumanUnits(args.collateralAmount, quoteDecimals, "collateralAmount");
@@ -13,7 +13,7 @@ export function injectBrowserProviderMock(chainId, provider, signer) {
13
13
  isMock: true,
14
14
  isMetaMask: true,
15
15
  request: async ({ method, params }) => {
16
- // console.log(`[Mock-RPC] ${method}`);
16
+ // console.error(`[Mock-RPC] ${method}`);
17
17
  if (method === "eth_chainId")
18
18
  return "0x" + chainId.toString(16);
19
19
  if (method === "eth_accounts" || method === "eth_requestAccounts")
@@ -22,12 +22,12 @@ export function injectBrowserProviderMock(chainId, provider, signer) {
22
22
  return signer.signMessage(params[0]);
23
23
  if (method === "eth_sendTransaction") {
24
24
  const tx = params[0];
25
- console.log(`\n[MCP Router] 拦截到 SDK 发送交易请求,正在签名广播...`);
25
+ console.error(`\n[MCP Router] 拦截到 SDK 发送交易请求,正在签名广播...`);
26
26
  // Ensure from is set correctly
27
27
  if (!tx.from)
28
28
  tx.from = signer.address;
29
29
  const txRes = await signer.sendTransaction(tx);
30
- console.log(`[MCP Router] 交易已提交,哈希: ${txRes.hash}`);
30
+ console.error(`[MCP Router] 交易已提交,哈希: ${txRes.hash}`);
31
31
  return txRes.hash;
32
32
  }
33
33
  if (method === "eth_estimateGas" || method === "eth_gasPrice" || method === "eth_getBalance" || method === "eth_call") {
@@ -1,9 +1,9 @@
1
1
  export const logger = {
2
2
  info: (message, meta) => {
3
- console.log(`[INFO] ${new Date().toISOString()} - ${message}`, meta ? meta : "");
3
+ console.error(`[INFO] ${new Date().toISOString()} - ${message}`, meta ? meta : "");
4
4
  },
5
5
  warn: (message, meta) => {
6
- console.warn(`[WARN] ${new Date().toISOString()} - ${message}`, meta ? meta : "");
6
+ console.error(`[WARN] ${new Date().toISOString()} - ${message}`, meta ? meta : "");
7
7
  },
8
8
  error: (message, err) => {
9
9
  console.error(`[ERROR] ${new Date().toISOString()} - ${message}`);
@@ -12,7 +12,7 @@ export const logger = {
12
12
  }
13
13
  },
14
14
  toolExecution: (toolName, args) => {
15
- console.log(`[TOOL_EXECUTION] ${new Date().toISOString()} - Target: ${toolName}`);
16
- console.dir(args, { depth: null, colors: true });
15
+ console.error(`[TOOL_EXECUTION] ${new Date().toISOString()} - Target: ${toolName}`);
16
+ console.error(JSON.stringify(args, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2));
17
17
  }
18
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@michaleffffff/mcp-trading-server",
3
- "version": "2.1.3",
3
+ "version": "2.2.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "myx-mcp": "dist/server.js"