Sponsored by Deepsite.site

Weather Bridge MCP

Created By
mm-camelcase7 months ago
A Spring Boot implementation of the Model Context Protocol (MCP) that enables AI agents like GitHub Copilot to access weather data. Demonstrates building a custom MCP server that connects AI models to weather APIs through standardised tools, complete with VS Code integration examples.
Content

Weather Bridge MCP

A Spring Boot implementation of the Model Context Protocol (MCP) that enables AI agents like GitHub Copilot to access weather data. Demonstrates building a custom MCP server that connects AI models to weather APIs through standardised tools, complete with VS Code integration examples.

Overview

This project demonstrates how to build a custom MCP server using Spring Boot that allows AI agents like GitHub Copilot to access weather data. The server exposes weather data through MCP tools that GitHub Copilot can use in agent mode to answer questions about weather conditions, forecasts, and alerts.

MCP Architecture

MCP ARCHITECTURE DIAGRAM

Project Structure

weather-bridge-mcp/
├── src/
│   ├── main/
│   │   ├── java/com/example/weatherbridgemcp/
│   │   │   ├── WeatherBridgeMcpApplication.java  # Main Spring Boot application
│   │   │   ├── config/
│   │   │   │   ├── McpServerConfig.java          # MCP server configuration
│   │   │   │   └── AppConfig.java                # General application config
│   │   │   ├── model/
│   │   │   │   ├── WeatherData.java              # Weather data model classes
│   │   │   │   └── ForecastData.java             # Forecast data model classes
│   │   │   ├── service/
│   │   │   │   └── WeatherService.java           # Service to call external Weather API
│   │   │   ├── mcp/
│   │   │   │   └── WeatherToolProvider.java      # MCP tool provider implementation
│   │   │   └── controller/
│   │   │       └── WeatherController.java        # Optional REST controller for testing
│   │   └── resources/
│   │       └── application.properties            # App configuration properties
│   └── test/                                     # Test classes
├── pom.xml                                       # Maven configuration
└── vscode-config/                                # VS Code configuration examples
    └── mcp.json                                  # Example MCP configuration for VS Code

TODO List

This is a project template. To complete the implementation, follow these steps:

  1. Set up the basic Spring Boot project using Spring Initializer
  2. Add Spring AI MCP dependencies to the pom.xml
  3. Implement the Weather API client (WeatherService.java)
  4. Configure the MCP server (McpServerConfig.java)
  5. Implement MCP tools (WeatherToolProvider.java)
  6. Create model classes for weather data
  7. Configure VS Code to connect to your MCP server
  8. Test with GitHub Copilot agent mode

Implementation Guide

Step 1: Set up Spring Boot Project

Create a new Spring Boot project using Spring Initializer (https://start.spring.io/) with the following settings:

  • Project: Maven
  • Language: Java
  • Spring Boot: Latest stable version
  • Dependencies: Spring Web, Spring Boot DevTools, Lombok (optional)

Step 2: Add MCP Dependencies

Add the following dependencies to your pom.xml:

<dependencies>
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Spring AI MCP -->
    <dependency>
        <groupId>org.springframework.experimental</groupId>
        <artifactId>spring-ai-mcp-core</artifactId>
        <version>0.1.0</version>
    </dependency>
    
    <!-- WebMVC SSE transport -->
    <dependency>
        <groupId>org.springframework.experimental</groupId>
        <artifactId>mcp-webmvc-sse-transport</artifactId>
        <version>0.1.0</version>
    </dependency>
    
    <!-- JSON processing -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>
    
    <!-- Lombok for reducing boilerplate code -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Step 3: Implement Weather Service

Create a service to interact with an external weather API (like OpenWeatherMap):

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.beans.factory.annotation.Value;

@Service
public class WeatherService {
    private final RestTemplate restTemplate;
    private final String apiKey;
    private final String baseUrl;

    public WeatherService(
            RestTemplate restTemplate,
            @Value("${openweathermap.api-key}") String apiKey,
            @Value("${openweathermap.base-url}") String baseUrl) {
        this.restTemplate = restTemplate;
        this.apiKey = apiKey;
        this.baseUrl = baseUrl;
    }

    public WeatherData getCurrentWeather(String city) {
        String url = String.format("%s/weather?q=%s&appid=%s&units=metric", 
                                  baseUrl, city, apiKey);
        return restTemplate.getForObject(url, WeatherData.class);
    }

    public ForecastData getForecast(String city, int days) {
        String url = String.format("%s/forecast?q=%s&appid=%s&units=metric&cnt=%d", 
                                  baseUrl, city, apiKey, days * 8); // 8 forecasts per day (3-hour steps)
        return restTemplate.getForObject(url, ForecastData.class);
    }
}

Step 4: Configure MCP Server

Set up the MCP server configuration:

import org.springframework.ai.mcp.server.McpServer;
import org.springframework.ai.mcp.server.transport.http.HttpServletSseServerTransportProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.web.servlet.ServletRegistrationBean;

@Configuration
@EnableWebMvc
public class McpServerConfig implements WebMvcConfigurer {

    @Bean
    public McpServer mcpServer(WeatherToolProvider weatherToolProvider) {
        return McpServer.builder()
                .withToolProvider(weatherToolProvider)
                .build();
    }

    @Bean
    public HttpServletSseServerTransportProvider servletSseServerTransportProvider() {
        return new HttpServletSseServerTransportProvider(new ObjectMapper(), "/mcp/sse");
    }

    @Bean
    public ServletRegistrationBean<HttpServletSseServerTransportProvider> sseServlet(
            HttpServletSseServerTransportProvider transportProvider) {
        return new ServletRegistrationBean<>(transportProvider, "/mcp/sse");
    }
}

Step 5: Implement MCP Tools

Create the MCP tool provider to expose weather tools:

import org.springframework.ai.mcp.server.tool.McpServerToolProvider;
import org.springframework.ai.mcp.server.tool.McpServerToolType;
import org.springframework.ai.mcp.server.tool.McpServerTool;
import org.springframework.stereotype.Component;

@Component
@McpServerToolType(name = "weather", description = "Tools for retrieving weather information")
public class WeatherToolProvider implements McpServerToolProvider {

    private final WeatherService weatherService;

    public WeatherToolProvider(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    @McpServerTool(
        name = "getCurrentWeather",
        description = "Get the current weather for a specific city",
        inputSchema = "{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"}},\"required\":[\"city\"]}"
    )
    public WeatherData getCurrentWeather(String city) {
        return weatherService.getCurrentWeather(city);
    }

    @McpServerTool(
        name = "getForecast",
        description = "Get the weather forecast for a specific city for a number of days",
        inputSchema = "{\"type\":\"object\",\"properties\":{\"city\":{\"type\":\"string\"},\"days\":{\"type\":\"integer\",\"minimum\":1,\"maximum\":5}},\"required\":[\"city\",\"days\"]}"
    )
    public ForecastData getForecast(String city, int days) {
        return weatherService.getForecast(city, days);
    }
}

