mcp-sms-ir
Version:
MCP Server for SMS.ir messaging services
132 lines (110 loc) ⢠3.63 kB
JavaScript
/**
* Simple test script for the SMS.ir MCP server
*
* Usage:
* 1. Set your API key as an environment variable:
* export SMS_IR_API_KEY=your-api-key
*
* 2. Run the test:
* node test.js
*/
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { createInterface } from 'readline';
// Get the current directory
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Path to the MCP server
const SERVER_PATH = join(__dirname, 'build', 'index.js');
// Test request IDs
const LIST_TOOLS_REQUEST_ID = 'list-tools-req';
const CHECK_CREDIT_REQUEST_ID = 'check-credit-req';
// Validate that the API key is set
if (!process.env.SMS_IR_API_KEY) {
console.error('Error: SMS_IR_API_KEY environment variable is not set');
console.error('Please set it using: export SMS_IR_API_KEY=your-api-key');
process.exit(1);
}
// Start the MCP server process
const serverProcess = spawn('node', [SERVER_PATH], {
stdio: ['pipe', 'pipe', 'inherit'],
env: process.env
});
// Create readline interface to read server output
const rl = createInterface({
input: serverProcess.stdout,
crlfDelay: Infinity
});
// Set up event listeners
rl.on('line', (line) => {
try {
const response = JSON.parse(line);
if (response.id === LIST_TOOLS_REQUEST_ID) {
console.log('\nā
Successfully received tools list:');
const tools = response.result.tools.map(tool => tool.name);
console.log(`Available tools: ${tools.join(', ')}`);
// After getting the tools list, test the check_credit tool
testCheckCredit();
}
else if (response.id === CHECK_CREDIT_REQUEST_ID) {
console.log('\nā
Successfully called check_credit tool:');
if (response.result?.content?.[0]?.text) {
const creditInfo = JSON.parse(response.result.content[0].text);
console.log('Credit response:', creditInfo);
} else {
console.log('Credit response:', response.result);
}
console.log('\nš Test completed successfully! The MCP server is working properly.');
cleanup();
}
} catch (error) {
console.error('Error parsing server response:', error);
}
});
// Function to send MCP requests to the server
function sendRequest(request) {
serverProcess.stdin.write(JSON.stringify(request) + '\n');
}
// Test function to list available tools
function testListTools() {
console.log('š Testing list_tools...');
const request = {
jsonrpc: '2.0',
id: LIST_TOOLS_REQUEST_ID,
method: 'list_tools',
params: {}
};
sendRequest(request);
}
// Test function to check account credit
function testCheckCredit() {
console.log('\nš Testing check_credit tool...');
const request = {
jsonrpc: '2.0',
id: CHECK_CREDIT_REQUEST_ID,
method: 'call_tool',
params: {
name: 'check_credit',
arguments: {}
}
};
sendRequest(request);
}
// Cleanup function to terminate the server process
function cleanup() {
console.log('\nShutting down test...');
serverProcess.kill('SIGTERM');
process.exit(0);
}
// Handle script termination
process.on('SIGINT', () => {
console.log('\nTest interrupted');
cleanup();
});
// Start the test
console.log('š Starting SMS.ir MCP server test...');
console.log(`š” Using API key: ${process.env.SMS_IR_API_KEY.substring(0, 3)}...${process.env.SMS_IR_API_KEY.substring(process.env.SMS_IR_API_KEY.length - 3)}`);
// Start testing after a brief delay to let the server initialize
setTimeout(testListTools, 1000);