The `testStartAwaberryMcp.js` script leverages `dotenv` for environment variable management, specifically requiring `deviceName`. It orchestrates a test sequence through `sendMcpRequest` to an MCP server. This includes initializing a protocol session, listing available tools, establishing a secure connection to a specified device (obtaining `sessionToken` and `deviceuuid`), and finally executing a remote terminal command like `date` to confirm operational integrity. Each step is validated, with failure at any point leading to process termination.
The testStartAwaberryMcp.js script serves as a comprehensive integration test for an MCP (Master Control Program) server. It validates the end-to-end communication flow, from initial handshake to executing commands on a connected device. The script is written in Node.js and uses ES module syntax.
The script relies on environment variables loaded from a .env file located two directories up from the script's location (../../.env). It uses the dotenv library to achieve this. A critical environment variable, deviceName, must be present; otherwise, the script will terminate with an error. This variable specifies the target device for connection.
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envFilePath = path.resolve(__dirname, '../../.env');
const result = dotenv.config({ path: envFilePath });
if (result.error) {
console.error('❌ [MCP Test] Error loading .env file:', result.error);
process.exit(1);
}
if (!process.env.deviceName) {
console.error('\n❌ [MCP Test] Missing required environment variable: deviceName');
process.exit(1);
}
The testMcpServer asynchronous function encapsulates the entire testing process, divided into four distinct stages:
Sends an initialize request to the MCP server with a specified protocol version, empty capabilities, and client information. This step establishes the basic communication session. Failure to initialize results in script termination.
const initResponse = await sendMcpRequest('initialize', {
protocolVersion: '2024-11-05',
capabilities: {},
clientInfo: { name: 'test-client', version: '1.0.0' }
});
After successful initialization, the script requests a list of available tools from the MCP server using the tools/list endpoint. It verifies that the response contains a tools array. The number of tools found is logged.
const toolsResponse = await sendMcpRequest('tools/list', {});
This crucial step calls the connect_to_device tool on the MCP server, passing the deviceName obtained from the environment variables. The MCP server automatically injects necessary credentials. Upon successful connection, the script extracts sessionToken and deviceuuid from the response, which are essential for subsequent authenticated operations.
const connectResponse = await sendMcpRequest('tools/call', {
name: 'connect_to_device',
arguments: { deviceName: process.env.deviceName }
});
// ... extraction of sessionToken and deviceuuid
Finally, the script utilizes the obtained sessionToken and deviceuuid to call the execute_terminal_command tool. It sends a simple date command to the connected device. The response is parsed to check for a success flag, confirming the command's execution status on the remote device.
const executeResponse = await sendMcpRequest('tools/call', {
name: 'execute_terminal_command',
arguments: {
sessionToken,
deviceuuid,
command: 'date'
}
});
The script incorporates robust error handling at each stage. If any sendMcpRequest fails or returns an unexpected result, an error message is logged to the console, and the process exits with a non-zero status code, indicating a test failure. A general try-catch block also handles any unexpected exceptions during the execution of testMcpServer.