{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreigs6kk54l7mmh7zsxipzbd5xsa4od4aogywguqg7alewfltab22cy",
    "uri": "at://did:plc:25rdn5elo5izoxrmtis34zuk/app.bsky.feed.post/3mozn62d2snp2"
  },
  "coverImage": {
    "$type": "blob",
    "ref": {
      "$link": "bafkreicccseqcstgyu33q3tvx2mkgsnnvvrm64jmtzuonhnfawqpcmlbqa"
    },
    "mimeType": "image/webp",
    "size": 50528
  },
  "path": "/ykpraveen/building-an-ai-chat-agent-with-mcp-spring-ai-f0n",
  "publishedAt": "2026-06-24T09:41:20.000Z",
  "site": "https://dev.to",
  "tags": [
    "ai",
    "spring",
    "mcp",
    "react",
    "GitHub",
    "Source code on GitHub",
    "Spring AI MCP documentation",
    "Model Context Protocol specification",
    "Google Gemini API documentation",
    "@McpTool",
    "@Component",
    "@McpToolParam",
    "@Service",
    "@Configuration",
    "@Bean"
  ],
  "textContent": "Model Context Protocol (MCP) is an open standard for connecting AI apps to tools and data sources. A useful way to think about it is as a USB-C port for AI: one standard interface that lets different models plug into different capabilities without custom glue code for every integration.\n\nIn this project, we combine MCP, Spring AI, and Google Gemini to build a chat app that can answer weather questions using real tools instead of hallucinating. The system has three parts:\n\n  * **MCP tool server** - a Spring Boot service that exposes weather and geocoding tools\n  * **AI chat agent** - a Spring Boot service that uses Spring AI + Gemini and calls MCP tools when needed\n  * **React chat UI** - a lightweight frontend for sending messages and rendering replies\n\n\n\nThe result is a small but realistic architecture you can extend into a production assistant.\n\n##  Architecture\n\n\n    User (Browser:3000)\n        | POST /api/chat\n        v\n    AI Agent (Spring:7171) -- MCP / Streamable HTTP --> MCP Server (Spring:7170)\n        |                                               |\n        | Google Gemini                                 | Bright Sky API (weather)\n        |                                               | OpenStreetMap Nominatim (geocoding)\n        v                                               v\n    Chat response                                    Tool execution\n\n\nThe full source code is available on GitHub.\n\n##  1. The MCP Tool Server\n\nThe tool server is a Spring Boot application that exposes MCP tools through Spring AI's annotation scanner. It runs on port `7170` and uses Streamable HTTP for transport.\n\n###  Dependencies\n\n\n    <dependency>\n        <groupId>org.springframework.ai</groupId>\n        <artifactId>spring-ai-starter-mcp-server-webmvc</artifactId>\n    </dependency>\n    <dependency>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-web</artifactId>\n    </dependency>\n\n\n###  Defining tools\n\nWith Spring AI, a tool is just a Spring bean method annotated with `@McpTool`:\n\n\n\n    @Component\n    public class WeatherTool {\n\n        private final WeatherToolService weatherToolService;\n\n        public WeatherTool(WeatherToolService weatherToolService) {\n            this.weatherToolService = weatherToolService;\n        }\n\n        @McpTool(name = \"get_current_weather\",\n                 description = \"Get current weather by dwd_station_id or by lat/lon\")\n        public Map<String, Object> getCurrentWeather(\n                @McpToolParam(description = \"DWD station ID\", required = false)\n                String dwd_station_id,\n                @McpToolParam(description = \"Latitude\", required = false) Double lat,\n                @McpToolParam(description = \"Longitude\", required = false) Double lon\n        ) {\n            return weatherToolService.getWeather(dwd_station_id, lat, lon);\n        }\n    }\n\n\nSpring turns that method into an MCP tool definition and publishes the parameter metadata as part of the schema. That means the model can discover the tool, understand its inputs, and decide when to call it.\n\nThe project also includes a geocoding tool that resolves city names to coordinates:\n\n\n\n    @McpTool(name = \"geocode_city\",\n             description = \"Convert a city name to latitude and longitude using OpenStreetMap Nominatim\")\n    public Map<String, Object> geocodeCity(\n            @McpToolParam(description = \"City name (e.g., 'Berlin', 'New York')\", required = true)\n            String cityName\n    ) { ... }\n\n\n###  The service layer\n\nThe tools delegate the real work to services that handle validation, caching, and external API calls:\n\n\n\n    @Service\n    public class WeatherToolService {\n\n        public Map<String, Object> getWeather(String dwdStationId, Double lat, Double lon) {\n            // Validate the request\n            // Check the cache\n            // Call Bright Sky if needed\n            // Return a structured response\n        }\n    }\n\n\nThe key design choices are straightforward:\n\n  * **Separate TTL caches** for station-id and coordinate lookups\n  * **Structured responses** with `success`, `error_code`, and `error_message`\n  * **Cache metadata** in each response so you can see whether the result came from cache or upstream\n\n\n\n###  Server configuration\n\n\n    server:\n      port: 7170\n\n    spring:\n      ai:\n        mcp:\n          server:\n            name: spring-sample-mcp-server\n            version: 1.0.0\n            protocol: STREAMABLE\n            type: SYNC\n            annotation-scanner:\n              enabled: true\n\n    mcp:\n      security:\n        api-key: ${MCP_API_KEY:}\n\n\nThe `STREAMABLE` protocol gives the agent a lightweight MCP transport, and the shared API key keeps the demo simple without adding full auth infrastructure.\n\n##  2. Security for the Demo\n\nThe MCP server and agent share an `MCP_API_KEY`. The agent adds it automatically as an `X-API-Key` header, and the server validates it on inbound MCP requests.\n\nThat is enough for local development and a sample project. For anything public-facing, move to Spring Security, OAuth2 or JWT, rate limiting, and a gateway in front of the MCP endpoint.\n\n##  3. The AI Chat Agent\n\nThe agent is responsible for deciding when to use tools, calling Gemini, and keeping the conversation stateful.\n\n###  Dependencies\n\n\n    <dependency>\n        <groupId>org.springframework.ai</groupId>\n        <artifactId>spring-ai-starter-model-google-genai</artifactId>\n    </dependency>\n    <dependency>\n        <groupId>org.springframework.ai</groupId>\n        <artifactId>spring-ai-starter-mcp-client</artifactId>\n    </dependency>\n    <dependency>\n        <groupId>org.springframework.boot</groupId>\n        <artifactId>spring-boot-starter-web</artifactId>\n    </dependency>\n\n\n###  MCP client configuration\n\nThe agent injects the shared API key through a custom HTTP request customizer:\n\n\n\n    @Configuration\n    public class AgentConfiguration {\n\n        @Bean\n        McpClientCustomizer<HttpClientStreamableHttpTransport.Builder>\n        streamableHttpTransportCustomizer(AgentProperties properties) {\n            McpSyncHttpClientRequestCustomizer requestCustomizer =\n                    (builder, method, uri, body, context) -> {\n                        if (StringUtils.hasText(properties.getMcpApiKey())) {\n                            builder.header(\"X-API-Key\", properties.getMcpApiKey());\n                        }\n                    };\n            return (name, builder) -> builder.httpRequestCustomizer(requestCustomizer);\n        }\n    }\n\n\n###  Core chat flow\n\nThe agent keeps a small in-memory conversation history, checks whether the user message looks like a tool request, and then routes the prompt through either a plain Gemini client or a tool-enabled client.\n\n\n\n    public String reply(String sessionId, String userMessage) {\n        List<ConversationTurn> history = memoryStore.history(sessionId);\n        String prompt = buildPrompt(history, userMessage);\n        boolean toolRequest = shouldUseTools(userMessage);\n        ChatClient client = toolRequest ? toolEnabledClient() : plainChatClient;\n        String answer = invokeModel(client, prompt);\n        memoryStore.appendTurn(sessionId, userMessage, answer);\n        return answer;\n    }\n\n\nThe lazy initialization is deliberate: the agent can start even if the MCP server is down, and it only initializes MCP clients when a tool request actually arrives.\n\nThe tool trigger is intentionally simple:\n\n\n\n    private static boolean shouldUseTools(String userMessage) {\n        String normalized = userMessage.toLowerCase(Locale.ROOT);\n        for (String keyword : TOOL_KEYWORDS) {\n            if (normalized.contains(keyword)) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n\nThat heuristic is enough for a demo and easy to explain. In a larger system, you could replace it with a router model or intent classifier.\n\n###  Virtual threads and timeout handling\n\nThe model call runs on a virtual thread with a configurable timeout so the request does not hang forever if Gemini is slow or unreachable:\n\n\n\n    private String invokeModel(ChatClient client, String prompt) {\n        var executor = Executors.newVirtualThreadPerTaskExecutor();\n        try {\n            var future = executor.submit(() ->\n                    client.prompt().user(prompt).call().content());\n            return future.get(timeoutSeconds, TimeUnit.SECONDS);\n        } catch (TimeoutException ex) {\n            throw new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, ...);\n        } finally {\n            executor.shutdownNow();\n        }\n    }\n\n\n###  Session memory\n\nConversation history lives in an in-memory LRU store with a small per-session turn window. That keeps follow-up questions like \"What about tomorrow?\" grounded in the earlier exchange without introducing a database too early.\n\nThe agent configuration sets the model to `gemini-3.5-flash`, the memory limit to 20 turns per session, and the session cap to 500.\n\n##  4. The React Chat UI\n\nThe frontend is a Vite app with a simple chat window, minimal state, and no component library.\n\n\n\n    const [messages, setMessages] = useState([]);\n    const [loading, setLoading] = useState(false);\n\n    const sendMessage = async (text) => {\n        setMessages(prev => [...prev, { role: 'user', content: text }]);\n        setLoading(true);\n        const response = await fetch('/api/chat', {\n            method: 'POST',\n            headers: { 'Content-Type': 'application/json' },\n            body: JSON.stringify({ sessionId, message: text })\n        });\n        const data = await response.json();\n        setMessages(prev => [...prev, {\n            role: 'assistant',\n            content: data.reply || 'No response'\n        }]);\n        setLoading(false);\n    };\n\n\nThe Vite dev server proxies `/api/*` to the agent:\n\n\n\n    proxy: {\n      '/api': {\n        target: 'http://localhost:7171',\n        changeOrigin: true,\n        rewrite: (path) => path.replace(/^\\/api/, '')\n      }\n    }\n\n\nThe UI is intentionally plain: a purple gradient, responsive layout, and a smooth message list are enough to make the app feel complete without distracting from the architecture.\n\n##  5. Putting It All Together\n\n###  Running the application\n\n  1. Set the environment variables:\n\n\n\n\n    export GEMINI_API_KEY=your_gemini_api_key\n    export MCP_API_KEY=a_shared_secret\n\n\n  1. Start the MCP server:\n\n\n\n\n    cd mcp-server-spring\n    mvn spring-boot:run\n\n\n  1. Start the agent:\n\n\n\n\n    cd mcp-spring-agent\n    mvn spring-boot:run\n\n\n  1. Start the UI:\n\n\n\n\n    cd mcp-ui\n    npm install\n    npm run dev\n\n\n###  What happens when you ask a question\n\nIf the user asks, \"What's the weather in Berlin?\" the flow looks like this:\n\n  1. The agent sees the word \"weather\" and switches to tool-enabled mode\n  2. Gemini calls `geocode_city(\"Berlin\")` to get coordinates\n  3. The agent calls `get_current_weather(lat=52.52, lon=13.41)`\n  4. Gemini turns the raw data into a readable response\n  5. The UI renders the answer\n\n\n\n##  6. Why This Architecture Works\n\n**MCP separates the model from the tools.** The agent knows what tools exist and how to call them, but not how those tools are implemented. That makes the system easier to evolve.\n\n**The same server can serve different models.** Gemini is just the model in this demo. The MCP server itself can work with any compatible client.\n\n**Lazy initialization keeps the app resilient.** The agent can boot even if the MCP server is temporarily unavailable, and tool support only activates when it is actually needed.\n\n##  7. What's Next\n\nThis sample is a solid starting point. Natural next steps include:\n\n  * **Docker Compose** - run all services together\n  * **PostgreSQL persistence** - durable chat history and richer memory\n  * **OAuth2** - authenticated multi-user access\n  * **WebSocket streaming** - token-by-token responses\n  * **Kubernetes** - scale the agent and tool server independently\n\n\n\n##  Resources\n\n  * Source code on GitHub\n  * Spring AI MCP documentation\n  * Model Context Protocol specification\n  * Google Gemini API documentation\n\n\n\n_Have you built anything with MCP and Spring AI? I'd love to hear how you approached it._",
  "title": "Building an AI Chat Agent with MCP, Spring AI"
}