This project demonstrates a web application that uses content safety checking before passing user prompts to a calculator service via Model Context Protocol (MCP).
- User Input: The user enters a calculation prompt in the web interface
- Content Safety Screening (Input): The prompt is analyzed by Azure Content Safety API
- Safety Decision (Input):
- If the content is safe (severity < 2 in all categories), it proceeds to the calculator
- If the content is flagged as potentially harmful, the process stops and returns a warning
- Calculator Integration: Safe content is processed by LangChain4j, which communicates with the MCP calculator server
- Content Safety Screening (Output): The bot's response is analyzed by Azure Content Safety API
- Safety Decision (Output):
- If the bot response is safe, it's shown to the user
- If the bot response is flagged as potentially harmful, it's replaced with a warning
- Response: Results (if safe) are displayed to the user along with both safety analyses
This project demonstrates how to use Model Context Protocol (MCP) to call calculator MCP services from LangChain4j. The implementation uses a local MCP server running on port 8080 to provide calculator operations.
Before using the content safety features, you need to create an Azure Content Safety service resource:
- Sign in to the Azure Portal
- Click "Create a resource" and search for "Content Safety"
- Select "Content Safety" and click "Create"
- Enter a unique name for your resource
- Select your subscription and resource group (or create a new one)
- Choose a supported region (check Region availability for details)
- Select an appropriate pricing tier
- Click "Create" to deploy the resource
- Once deployment is complete, click "Go to resource"
- In the left pane, under "Resource Management", select "Keys and Endpoint"
- Copy either of the keys and the endpoint URL for use in the next step
Set the GITHUB_TOKEN environment variable for GitHub models authentication:
export GITHUB_TOKEN=<your_github_token>For content safety features, set:
export CONTENT_SAFETY_ENDPOINT=<your_content_safety_endpoint>
export CONTENT_SAFETY_KEY=<your_content_safety_key>These environment variables are used by the application to authenticate with the Azure Content Safety service. If these variables are not set, the application will use placeholder values for demonstration purposes, but the content safety features will not work properly.
Before running the client, you need to start the calculator MCP server in SSE mode on localhost:8080.
This project demonstrates the integration of Model Context Protocol (MCP) with LangChain4j to call calculator services. Key features include:
- Using MCP to connect to a calculator service for basic math operations
- Dual-layer content safety checking on both user prompts and bot responses
- Integration with GitHub's gpt-4.1-nano model via LangChain4j
- Using Server-Sent Events (SSE) for MCP transport
The project includes comprehensive content safety features to ensure that both user inputs and system responses are free from harmful content:
-
Input Screening: All user prompts are analyzed for harmful content categories such as hate speech, violence, self-harm, and sexual content before processing.
-
Output Screening: Even when using potentially uncensored models, the system checks all generated responses through the same content safety filters before displaying them to the user.
This dual-layer approach ensures that the system remains safe regardless of which AI model is being used, protecting users from both harmful inputs and potentially problematic AI-generated outputs.
The application includes a user-friendly web interface that allows users to interact with the Content Safety Calculator system:
- Simple, intuitive form for entering calculation prompts
- Dual-layer content safety validation (input and output)
- Real-time feedback on prompt and response safety
- Color-coded safety indicators for easy interpretation
- Clean, responsive design that works on various devices
- Example safe prompts to guide users
-
Start the application:
mvn spring-boot:run
-
Open your browser and navigate to
http://localhost:8087 -
Enter a calculation prompt in the provided text area (e.g., "Calculate the sum of 24.5 and 17.3")
-
Click "Submit" to process your request
-
View the results, which will include:
- Content safety analysis of your prompt
- The calculated result (if prompt was safe)
- Content safety analysis of the bot's response
- Any safety warnings if either the input or output was flagged
The web client automatically handles both content safety verification processes, ensuring all interactions are safe and appropriate regardless of which AI model is being used.
