Building MCP Server(SSE) with Spring Boot and AWS ECS



This content originally appeared on Level Up Coding – Medium and was authored by Vinod Dampuru

The functional value of autonomous agents depends critically on their capacity to interface with external data sources and services. To address this, the Model-Context-Protocol (MCP) provides a standardized interface, enabling seamless communication between AI models and external tools.

This technical guide will outline the engineering of a resilient MCP server using Spring Boot, which utilizes the Server-Sent Events (SSE) protocol for real-time data transfer. We will illustrate the client integration for GitHub Copilot across both local and remote AWS ECS Fargate deployments, presenting an architectural pattern that is readily adaptable for other platforms, such as AWS Bedrock Agents and custom LLM chatbots.

The complete source code for this project is available on GitHub 💻

GitHub – damvinod/spring-mcp-server-demo

There are two primary communication methods an agent can use to connect to an MCP server: STDIO and SSE.

STDIO (Standard Input/Output):

STDIO is the simplest way to establish a connection. In this model, the AI agent’s environment directly launches your MCP server application as a local child process. Communication happens through the standard I/O streams: the agent writes requests to the server’s standard input (stdin) and reads responses from its standard output (stdout).

When to use it: This method is perfect for local development and testing. Because the server runs on the same machine as the agent, it’s easy to debug and requires no network configuration. It’s an excellent starting point for building and verifying your tool’s logic.

SSE (Server-Sent Events):

SSE is a more powerful and flexible method that uses a standard web-based protocol. Here, the MCP server runs as an HTTP web server. The AI agent acts as a client, establishing a persistent HTTP connection. The server can then “push” data and updates to the agent in real time.

When to use it: SSE is designed for production and remote deployments. By hosting your MCP server on a cloud platform like AWS, you decouple it from the agent. This allows multiple different agents to securely connect to your centralized, scalable, and independently managed tool from anywhere. This is the approach we’ll focus on for building a production-ready solution. ☁

Building MCP Server with Spring Boot:

Here’s how to build an MCP server using Spring Boot. This server will provide a tool get_traffic_image that connects to Singapore's Traffic Images API (data.gov.sg) to fetch real-time camera data. When an AI agent makes a request, the server will return information about the latest traffic images.

Note: A key advantage of this approach is that you can easily extend an existing Spring Boot REST API to also serve as an MCP server. Your business logic can be exposed to AI agents without altering your current REST endpoints.
  1. Add the Dependency: First, add the Spring AI MCP WebMVC Server dependency to your build.gradle file. This library provides all the necessary components for creating an MCP server using SSE endpoints.
implementation 'org.springframework.ai:spring-ai-starter-mcp-server-webmvc'

2. Configure the Server: Next, define your server’s name and version in the application.properties file. These properties identify your tool server to connecting agents, and the full available configuration can be found here.

spring.ai.mcp.server.name=spring-mcp-server-demo
spring.ai.mcp.server.version=0.0.1

3. Define the Tool: Define your tool by creating a method and annotating it with @Tool. This annotation describes the tool's function to the AI agent. In this example, the getTrafficImage method calls the Singapore Traffic Images API endpoint using a RestClient. The JSON response from the API is automatically mapped to a TrafficApiResponse Java record.

@Service
public class TrafficImageService {

private static final String BASE_URL = "https://api.data.gov.sg/v1/transport/traffic-images";

private final RestClient restClient;

TrafficImageService() {
this.restClient = RestClient.builder()
.baseUrl(BASE_URL)
.defaultHeader("Accept", "application/geo+json")
.defaultHeader("User-Agent", "WeatherApiClient/1.0 (your@email.com)")
.build();
}

@Tool(name = "get_traffic_image", description = "Get traffic image from the camera")
public TrafficImageResponse trafficImage() {

return restClient.get()
.uri(BASE_URL)
.retrieve()
.body(TrafficImageResponse.class);
}
}

4. Register the Tool: Finally, register the service containing your tool by creating a ToolCallbackProvider bean. This makes all methods annotated @Tool within TrafficImageService available for the MCP server to execute.

@SpringBootApplication
public class SpringMcpServerDemoApplication {

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

@Bean
public ToolCallbackProvider weatherTools(TrafficImageService trafficImageService) {
return MethodToolCallbackProvider.builder().toolObjects(trafficImageService).build();
}
}

