/**
* Basic Usage Example for MCP OpenClaw
*
* This example demonstrates how to use the OpenClaw client directly
* (not through the MCP server interface).
*/
import { OpenClawClient } from '../src/openclaw-client';
import type {
SendMessageParams,
ExecuteCommandParams,
CreateCalendarEventParams,
SendEmailParams,
} from '../src/types';
async function main() {
// Initialize the client
const client = new OpenClawClient({
apiUrl: process.env.OPENCLAW_API_URL || 'https://api.openclaw.example.com',
apiKey: process.env.OPENCLAW_API_KEY || 'your-api-key',
timeout: 30000,
});
console.log('=== MCP OpenClaw Basic Usage Examples ===\n');
// Example 1: Send a message
console.log('1. Sending a message to Telegram...');
const messageParams: SendMessageParams = {
platform: 'telegram',
recipient: '@example_user',
message: 'Hello from OpenClaw!',
};
const messageResult = await client.sendMessage(messageParams);
console.log('Result:', JSON.stringify(messageResult, null, 2));
console.log('');
// Example 2: Execute a command synchronously
console.log('2. Executing a command...');
const commandParams: ExecuteCommandParams = {
command: 'echo "Hello, World!"',
timeout: 30,
async: false,
};
const commandResult = await client.executeCommand(commandParams);
console.log('Result:', JSON.stringify(commandResult, null, 2));
console.log('');
// Example 3: Execute a command asynchronously
console.log('3. Executing an async command...');
const asyncCommandParams: ExecuteCommandParams = {
command: 'sleep 5 && echo "Done"',
timeout: 60,
async: true,
};
const asyncCommandResult = await client.executeCommand(asyncCommandParams);
console.log('Result:', JSON.stringify(asyncCommandResult, null, 2));
// Poll for task status if we got a task ID
if (asyncCommandResult.success && asyncCommandResult.taskId) {
const taskId = asyncCommandResult.taskId;
console.log(`\n4. Polling task status for ${taskId}...`);
let status = await client.getTaskStatus({ taskId });
while (status.status === 'running' || status.status === 'pending') {
console.log(` Status: ${status.status}`);
await new Promise((resolve) => setTimeout(resolve, 2000));
status = await client.getTaskStatus({ taskId });
}
console.log('Final Status:', JSON.stringify(status, null, 2));
}
console.log('');
// Example 5: Create a calendar event
console.log('5. Creating a calendar event...');
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
tomorrow.setHours(10, 0, 0, 0);
const eventParams: CreateCalendarEventParams = {
title: 'Team Meeting',
description: 'Weekly team sync',
startTime: tomorrow.toISOString(),
endTime: new Date(tomorrow.getTime() + 60 * 60 * 1000).toISOString(),
location: 'Conference Room A',
reminder: 15,
};
const eventResult = await client.createCalendarEvent(eventParams);
console.log('Result:', JSON.stringify(eventResult, null, 2));
console.log('');
// Example 6: Send an email
console.log('6. Sending an email...');
const emailParams: SendEmailParams = {
to: 'recipient@example.com',
subject: 'Test Email from OpenClaw',
body: 'This is a test email sent via OpenClaw.',
html: false,
};
const emailResult = await client.sendEmail(emailParams);
console.log('Result:', JSON.stringify(emailResult, null, 2));
console.log('');
// Example 7: Health check
console.log('7. Checking API health...');
const healthResult = await client.healthCheck();
console.log('Health:', JSON.stringify(healthResult, null, 2));
// Clean up
client.close();
}
// Run the examples
main().catch(console.error);