#!/usr/bin/env node
// Query BlackBerry costs from AllCloud MSP account
const { spawn } = require('child_process');
async function getBlackBerryCosts() {
console.log('π QUERYING BLACKBERRY COSTS FROM ALLCLOUD MSP');
console.log('==============================================');
let mcpProcess = null;
try {
// Start server
mcpProcess = spawn('node', ['dist/index.js'], {
stdio: ['pipe', 'pipe', 'pipe'],
cwd: __dirname,
env: {
...process.env,
UMBRELLA_API_BASE_URL: 'https://api.umbrellacost.io/api/v1'
}
});
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('β
MCP server started');
// Authenticate with AllCloud MSP account
console.log('\nπ Authenticating AllCloud MSP account...');
const authResult = await makeCall(mcpProcess, 'authenticate_user', {
username: 'david+allcloud@umbrellacost.com',
password: 'B4*zcI7#F7poEC'
});
if (authResult.success) {
console.log('β
AllCloud MSP authentication successful');
// Query BlackBerry costs using customer detection
console.log('\nπ§ͺ Querying BlackBerry costs...');
const blackberryResult = await makeCall(mcpProcess, 'api___invoices_caui', {
userQuery: 'Show me BlackBerry costs for last month',
startDate: '2025-07-01',
endDate: '2025-07-31',
periodGranLevel: 'month',
costType: ['cost', 'discount'],
isUnblended: true,
cloud_context: 'aws'
});
if (blackberryResult.success) {
const text = blackberryResult.data?.content?.[0]?.text || '';
console.log('\nπ BLACKBERRY COST RESULTS:');
console.log('β'.repeat(50));
console.log(text);
// Extract cost data
const jsonMatch = text.match(/```json\n([\s\S]*?)\n```/);
if (jsonMatch) {
try {
const data = JSON.parse(jsonMatch[1]);
console.log('\nπ° BLACKBERRY COST ANALYSIS:');
if (Array.isArray(data)) {
const totalCost = data.reduce((sum, item) => {
return sum + (parseFloat(item.total_cost) || 0);
}, 0);
console.log(`π Total BlackBerry Costs: $${totalCost.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`);
// Show breakdown
data.forEach((item, index) => {
console.log(`\n Item ${index + 1}:`);
console.log(` Account: ${item.account_id}`);
console.log(` Date: ${item.usage_date}`);
console.log(` Cost: $${parseFloat(item.total_cost || 0).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`);
if (item.total_usage_quantity) {
console.log(` Usage Quantity: ${parseFloat(item.total_usage_quantity).toLocaleString()}`);
}
});
} else if (data.total_cost !== undefined) {
const cost = parseFloat(data.total_cost) || 0;
console.log(`π BlackBerry Cost: $${cost.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`);
} else {
console.log('β No cost data found in response');
}
} catch (parseError) {
console.log('β Failed to parse JSON:', parseError.message);
}
} else {
console.log('β No JSON data found in response');
}
} else {
console.log('β BlackBerry query failed:', blackberryResult.error);
}
// Also try getting customer list to see if BlackBerry is there
console.log('\nπ Checking MSP customer list for BlackBerry...');
const customersResult = await makeCall(mcpProcess, 'api___plain_sub_users', {});
if (customersResult.success) {
const text = customersResult.data?.content?.[0]?.text || '';
if (text.toLowerCase().includes('blackberry')) {
console.log('β
BlackBerry found in customer list');
} else {
console.log('β οΈ BlackBerry not found in customer list');
console.log('π Customer list preview:', text.substring(0, 300));
}
} else {
console.log('β Failed to get customer list:', customersResult.error);
}
} else {
console.log('β AllCloud authentication failed:', authResult.error);
}
} catch (error) {
console.error('β Query failed:', error.message);
} finally {
if (mcpProcess) {
mcpProcess.kill();
console.log('\nπ Query completed');
}
}
}
async function makeCall(process, endpoint, params) {
return new Promise((resolve) => {
try {
const request = {
jsonrpc: '2.0',
id: Date.now(),
method: 'tools/call',
params: {
name: endpoint,
arguments: params
}
};
const requestString = JSON.stringify(request) + '\n';
process.stdin.write(requestString);
const timeout = setTimeout(() => {
resolve({ success: false, error: 'Timeout after 15s' });
}, 15000);
const responseHandler = (data) => {
clearTimeout(timeout);
try {
const lines = data.toString().split('\n').filter(line => line.trim());
const lastLine = lines[lines.length - 1];
const response = JSON.parse(lastLine);
if (response.error) {
resolve({ success: false, error: response.error.message });
} else {
resolve({ success: true, data: response.result });
}
} catch (error) {
resolve({ success: false, error: 'Parse error: ' + error.message });
}
};
process.stdout.once('data', responseHandler);
} catch (error) {
resolve({ success: false, error: error.message });
}
});
}
getBlackBerryCosts();