Step 6: Create Model Classes

Create model classes for weather data:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.util.List;

@Data
public class WeatherData {
    private Coordinates coord;
    private List<Weather> weather;
    private String base;
    private MainData main;
    private long visibility;
    private Wind wind;
    private Clouds clouds;
    private long dt;
    private Sys sys;
    private int timezone;
    private long id;
    private String name;
    private int cod;

    @Data
    public static class Coordinates {
        private double lon;
        private double lat;
    }

    @Data
    public static class Weather {
        private long id;
        private String main;
        private String description;
        private String icon;
    }

    @Data
    public static class MainData {
        private double temp;
        @JsonProperty("feels_like")
        private double feelsLike;
        @JsonProperty("temp_min")
        private double tempMin;
        @JsonProperty("temp_max")
        private double tempMax;
        private int pressure;
        private int humidity;
    }

    @Data
    public static class Wind {
        private double speed;
        private int deg;
        private double gust;
    }

    @Data
    public static class Clouds {
        private int all;
    }

    @Data
    public static class Sys {
        private int type;
        private long id;
        private String country;
        private long sunrise;
        private long sunset;
    }
}

Step 7: Main Application Class

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class WeatherBridgeMcpApplication {

    public static void main(String[] args) {
        SpringApplication.run(WeatherBridgeMcpApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

Step 8: Configure Application Properties

Create an application.properties file:

server.port=8080
openweathermap.api-key=YOUR_API_KEY_HERE
openweathermap.base-url=https://api.openweathermap.org/data/2.5

Step 9: VS Code Integration

Create a .vscode/mcp.json file in your VS Code workspace:

{
  "inputs": [],
  "servers": {
    "WeatherBridgeMcp": {
      "type": "sse",
      "url": "http://localhost:8080/mcp/sse"
    }
  }
}

Running the Project

  1. Get an API key from OpenWeatherMap (or another weather API)
  2. Add your API key to application.properties
  3. Build and run the Spring Boot application
  4. Open VS Code and configure it with the MCP settings
  5. Use GitHub Copilot agent mode to ask weather-related questions

Testing in GitHub Copilot

Once your server is running, you can test it with GitHub Copilot by:

  1. Opening GitHub Copilot
  2. Activating Agent Mode
  3. Selecting your Weather MCP tools
  4. Asking questions like "What's the current weather in New York?" or "Give me the 5-day forecast for London"

Resources

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Tavily Mcp
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
CursorThe AI Code Editor
WindsurfThe new purpose-built IDE to harness magic
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
ChatWiseThe second fastest AI chatbot™
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
Serper MCP ServerA Serper MCP Server
Playwright McpPlaywright MCP server
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Amap Maps高德地图官方 MCP Server
DeepChatYour AI Partner on Desktop
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.