- Explore MCP Servers
- langchain4j-for-tools-and-mcp-demo
Langchain4j For Tools And Mcp Demo
What is Langchain4j For Tools And Mcp Demo
langchain4j-for-tools-and-mcp-demo is a demo repository that showcases how to integrate Large Language Models (LLMs) with Java applications using LangChain4j and the Model Context Protocol (MCP). It contains code examples from a talk presented at the Madrid JUG.
Use cases
Use cases include building chatbots, creating interactive applications that leverage LLM capabilities, and developing tools that require real-time data processing and interaction with LLMs.
How to use
To use langchain4j-for-tools-and-mcp-demo, clone the repository and run the provided Java examples using JBang. Each demo illustrates different functionalities, such as connecting to an LLM, maintaining conversation context, and exposing Java functions as tools for LLMs.
Key features
Key features include easy integration with LLMs, stateless and stateful interactions, built-in memory management, and the ability to expose Java functions as callable tools for LLMs.
Where to use
langchain4j-for-tools-and-mcp-demo can be used in various fields such as software development, AI research, and any application requiring integration between Java applications and LLMs.
Clients Supporting MCP
The following are the main client software that supports the Model Context Protocol. Click the link to visit the official website for more information.
Overview
What is Langchain4j For Tools And Mcp Demo
langchain4j-for-tools-and-mcp-demo is a demo repository that showcases how to integrate Large Language Models (LLMs) with Java applications using LangChain4j and the Model Context Protocol (MCP). It contains code examples from a talk presented at the Madrid JUG.
Use cases
Use cases include building chatbots, creating interactive applications that leverage LLM capabilities, and developing tools that require real-time data processing and interaction with LLMs.
How to use
To use langchain4j-for-tools-and-mcp-demo, clone the repository and run the provided Java examples using JBang. Each demo illustrates different functionalities, such as connecting to an LLM, maintaining conversation context, and exposing Java functions as tools for LLMs.
Key features
Key features include easy integration with LLMs, stateless and stateful interactions, built-in memory management, and the ability to expose Java functions as callable tools for LLMs.
Where to use
langchain4j-for-tools-and-mcp-demo can be used in various fields such as software development, AI research, and any application requiring integration between Java applications and LLMs.
Clients Supporting MCP
The following are the main client software that supports the Model Context Protocol. Click the link to visit the official website for more information.
Content
LangChain4j for Tools and MCP - Demo Repository
This repository contains all the code and examples from my talk:
MCP: using Java and Quarkus to bridge LLMs with your applications and data, presented at:
- 2025-04-02 - Sevilla JUG (Sevilla, Spain) - Slides
- 2024-03-05 - Madrid JUG (Madrid, Spain) - Slides
This repository showcases a series of demos illustrating the integration of Large Language Models (LLMs) with Java using LangChain4j and the Model Context Protocol (MCP).
Currently, the demos use the OpenAI API as the LLM provider, but they can be adapted to work with any other LLM, including self-hosted or local models.
Demos Included
1️. Hello World with LangChain4j
A set of Java examples demonstrating how to connect to an LLM using LangChain4j and JBang.
Demos
HelloLangChain4j01.java: Sends a message to the LLM and receives a response.HelloLangChain4j02.java: Demonstrates that LLM interactions are stateless—the model does not remember previous inputs.HelloLangChain4j03.java: Sends the full conversation history to the LLM, allowing it to maintain context.HelloLangChain4j04.java: Uses LangChain4j’s built-in memory to avoid manually sending the entire message history.
📌 All these demos use JBang to keep execution simple and portable.
A set of Java examples demonstrating how to expose Java functions as tools that LLMs can invoke dynamically.
Like the previous examples, these demos are based on LangChain4j and use JBang for easy execution.
This section’s demos use LyingWeatherTool.java, a class where we define a (fake) tool that we expose to the LLM.
The Lying Weather Tool contains a single function:
@Tool
public static String getWeather(String city) {
return "The weather in " + city + " is sunny and hot.";
}
- The function is registered as a tool with the description:
“A tool to get the current weather in a city.” - However, the function always returns the same (fake) weather response, no matter the city.
Demos
CallingTool01.java:
- Defines a tool specification for the Lying Weather Tool and provides it to the LLM.
- The LLM understands the tool and requests an execution.
CallingTool02.java:
- Manually executes the tool request and sends the response back to the LLM.
- The LLM integrates the result into a natural language response.
- Demonstrates why manual execution is cumbersome—this is where automated execution come in!
CallingTool03.java:
- Uses another LangChain4j concept, AI Services, that make everything simpler, with features like calling tools transparently
3️. Real Weather Tool with LangChain4j
This demo showcases how to integrate real-time weather data into a Java application using LangChain4j, OkHttp, and Jackson Databind. The tool fetches current weather information for a specified city and country code by:
-
Geocoding: Retrieving the latitude and longitude of the specified city using the Open-Meteo Geocoding API.
-
Fetching Weather Data: Using the obtained coordinates to request current weather conditions from the Open-Meteo Forecast API.
Demo
RealWeatherTool.java contains the implementation of the weather tool, including methods for geocoding and fetching weather data:
-
Define the Tool: Annotate the
getWeathermethod with@Toolto expose it for LLM integration.@Tool("A tool to get the current weather for a given city and country code") public static String getWeather(String city, String countryCode) { // Implementation } -
Geocoding: Implement the
getCoordinatesmethod to fetch latitude and longitude for the specified city and country code using the Open-Meteo Geocoding API. -
Fetch Weather Data: Use the coordinates to request current weather information from the Open-Meteo Forecast API.
-
Handle Responses: Parse the JSON responses using Jackson’s
ObjectMapperand extract relevant data such as temperature, windspeed, and weather conditions. -
Integrate with LLM: Utilize LangChain4j to enable the LLM to call the
getWeathertool dynamically, providing real-time weather information based on user queries.
CallingTool01.java simply uses AI Services, to allows the LLM to call the Real Weather Tool transparently.
This demo showcases how to implement a Model Context Protocol (MCP) server using Quarkus MCP and the real weather tool functionality from the previous examples.
The Model Context Protocol (MCP) is a standardized way for tools and LLMs to communicate, allowing:
- Tools to expose their functionality to any MCP-compatible LLM
- LLMs to discover and use tools without being tied to specific implementations
- A consistent interface for tool specifications and invocations
Demo
McpWeatherServer.java implements an MCP server that:
-
Uses Quarkus MCP Server STDIO: Handles the MCP protocol communication through standard input/output.
-
Exposes a Weather Tool: Makes the weather tool discoverable and usable by any MCP-compatible LLM.
@Tool(description = "A tool to get the current weather a given city and country code") public String getWeather( @ToolArg(description = "The city") String city, @ToolArg(description = "The country code") String countryCode) { // Implementation } -
MCP-specific Annotations: Uses
@Tooland@ToolArgannotations from the MCP specification instead of LangChain4j’s annotations. -
Environment Compatibility: Includes a wrapper script (
jbang-wrapper.sh) to handle environment variable issues when running from AI assistants like Claude Desktop on Mac.
Getting Started
Prerequisites
- Java 17+: Ensure you have Java 17 or higher installed.
- JBang: A tool to run Java applications with minimal setup. Install it from https://www.jbang.dev/.
Running the Demos
-
Clone the Repository:
git clone https://github.com/LostInBrittany/langchain4j-for-tools-and-mcp-demo.git cd langchain4j-for-tools-and-mcp-demo -
Execute any of the demos using JBang:
jbang ./01-Hello-LangChain4j/HelloLangChain4j01.javaReplace
./01-Hello-LangChain4j/HelloLangChain4j01.javawith the appropriate script for other demos.
Resources & Further Reading
- 📘 LangChain4j Documentation: https://github.com/langchain4j/langchain4j
- 🛠 JBang Documentation: https://www.jbang.dev/documentation/
License
📝 This repository is licensed under the MIT License. See the LICENSE file for details.
Dev Tools Supporting MCP
The following are the main code editors that support the Model Context Protocol. Click the link to visit the official website for more information.










