Hands-On: Building MCP Servers and Clients in Java

Hands-On: Building MCP Servers and Clients in Java

Introduction

The Model Context Protocol (MCP) has emerged as a standard way to connect AI models with external tools and data sources. If you’re a Java developer looking to integrate MCP into your applications, this hands-on guide will walk you through building both servers and clients.

MCP enables LLMs to interact with your Java services in a standardized way, making it easier to expose tools, resources, and prompts to AI applications.

What is MCP?

MCP is an open protocol that standardizes how applications provide context to LLMs. It defines a client-server architecture where:

Setting Up Your Java Project

Let’s start with a Spring Boot project structure:

1
2
3
4
5
6
7
8
9
10
11
12
// pom.xml dependencies
<dependencies>
<dependency>
<groupId>io.modelcontextprotocol</groupId>
<artifactId>mcp-server-sdk</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

Building an MCP Server

Here’s how to create a basic MCP server in Java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
public class McpServerController {

@PostMapping("/mcp/tools")
public ResponseEntity<List<Tool>> getTools() {
List<Tool> tools = Arrays.asList(
new Tool("get_weather", "Get weather information")
);
return ResponseEntity.ok(tools);
}

@PostMapping("/mcp/call")
public ResponseEntity<CallToolResult> callTool(@RequestBody CallToolRequest request) {
// Implement tool logic
return ResponseEntity.ok(new CallToolResult("sunny"));
}
}

Creating an MCP Client

The client connects to servers and invokes tools:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Component
public class McpClient {

private final WebClient webClient;

public McpClient(WebClient.Builder builder) {
this.webClient = builder.baseUrl("http://localhost:8080").build();
}

public String callTool(String toolName, Map<String, Object> arguments) {
return webClient.post()
.uri("/mcp/call")
.bodyValue(new CallToolRequest(toolName, arguments))
.retrieve()
.bodyToMono(CallToolResult.class)
.block();
}
}

Key Takeaways