5. Run the Application: With all the components in place, the final step is to start your Spring Boot application. You can run the application directly from your IDE (like IntelliJ IDEA or VS Code) or by using the Gradle wrapper in your terminal.

From the root directory of your project, execute the following command:

./gradlew bootRun

Deploying MCP Server to AWS ECS Fargate: ☁

While running a local server is great for development, a real-world solution requires a scalable, remote server. In this section, we’ll deploy our Spring Boot MCP application to AWS using an Application Load Balancer (ALB) and ECS Fargate.

The entire cloud infrastructure can be provisioned automatically using the Terraform scripts available in the project’s GitHub repository: spring-mcp-server-demo/terraform.

Note on Security: By deploying MCP Server behind an Application Load Balancer (ALB) in AWS, we can easily enhance its security using the AWS Web Application Firewall (WAF). Simply by enabling the AWS-managed rule set for the OWASP Top 10, the WAF automatically inspects and blocks common exploits like SQL Injection and Cross-Site Scripting before they ever reach our application, providing a powerful layer of security with minimal configuration.

Provisioning the AWS Infrastructure (IaC):

You can deploy the infrastructure using one of two methods: running Terraform from your local machine or automating it with GitHub Actions.

Method 1: Run Terraform Locally

This approach is straightforward if you have the AWS CLI and Terraform installed and configured on your machine.

  1. Clone the Repository: Get a local copy of the project.
  2. Navigate to the Terraform Directory: cd spring-mcp-server-demo/terraform
  3. Initialize, Plan, and Apply: Run the standard Terraform commands to build the infrastructure in your AWS account.
terraform init 
terraform plan
terraform apply

Method 2: Automated Deployment with GitHub Actions (CI/CD)

For a fully automated workflow, you can use the GitHub Actions pipeline included in the repository.

  1. Fork the Repository: Start by forking the project to your own GitHub account.
  2. Add GitHub Secrets: In your forked repository, go to Settings > Secrets and variables > Actions and add the following secrets:
  • AWS_IAM_ROLE: The ARN of the IAM role GitHub Actions will use to deploy.
  • DOCKER_USERNAME: Your Docker Hub username.
  • DOCKER_PASSWORD: Your Docker Hub password or access token.
  • DOCKER_HUB_REGISTRY: Your Docker Hub registry name (e.g., your-docker-username/spring-mcp-server-demo

3. Update Terraform Variables: In the spring-mcp-server-demo/terraform/variables.tf file, update the docker_image variable to point to the image that the GitHub Action will build and push (e.g., your-docker-username/spring-mcp-server-demo)

4. Run the Action: Trigger the GitHub Actions workflow. It will automatically build the Docker image, push it to Docker Hub, and then run Terraform to deploy the new image to AWS ECS.

Connecting AI Agents to Remote MCP Server(SSE):

With your Spring Boot MCP server running in AWS ECS Fargate, you can connect AI agents like GitHub Copilot to use your get_traffic_image tool.

  1. Follow the steps based on the numbering in the image below to open the mcp.json

2. Add the following server definition to the mcp.json. This tells the Copilot to connect to the Remote MCP server using SSE.

{
"servers": {
"spring-mcp-server-demo-alb": {
"url": "http://<ALB_DOMAIN>.ap-southeast-1.elb.amazonaws.com/sse"
}
}
}

3. After saving the file, the following logs should appear, which show the connection is successful.

4. Key in the prompt, Get current traffic images from the first cameras and this should be able to connect to the Remove MCP server and display the details as shown below.

Connecting AI Agents to Local MCP Server (SSE):

With your Spring Boot MCP server running, you can connect AI agents like GitHub Copilot to use your get_traffic_image tool. Since we are focusing on a network-based connection, we will configure the agent to communicate with our server's SSE (Server-Sent Events) endpoint.

  1. Follow the steps based on the numbering in the image below to open the mcp.json

2. Add the following server definition to the mcp.json. This tells the Copilot to connect to the MCP server running locally using SSE.

{
"servers": {
"spring-mcp-server-demo-sse": {
"url": "http://localhost:8080/sse"
}
}
}

3. After saving the file, the following logs should appear, which show the connection is successful.

4. Key in the prompt, Get current traffic images from the first cameras and this should be able to connect to the Local MCP server and display the details as shown below.


Building MCP Server(SSE) with Spring Boot and AWS ECS 🚀 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding – Medium and was authored by Vinod Dampuru