#!/usr/bin/env node
const { spawn } = require('child_process');
class BankHapoalimExactMatch {
constructor() {
this.server = null;
this.requestId = 1;
}
async start() {
console.log('🏦 BANK HAPOALIM - MATCHING UI REQUEST EXACTLY');
console.log('===============================================');
this.server = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe']
});
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('✅ Server ready\n');
}
async sendRequest(method, params = {}) {
return new Promise((resolve, reject) => {
const request = {
jsonrpc: '2.0',
id: this.requestId++,
method,
params
};
this.server.stdin.write(JSON.stringify(request) + '\n');
const timeout = setTimeout(() => {
reject(new Error(`Timeout: ${method}`));
}, 30000);
const handleData = (data) => {
clearTimeout(timeout);
this.server.stdout.removeListener('data', handleData);
try {
const lines = data.toString().split('\n').filter(line => line.trim());
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i];
if (line.trim()) {
try {
const response = JSON.parse(line);
resolve(response);
return;
} catch (e) {
continue;
}
}
}
reject(new Error('No valid JSON response found'));
} catch (error) {
reject(error);
}
};
this.server.stdout.on('data', handleData);
});
}
async getCostData() {
try {
console.log('🔧 Initialize and Authenticate');
await this.sendRequest('initialize', {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: { name: 'Bank Hapoalim Exact', version: '1.0.0' }
});
await this.sendRequest('tools/call', {
name: 'authenticate_user',
arguments: {
username: 'david+allcloud@umbrellacost.com',
password: 'B4*zcI7#F7poEC'
}
});
console.log('✅ Authenticated\n');
// Exactly match the UI request
console.log('💰 MATCHING UI REQUEST (excluding taxes):');
console.log('==========================================\n');
const resp = await this.sendRequest('tools/call', {
name: 'api___invoices_caui',
arguments: {
startDate: '2025-01-01',
endDate: '2025-08-31',
costType: ['cost', 'discount'], // Both cost and discount
groupBy: 'none', // The UI sends this twice but we only need once
periodGranLevel: 'month',
excludeFilters: {
chargetype: ['Tax'] // EXCLUDE TAXES like the UI does
},
cloud_context: 'aws',
customer_account_key: '16185' // Bank Hapoalim
// isPpApplied is automatically added by our implementation
}
});
const content = resp.result?.content?.[0]?.text;
if (content) {
// Extract the JSON data
const jsonMatch = content.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
const mcpData = JSON.parse(jsonMatch[1]);
// UI response data from your paste
const uiData = [
{ month: "2025-01", cost: 2839.380705943422 },
{ month: "2025-02", cost: 3382.1896458348833 },
{ month: "2025-03", cost: 2684.566823560157 },
{ month: "2025-04", cost: 2060.9445014484645 },
{ month: "2025-05", cost: 2236.337430109406 },
{ month: "2025-06", cost: 2150.8971216148257 },
{ month: "2025-07", cost: 2569.8836261986307 },
{ month: "2025-08", cost: 2144.2859467385892 }
];
console.log('📊 COMPARISON: UI vs MCP (Excluding Taxes)');
console.log('===========================================');
console.log('Month | UI Amount | MCP Amount | Difference');
console.log('---------|----------------|----------------|------------');
let totalDiff = 0;
mcpData.forEach((item, index) => {
const uiAmount = uiData[index]?.cost || 0;
const mcpAmount = item.total_cost;
const diff = mcpAmount - uiAmount;
totalDiff += diff;
console.log(`${item.usage_date} | $${uiAmount.toFixed(2).padEnd(13)} | $${mcpAmount.toFixed(2).padEnd(13)} | ${diff >= 0 ? '+' : ''}$${diff.toFixed(2)}`);
});
console.log('---------|----------------|----------------|------------');
const uiTotal = uiData.reduce((sum, item) => sum + item.cost, 0);
const mcpTotal = mcpData.reduce((sum, item) => sum + item.total_cost, 0);
console.log(`TOTAL | $${uiTotal.toFixed(2).padEnd(12)} | $${mcpTotal.toFixed(2).padEnd(12)} | ${totalDiff >= 0 ? '+' : ''}$${totalDiff.toFixed(2)}`);
console.log('\n📈 ANALYSIS:');
if (Math.abs(totalDiff) < 1) {
console.log('✅ Perfect match! The MCP server is returning the same values as the UI.');
} else if (Math.abs(totalDiff) < 100) {
console.log('✅ Very close match! Small differences might be due to rounding or timing.');
console.log(` Total difference: $${Math.abs(totalDiff).toFixed(2)}`);
} else {
console.log('⚠️ Significant difference detected.');
console.log(` Total difference: $${Math.abs(totalDiff).toFixed(2)}`);
console.log(' Possible causes:');
console.log(' - Different data refresh times');
console.log(' - Additional filters in UI not replicated');
console.log(' - API endpoint differences (frontend vs backend)');
}
}
}
} catch (error) {
console.error('❌ Error:', error.message);
}
}
async cleanup() {
if (this.server) {
this.server.kill();
}
}
async run() {
try {
await this.start();
await this.getCostData();
} finally {
await this.cleanup();
console.log('\n🏁 Bank Hapoalim Exact Match Test Complete');
}
}
}
const costQuery = new BankHapoalimExactMatch();
costQuery.run();