#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import axios from 'axios';
import dotenv from 'dotenv';
// Load environment variables
dotenv.config();
// Miele API configuration
const MIELE_API_BASE_URL = process.env.MIELE_API_BASE_URL || 'https://api.mcs3.miele.com/v1';
const MIELE_ACCESS_TOKEN = process.env.MIELE_ACCESS_TOKEN;
// Create axios instance for Miele API
const mieleApi = axios.create({
baseURL: MIELE_API_BASE_URL,
headers: {
'Authorization': `Bearer ${MIELE_ACCESS_TOKEN}`,
'Content-Type': 'application/json',
},
});
// Create MCP server
const server = new Server(
{
name: 'miele-home-controller',
version: '1.0.0',
},
{
capabilities: {
tools: {},
},
}
);
// List available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: [
{
name: 'get_devices',
description: 'Get all Miele devices connected to your account',
inputSchema: {
type: 'object',
properties: {},
},
},
{
name: 'get_device_status',
description: 'Get detailed status information for a specific Miele device',
inputSchema: {
type: 'object',
properties: {
deviceId: {
type: 'string',
description: 'The ID of the device to get status for',
},
},
required: ['deviceId'],
},
},
{
name: 'device_action',
description: 'Perform an action on a Miele device (start, stop, pause, etc.)',
inputSchema: {
type: 'object',
properties: {
deviceId: {
type: 'string',
description: 'The ID of the device to control',
},
action: {
type: 'string',
description: 'The action to perform (e.g., "start", "stop", "pause")',
enum: ['start', 'stop', 'pause', 'powerOn', 'powerOff'],
},
},
required: ['deviceId', 'action'],
},
},
],
};
});
// Handle tool execution
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case 'get_devices': {
const response = await mieleApi.get('/devices');
return {
content: [
{
type: 'text',
text: JSON.stringify(response.data, null, 2),
},
],
};
}
case 'get_device_status': {
const { deviceId } = args;
const response = await mieleApi.get(`/devices/${deviceId}`);
return {
content: [
{
type: 'text',
text: JSON.stringify(response.data, null, 2),
},
],
};
}
case 'device_action': {
const { deviceId, action } = args;
const response = await mieleApi.put(`/devices/${deviceId}/actions`, {
processAction: action,
});
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
action: action,
deviceId: deviceId,
response: response.data,
}, null, 2),
},
],
};
}
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
return {
content: [
{
type: 'text',
text: JSON.stringify({
error: true,
message: error.message,
details: error.response?.data || 'No additional details',
}, null, 2),
},
],
isError: true,
};
}
});
// Start the server
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('Miele Home Controller MCP server running on stdio');
}
main().catch((error) => {
console.error('Fatal error in main():', error);
process.exit(1);
});