使用说明
Spring AI MCP Lightbulb Controller
A demonstration project showcasing how to build an MCP (Model Context Protocol) server using Spring AI and Kotlin to control a smart lightbulb via AI assistants.
Overview
This project creates an MCP server that exposes tools for controlling a smart lightbulb. It can be used by AI assistants (such as Cursor, Claude Desktop, or other MCP-compatible clients) to interact with IoT devices through natural language commands.
Features
- MCP Server - Exposes lightbulb control as AI-callable tools
- Smart Lightbulb Integration - Controls lightbulbs via HTTP API (compatible with Sonoff DIY mode and similar devices)
- Spring Boot 4.1 - Built on modern Spring Boot with Kotlin
- Spring AI 2.0 - Leverages Spring AI's MCP server support
Available MCP Tools
| Tool | Description |
|---|---|
pulse |
Pulse a lightbulb for 500 ms |
pulseNTimes |
Pulse a lightbulb n times for 500 ms each |
Configuration
Lightbulb Connection
Configure the lightbulb base URL in application.properties:
app.lightbulb.base-url=http://ewelink_10008fe75b.local:8081
Note: Some MCP clients may have trouble resolving mDNS hostnames. If you experience connection issues, use the IP address instead of the
.localhostname.
To get the IP address from the mDNS hostname:
ping -c 1 -a ewelink_10008fe75b.local
MCP Server Settings
The MCP server is configured in application.properties:
spring.ai.mcp.server.protocol=streamable
spring.ai.mcp.server.streamable-http.mcp-endpoint=/mcp
Running the Application
./gradlew bootRun
The MCP server will be available at http://localhost:8080/mcp
Using with AI Assistants
Cursor IDE
Add the following to your MCP settings:
{
"mcpServers": {
"lightbulb": {
"url": "http://localhost:8080/mcp"
}
}
}
Once configured, you can ask the AI assistant to "pulse the lightbulb" or "pulse the lightbulb 3 times".
Sonoff DIY Mode Setup
Initial Setup
- Restart Sonoff device - it should blink every 1s
- Connect to
ITEAD-xxxxxWiFi network using password12345678 - Open http://10.10.7.1/ to configure the device
API Examples
Switch
POST http://ewelink_10008fe75b.local:8081/zeroconf/switch
Content-Type: application/json
{
"deviceId": "10008fe75b",
"data": {
"switch": "on"
}
}
Pulse (multiples of 500ms)
POST http://ewelink_10008fe75b.local:8081/zeroconf/pulse
Content-Type: application/json
{
"deviceid": "10008fe75b",
"data": {
"pulse": "on",
"pulseWidth": 500
}
}
License
See LICENSE file.