// ============================================================================
// MCP Server Test Script
// ============================================================================
// This script tests the MCP server endpoint exposed through Azure APIM
// Usage: node test-mcp.js <apim-url> <subscription-key>
// ============================================================================
const https = require('https');
const http = require('http');
// Configuration from command line or environment
const APIM_URL = process.argv[2] || process.env.APIM_MCP_URL || 'https://your-apim.azure-api.net/mcp';
const SUBSCRIPTION_KEY = process.argv[3] || process.env.APIM_SUBSCRIPTION_KEY || '';
if (!SUBSCRIPTION_KEY) {
console.error('Error: Subscription key is required');
console.error('Usage: node test-mcp.js <apim-url> <subscription-key>');
console.error(' or set APIM_MCP_URL and APIM_SUBSCRIPTION_KEY environment variables');
process.exit(1);
}
// Helper to make JSON-RPC requests
async function mcpRequest(method, params = {}, id = null) {
const url = new URL(APIM_URL);
const isHttps = url.protocol === 'https:';
const client = isHttps ? https : http;
const body = JSON.stringify({
jsonrpc: '2.0',
method,
params,
id: id || Date.now().toString()
});
const options = {
hostname: url.hostname,
port: url.port || (isHttps ? 443 : 80),
path: url.pathname,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body),
'Ocp-Apim-Subscription-Key': SUBSCRIPTION_KEY
}
};
return new Promise((resolve, reject) => {
const req = client.request(options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
resolve({
status: res.statusCode,
headers: res.headers,
body: JSON.parse(data)
});
} catch (e) {
resolve({
status: res.statusCode,
headers: res.headers,
body: data
});
}
});
});
req.on('error', reject);
req.write(body);
req.end();
});
}
// Test cases
const tests = [
{
name: 'Initialize',
method: 'initialize',
params: {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: {
name: 'test-client',
version: '1.0.0'
}
}
},
{
name: 'List Tools',
method: 'tools/list',
params: {}
},
{
name: 'Get Products',
method: 'tools/call',
params: {
name: 'get_products',
arguments: {}
}
},
{
name: 'Get Products (filtered by category)',
method: 'tools/call',
params: {
name: 'get_products',
arguments: {
category: 'electronics'
}
}
},
{
name: 'Search Products',
method: 'tools/call',
params: {
name: 'search_products',
arguments: {
query: 'widget',
maxResults: 5
}
}
},
{
name: 'Get Single Product',
method: 'tools/call',
params: {
name: 'get_product',
arguments: {
productId: 'prod-001'
}
}
},
{
name: 'Create Order',
method: 'tools/call',
params: {
name: 'create_order',
arguments: {
productId: 'prod-001',
quantity: 2
}
}
},
{
name: 'Get Orders',
method: 'tools/call',
params: {
name: 'get_orders',
arguments: {}
}
},
{
name: 'Check Health',
method: 'tools/call',
params: {
name: 'check_health',
arguments: {}
}
}
];
// Run tests
async function runTests() {
console.log('============================================');
console.log('MCP Server Tests');
console.log('============================================');
console.log(`URL: ${APIM_URL}`);
console.log('============================================\n');
let passed = 0;
let failed = 0;
for (const test of tests) {
console.log(`\nš Test: ${test.name}`);
console.log(` Method: ${test.method}`);
try {
const start = Date.now();
const response = await mcpRequest(test.method, test.params);
const duration = Date.now() - start;
if (response.body.error) {
console.log(` ā FAILED (${duration}ms)`);
console.log(` Error: ${JSON.stringify(response.body.error, null, 2)}`);
failed++;
} else {
console.log(` ā
PASSED (${duration}ms)`);
console.log(` Response preview: ${JSON.stringify(response.body.result, null, 2).substring(0, 500)}...`);
passed++;
}
} catch (error) {
console.log(` ā FAILED`);
console.log(` Error: ${error.message}`);
failed++;
}
}
console.log('\n============================================');
console.log('Test Summary');
console.log('============================================');
console.log(`Total: ${tests.length}`);
console.log(`Passed: ${passed}`);
console.log(`Failed: ${failed}`);
console.log('============================================\n');
process.exit(failed > 0 ? 1 : 0);
}
runTests();