#!/usr/bin/env node
/**
* COMPARE API KEY HANDLING: CAUI API vs RECOMMENDATIONS API
* ========================================================
*
* This script will compare how the CAUI API and recommendations API
* handle API key generation for MSP customer detection.
*/
const { Client } = require('@modelcontextprotocol/sdk/client/index.js');
const { StdioClientTransport } = require('@modelcontextprotocol/sdk/client/stdio.js');
console.log('🔬 COMPARING API KEY HANDLING: CAUI vs RECOMMENDATIONS');
console.log('='.repeat(70));
async function testBothAPIs() {
const transport = new StdioClientTransport({
command: 'node',
args: ['/Users/david/Downloads/MCP/UmbrellaMCP/dist/index.js'],
env: {
...process.env,
UMBRELLA_BASE_URL: 'https://umbrellacost.io',
DEBUG: 'true'
}
});
const client = new Client(
{ name: 'api-comparison-client', version: '1.0.0' },
{ capabilities: {} }
);
try {
await client.connect(transport);
console.log('✅ Connected to MCP server');
// Step 1: OAuth Authentication
console.log('\n🔐 STEP 1: OAuth Authentication');
console.log('-'.repeat(40));
const authResult = await client.request({
method: 'tools/call',
params: {
name: 'oauth_authenticate',
arguments: {
username: 'david+allcloud@umbrellacost.com',
password: process.env.UMBRELLA_PASSWORD || 'defaultPassword'
}
}
});
if (!authResult.content?.[0]?.text?.includes('successfully')) {
throw new Error('OAuth authentication failed');
}
console.log('✅ OAuth authentication successful');
// Step 2: Test CAUI API (Known Working)
console.log('\n🏦 STEP 2: Testing CAUI API (Known Working)');
console.log('-'.repeat(50));
console.log('Query: "show Bank Leumi BL Test Env costs"');
const cauiResult = await client.request({
method: 'tools/call',
params: {
name: 'api__invoices_caui',
arguments: {
userQuery: 'show Bank Leumi BL Test Env costs',
// Add minimal required parameters
startDate: '2024-01-01',
endDate: '2024-01-31'
}
}
});
console.log('\n📊 CAUI API RESULT:');
console.log('='.repeat(40));
if (cauiResult.content?.[0]?.text) {
const cauiText = cauiResult.content[0].text;
console.log(cauiText);
// Check for API key patterns
const apiKeyMatch = cauiText.match(/57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1/);
if (apiKeyMatch) {
console.log('\n✅ CAUI API KEY FOUND: 57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1');
} else {
console.log('\n⚠️ CAUI API KEY NOT FOUND in response');
}
} else {
console.log('❌ No CAUI response content');
}
// Step 3: Test Recommendations API (Problematic)
console.log('\n🎯 STEP 3: Testing Recommendations API (Problematic)');
console.log('-'.repeat(55));
console.log('Query: "show Bank Leumi BL Test Env cost recommendations"');
const recommendationsResult = await client.request({
method: 'tools/call',
params: {
name: 'api__api_v2_recommendations_list',
arguments: {
userQuery: 'show Bank Leumi BL Test Env cost recommendations'
}
}
});
console.log('\n📊 RECOMMENDATIONS API RESULT:');
console.log('='.repeat(40));
if (recommendationsResult.content?.[0]?.text) {
const recText = recommendationsResult.content[0].text;
console.log(recText);
// Check for API key patterns
const apiKeyMatch = recText.match(/57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1/);
if (apiKeyMatch) {
console.log('\n✅ RECOMMENDATIONS API KEY FOUND: 57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1');
} else {
console.log('\n❌ RECOMMENDATIONS API KEY NOT FOUND in response');
}
// Check for error patterns
if (recText.includes('No recommendations data received')) {
console.log('\n❌ RECOMMENDATIONS API: "No recommendations data received" error detected');
}
} else {
console.log('❌ No recommendations response content');
}
// Step 4: Analysis
console.log('\n🔍 STEP 4: ANALYSIS');
console.log('-'.repeat(30));
console.log('\n📋 COMPARISON SUMMARY:');
console.log('1. Both APIs use the same customer detection logic (line 1292-1294 in server.ts)');
console.log('2. Both APIs should call detectCustomerFromQuery() for MSP users');
console.log('3. Both APIs should generate the same API key format');
console.log('4. The difference must be in how the detected customer info is applied');
console.log('\n🎯 KEY FINDINGS:');
const cauiHasApiKey = cauiResult.content?.[0]?.text?.includes('57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1');
const recHasApiKey = recommendationsResult.content?.[0]?.text?.includes('57ade50e-c9a8-49f3-8ce7-28d44536a669:24223:1');
if (cauiHasApiKey && !recHasApiKey) {
console.log('❌ PROBLEM CONFIRMED: CAUI API generates API key, Recommendations API does not');
console.log(' - This suggests the issue is AFTER customer detection');
console.log(' - Customer detection is working, but API key application differs');
} else if (cauiHasApiKey && recHasApiKey) {
console.log('✅ BOTH APIs working correctly - investigate why user reports issues');
} else if (!cauiHasApiKey && !recHasApiKey) {
console.log('❌ BOTH APIs broken - customer detection not working at all');
} else {
console.log('⚠️ Unexpected result pattern - needs deeper investigation');
}
} catch (error) {
console.error('\n💥 Test failed:', error.message);
console.error('Stack:', error.stack);
} finally {
try {
await client.close();
} catch (e) {
// Ignore close errors
}
}
}
testBothAPIs().then(() => {
console.log('\n' + '='.repeat(70));
console.log('🏁 API KEY COMPARISON COMPLETE');
console.log('='.repeat(70));
}).catch(error => {
console.error('❌ Fatal error:', error.message);
process.exit(1);
});