MCP Client with LangChain4j: Connecting an Agent to Multiple Services
In the previous post, I turned each microservice into an MCP server. Now let's connect an AI agent to all of them. The agent will have access to 12+ tools across 4 services and the LLM will decide which ones to call at runtime.
The Client Configuration
LangChain4j provides McpToolProvider , a tool provider that connects to one or more MCP servers and exposes their tools to the agent. Here's my config:
@Configuration @RequiredArgsConstructor public class McpClientConfig {
@Value ( "${mcp.order-service-url}" ) private String orderServiceUrl ; @Value ( "${mcp.payment-service-url}" ) private String paymentServiceUrl ; @Value ( "${mcp.inventory-service-url}" ) private String inventoryServiceUrl ; @Value ( "${mcp.product-validation-url}" ) private String productValidationUrl ;
@Bean public McpToolProvider mcpToolProvider () { return McpToolProvider . builder () . mcpClients ( List .…