Introduction
MCP (Model Context Protocol) from Anthropic and now an open protocol. Allows LLMs to use external tools and disparate data sources. In this short tutorial, I’ll walk you through a rather silly impractical exercise that will demonstrate how to build a MCP server with Spring AI that can lock your screen.
Lets get started
To start head to Spring Starter .
The only dependency you need is Model Context Protocol Server
Generate and unzip the project and open it in your favorite IDE.
Adding the Service to lock your screen
Note that this only works on MacOS, but you can adapt it to your OS of choice with the right commands to accomplish the same thing.
The service is:
@Service
public class LockScreenService {
@Tool(name = "cn_lock_screen", description = "Lock work computer screen")
public Boolean lockScreen() {
try {
ProcessBuilder processBuilder = new ProcessBuilder("pmset", "displaysleepnow");
Process process = processBuilder.start();
process.waitFor();
return true;
} catch (Exception e) {
return false;
}
}
}
Then you need to register the service in the MCP server:
@SpringBootApplication
public class McpMacLockscreenApplication {
public static void main(String[] args) {
SpringApplication.run(McpMacLockscreenApplication.class, args);
}
@Bean
public List<ToolCallback> mcpTools(LockScreenService lockScreenService) {
return List.of(ToolCallbacks.from(lockScreenService));
}
}
Now build and package the application
You can use the following command to build and package the application:
cd myproject
./mvnw clean package
Configuring claude desktop to use the MCP server
Now you’ll need to configure the claude desktop to use the MCP server you just created. You’ll need to specify where your Java executable is located and also your jar file for the MCP server.
Open ~/Library/Application\ Support/Claude/claude_desktop_config.json
with your favourite editor.
If you don’t have this file, you’ll need to open the Claude Desktop app and go to settings from your Mac menu bar then go to developer tab and hit ‘Edit config’.
That will open finder, and you’ll see the file. Put the below text into it, but modify to your project specifics:
{
"mcpServers": {
"mac-lockscreen": {
"command": "/path/to/java",
"args": [
"-jar",
"/path/to/your/target/*-SNAPSHOT.jar"
]
}
}
}
How to use it
Now to use your new-found MCP server that will lock your screen go into Claude Desktop. Start a new chat and type the following:
lock my work computer screen
Claude will be able to reach out to your MCP server and lock the screen for you and MCP server will return true
if it was successful or false
if it failed to lock the screen.
That’s it!
My initial goal with this, was whether I’d be able to use Claude Mobile App to lock my screen If I was away and I forgot. I know it’s a silly idea, but it would be fun and I wanted to present it to my colleagues as a use of MCP. Unfortunately, the mobile app doesn’t support MCP at this point in time.