Model Context Protocol (MCP)
MCP is an open protocol, which standardizes the way applications provide context to the LLMs, Basically it provides a standard way to LLMs so that they can connect to different sources and tools.
This protocol is free to implement and use.
BUILD YOUR FIRST MCP SERVER
In this tutorial, we build a MCP server which will get the Air Quality for the given city, which will be used by Claude for Desktop or any other client to get the context by using the tools provided by the MCP server.
What are we trying to solve by using MCP server?
Currently LLM models do not have the ability to fetch the Air Quality for the city. This can be solved by MCP Server, in this case the MCP Server for Air Quality is built on top of the APIs provided by api-ninjas.
Will create a tool getAQByCity in the server which will be exposed and will be used by the host (here, Claude for Desktop).
Lets build the MCP server,
Step 1: Go to Spring.io
Step 2: Create a project in the latest spring boot version.
Step 3: Add the below dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-mcp-server-webmvc</artifactId></dependency>
Step 4: Create the api key for api-ninjas and use the air quality apis for creating the tool in MCP server.
You can use the below github repo, where I added the code.
github
Building the server
Lets build the service AirQualityService.java that uses the Air Quality APIs provided by api-ninjas.
@Service
public class AirQualityService {
private static final String BASE_URL = "https://api.api-ninjas.com/v1/airquality";
private final RestClient restClient;
private String api_key = "<Add Your API KEY Here>";
public AirQualityService() {
restClient = RestClient.builder().
baseUrl(BASE_URL)
.defaultHeader("X-Api-Key", api_key)
.build();
}
@Tool(description = "GET air quality details for a specific city")
public String getAQByCity(String city) {
var airQualityDetails = restClient.get()
.uri("?city=" + city).
retrieve().
body(AirQualityDTO.class);
String AQIText = String.format("AQI for city %s:" +
"CO: {Concentration : %s, api : %s}" +
"PM10: {Concentration : %s, api : %s}" +
"SO2: {Concentration : %s, api : %s}" +
"PM2.5: {Concentration : %s, api : %s}" +
"O3: {Concentration : %s, api : %s}" +
"NO2: {Concentration : %s, api : %s}" +
"overall_aqi: %s", city,
airQualityDetails.CO().concentration(), airQualityDetails.CO().api(),
airQualityDetails.PM10().concentration(), airQualityDetails.PM10().api(),
airQualityDetails.SO2().concentration(), airQualityDetails.SO2().api(),
airQualityDetails.PM25().concentration(), airQualityDetails.PM25().api(),
airQualityDetails.O3().concentration(), airQualityDetails.O3().api(),
airQualityDetails.NO2().concentration(), airQualityDetails.NO2().api(),
airQualityDetails.overall_aqi());
return AQIText;
}
public static void main(String[] args) {
AirQualityService airQualityService = new AirQualityService();
String res = airQualityService.getAQByCity("mumbai");
System.out.println(res);
}
}
The @Service annotation will register your service in application context and Spring Ai’s @Tool annotation will help to create the MCP tools. You can consider tools as functions which are used by MCP clients to get the context.
The auto-configuration will automatically register the tools with the MCP server.
Now create the Boot Application
@SpringBootApplication
public class AirqualityMcpApplication {
public static void main(String[] args) {
SpringApplication.run(AirqualityMcpApplication.class, args);
}
@Bean
public ToolCallbackProvider AQTools(AirQualityService airQualityService) {
return MethodToolCallbackProvider.builder().toolObjects(airQualityService).build();
}
}
MethodToolCallbackProvider will convert the tools into actionable call back, which are being used by the MCP Server.
Lets build the server by below command
mvn clean install
This will generate the airqualityMCP-0.0.1-SNAPSHOT.jar, which will be created in the target folder. Once the jar has been built successfully, we will configure the servers’s jar path in claude_desktop config file, so that the claude can use the tools defined in the server. We can also configure multiple servers.
cd ~/Library/Application\ Support/Claude/
vi claude_desktop_config.json
{
"mcpServers": {
"spring-ai-mcp-aq": {
"command": "java",
"args": [
"-Dspring.ai.mcp.server.stdio=true",
"-jar",
"/.../target/airqualityMCP-0.0.1-SNAPSHOT.jar"
]
}
}
}
The above configuration tells the Claude desktop that there is the MCP Server with name Spring-ai-mcp-aq, and to launch that use the command defined in args[].
Save the file and restart the claude desktop.
Once the server gets configured successfully, you will see the server in the list.
Lets, search the AQ for nagpur and the pollutants concentration.
The claude desktop is trying to access the configured MCP server's tool, and to use that its asking for permission. You can select Allow Once or Always allow, so that claude can get the details using the tools configured.
The above response was generated using the details returned by the Spring-ai-mcp-aq server’s tool getAQByCity which is calling api-ninjas api internally.
If you see the above response with the Air Quality and its pollutants concentration for the given city, then congratulate yourself, you have successfully built your first MCP Server.🎉
Top comments (0)