top of page

Welcome
to NumpyNinja Blogs

NumpyNinja: Blogs. Demystifying Tech,

One Blog at a Time.
Millions of views. 

From Hallucination to Precision: Building a Reliable AI Meal Planner with Spring AI's MCP


Artificial Intelligence, especially Large Language Models (LLMs), holds incredible promise for creating personalized user experiences. At a recent project, we aimed to harness this power to generate custom meal plans for patients based on their unique health conditions, dietary preferences, and restrictions. The goal was simple: an AI-powered dietician’s assistant.

Our initial approach was straightforward: send a detailed prompt to an LLM like GPT-4 and get a meal plan back. But we quickly ran into the classic LLM pitfalls.


The Problem: When Your AI Chef Goes Rogue

LLMs are masters of language and creativity, but they lack a true understanding of facts and consequences. This led to several critical issues:

  1. Dangerous Hallucinations: The model would sometimes “hallucinate” and include ingredients that were explicitly forbidden. A plan for a patient with a severe peanut allergy might include a peanut-based snack — a serious safety risk.

  2. Repetitive and Uninspired Plans: The LLM often fell into patterns, suggesting the same meals repeatedly, leading to a poor user experience.

  3. The Automation Paradox: If a human dietician had to manually review and correct every single plan, the purpose of our AI automation was defeated.

We needed to ground the LLM in a source of truth. This is where Spring AI’s Model Context Protocol(MCP) architecture changed the game.


The Solution: Separating Creativity from Facts with MCP

The core idea behind our new approach was to separate responsibilities. We would let the LLM do what it does best — structure content creatively — but force it to source its core facts (the ingredients) from a reliable, controlled source.

We implemented an MCP Client-Server model:

  • MCP Client (Our Main Application): The Spring Boot app that interacts with the LLM. It manages the conversation and directs the LLM to use external “Tools.”

  • MCP Server (Our Data & Logic Hub): A separate service that exposes our business logic and database connections as “Tools.”

This communication is typically handled via an SSE (Server-Sent Events) connection, which allows the server to efficiently push tool information and responses to the client whenever the LLM requests them.


Building the MCP Client

First, we set up our main Spring Boot application as the MCP client. This required two key dependencies in our pom.xml.

  1. Spring AI for the LLM Connection:









2. Spring AI for the MCP Client:









  1. This starter auto-configures the client machinery to discover and invoke tools exposed by MCP servers.


Here’s a look at our MealPlanService. Note how we now use a single prompt. The LLM intelligently decides on its own to call the tool first to get the ingredients before generating the plan.































Understanding the Key Components

  • ChatClient: Your main gateway to the LLM. It provides a fluent API for building prompts, configuring tools, managing memory, and sending requests.

  • Prompt: A structured message sent to the model, containing user input, system instructions, and conversation history.

  • ChatMemory: The AI’s short-term memory, which stores recent exchanges to maintain context.

  • ChatResponse: A structured object containing the LLM’s full reply, including the text content and any metadata about tool calls.


Building the MCP Server: The Guardian of Facts

The server’s job is to expose our trusted data and logic. We started by adding the MCP server dependency to its pom.xml:









This powerful starter turns a Spring Boot application into an MCP server, automatically exposing REST endpoints (/sse/mcp/tools, /sse/mcp/call) for tool discovery and invocation.


On the server, we defined our Tools. These are simple Java methods annotated to be discoverable by the MCP framework. They connect to our curated database, which is built from the USDA dataset and stripped of processed foods.


























By defining these tools, we created a secure boundary. The LLM can no longer invent ingredients. It must call one of these functions to get its food list.


The Final Result: Safe and Creative Meal Plans

With this architecture, the LLM now produces reliable outputs. For a “Low Fat Vegan” request, the final response is built only from the ingredients provided by our tool:


LLM Response:












No peanuts. No random ingredients. Just a perfect plan built from a foundation of truth. By adopting Spring AI’s MCP architecture, we transformed our application from a risky novelty into a reliable tool.


Flowchart:



In the rapidly evolving world of AI, it’s clear that the real power lies not just in the intelligence of the model, but in the robustness of the architecture surrounding it. By implementing Spring AI’s MCP client-server model, we transformed a potentially unreliable tool into a trustworthy assistant. We successfully tamed the LLM’s tendency to hallucinate by grounding it in a verifiable source of truth. Ultimately, we didn’t just build an AI feature; we built a system that bridges the gap between creative potential and factual reliability, paving the way for safer and more powerful AI-powered applications.












 
 

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2025 by Numpy Ninja Inc.

  • Twitter
  • LinkedIn
bottom of page