Spring AI
    June 7, 2026

    Building an MCP Server with Spring AI

    Most MCP content is Python/TypeScript. Here's how to build an MCP server in Java with Spring AI — expose your services as tools any MCP client can use.

    Share

    The Model Context Protocol (MCP) has become the standard way to connect AI models to external tools and data. But nearly every tutorial is Python or TypeScript — leaving Java shops out. The good news: with Spring AI, exposing your existing Spring services as MCP tools is straightforward, and any MCP client (Claude Desktop, IDE assistants, your own agent) can then call them.

    Why an MCP server?

    Without MCP, every AI integration is bespoke: you wire each tool into each app by hand. MCP flips that — you build one server that exposes capabilities in a standard shape, and any MCP-aware client can discover and use them. For an enterprise with existing Java services, an MCP server is the cleanest bridge between your systems and AI assistants.

    flowchart LR Client[MCP client - Claude / IDE / agent] -->|MCP| Server[Your Spring AI MCP server] Server --> S1[Order service] Server --> S2[Inventory DB] Server --> S3[Internal API]

    1. Add the MCP server starter

    <dependency>
      <groupId>org.springframework.ai</groupId>
      <artifactId>spring-ai-starter-mcp-server</artifactId>
    </dependency>
    spring.ai.mcp.server.name=order-tools
    spring.ai.mcp.server.version=1.0.0

    2. Expose services as tools

    A tool is a Spring bean method annotated with @Tool — exactly like local tool calling. The MCP server publishes these so remote clients can call them. The description is the contract the client's model reads to decide when to call it.

    @Service
    class OrderTools {
    
        private final OrderRepository orders;
    
        OrderTools(OrderRepository orders) {
            this.orders = orders;
        }
    
        @Tool(description = "Get the current status and ETA for a customer order by ID")
        OrderStatus orderStatus(
            @ToolParam(description = "Order ID, e.g. ORD-1234") String orderId) {
            return orders.findStatus(orderId);
        }
    }
    
    record OrderStatus(String orderId, String state, String eta) {}

    3. Register the tools with the server

    @Configuration
    class McpConfig {
        @Bean
        ToolCallbackProvider orderToolCallbacks(OrderTools orderTools) {
            return MethodToolCallbackProvider.builder()
                .toolObjects(orderTools)
                .build();
        }
    }

    That's it — start the app and the server advertises orderStatus over MCP. Local clients connect over stdio; networked clients over SSE/HTTP, configurable via properties.

    4. Point a client at it

    In an MCP client (e.g. Claude Desktop), register the server command. The client lists your tools and the model calls orderStatus whenever a user asks about an order — no per-client glue code.

    Production notes

    • Treat tool inputs as untrusted. The model chooses the arguments; validate them and guard writes with real authorization.
    • Scope what you expose. Don't surface every service — publish a deliberate, well-described set. Smaller, sharper tool lists route more reliably.
    • Return structured errors ("not found" vs "unavailable") so the client's model recovers gracefully.
    • Auth & observability belong at the server boundary — log tool calls, rate-limit, and authenticate clients like any other API.

    Wrap-up

    MCP isn't Python-only. With Spring AI's MCP server starter, your Java services become first-class AI tools in a few annotations — the enterprise-friendly path to making existing systems usable by any AI assistant.

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Join engineers building production AI systems