gtag('config', 'G-0PFHD683JR');
Price Prediction

Reconsidering Langchain4j after 6 months

Last year, she started drilling a little around Langchain4j. It is a fast -growing project, and I wanted to know the updates. I also wanted to check how to merge the model protocol server into Langchain4j.

The version is 1 beta

I wrote my last post in November 2024 and used the latest version available at that time, V0.35. Langchain4j began its journey about 1.0 December.

date

Release

September 25, 2024

0.35.0

December 22, 2024

1.0.0-Alpha1

February 10, 2025

1.0.0-beta1

March 13, 2025

1.0.0-beta2

April 12, 2025

1.0.0-beta3

Langchain4j follows Semver. The supervisors used this occasion to make breakage changes. In my case, I had to update my code to calculate API changes.

V0.35

v1.0.0-beta3

Val s = sinks.many ()
.
.onbackPressurebuffer()
Chatbot.talk (M.ssionid, M.TEXT)
.onnext (s :: tryemitnext)
.NERROR (S :: Tryemiterror)
Oncomplete {
S.Tryemcomplete ()
} It begins ()
Return Serverresponse.ok (). Bodyandawait (
S.asflux (). Asflow ()
))

Val s = sinks.many ()
.
.onbackPressurebuffer()
Chatbot.talk (M.ssionid, M.TEXT)
Conpartialresponse (s :: tryemitnext)
.NERROR (S :: Tryemiterror)
Oncompleesponse {
S.Tryemcomplete ()
} It begins ()
Return Serverresponse.ok (). Bodyandawait (
S.asflux (). Asflow ()
))

The integration of the project reactor

Langchain4j provides the integration of the project reactor; I missed this in my previous reflections. With Kotlin Coroutines, it simplifies the code a lot.

I am using AiServicesSo I previously introduced an interface for Langchain4j to implement it at the time of operation:

interface ChatBot {
    fun talk(@MemoryId sessionId: String, @UserMessage message: String): TokenStream
}

We must add the following dependency:


    dev.langchain4j
    langchain4j-reactor
    1.0.0-beta3

We can now change the type of return from a Flux to TokenStream. Here is the updated signature:

interface ChatBot {
    fun talk(@MemoryId sessionId: String, @UserMessage message: String): Flux
}

Makes creation sink Above is unnecessary. We can simplify the code as follows:

val flux = chatBot.talk(m.sessionId, m.text)
ServerResponse.ok().bodyAndAwait(flux.asFlow())

Remember that two days of correcting errors can easily save you two hours of reading documents! I did not do the latter.

Merging the protocol of the context of the form

Even this point, our changes were minimal. In this section, I want to merge MCP In my Langchain4j app.

A generation for retrieval

One needs a lot and a lot of resources to train Llm: Translate directly into time and money. For this reason, companies limit the training of new model versions. The suitability of the model decreases over time as the information accumulates and changes it, while the LLM database is not changeable. Moreover, LLMS is trained in general data – through nature, while most companies want to inquire about their own data as well.

The enhanced generation of retrieval was the traditional way to deal with these limits. The generation of recovery is a two -step process. In the first step, the tool distributes data, directs it according to LLM, and stores it in a vector database; In the second, the tool uses the database as additional tubes when inquiring about LLM.

Form context protocol

The latest way to deal with the fixed nature of LLMS is MCP.

MCP is an open protocol that shows how to provide LLMS context applications. Think of MCP like a USB-C port for artificial intelligence applications. Just as USB-C provides a unified method of connecting your devices to various peripherals and accessories, MCP provides a unified method to connect artificial intelligence models to different sources and data tools.

Start with the form of the context of the model

MCP has benefits on RAT:

  • The data processed is designed with a cloth for the form. If one wants to use a new model, one must re -implement the analysis stage. The MCP unifies the reactions between the customer and the server, which makes it independent of technology.

  • RAT allows the data to be read. MCP allows any API call either access to data dynamically Or implement procedures!

MCP determines the transmission alternatives to customer server communications:

  • STDIO: The customer launches a sub -process, and communication occurs across the standard and standard
  • Http with server server events

Teaching the solution

After the theory above, we are now ready for the practical part. It begins to choose a MCP server. This is a good starting point. However, I chose the official GitHub MCP server because Langchain4J documents mention it.

GitHub MCP server offers a file Stwio Transfer. This means that we must get the duo and start it from the application. It is fast compared to the transmission of HTTP, but given the total time that includes the HTTP invitation to the model and its account time alongside it, it is not relevant. From the point of view of architecture, the best component is dedicated to its process.

After some research, I found the MCP-PROXY project. It allows you to switch between STDIO to http or from HTTP to StDIO. It is also available as a Docker. We can combine both the server and the agent with the following Dockerfile:

FROM ghcr.io/sparfenyuk/mcp-proxy:latest

ENV VERSION=0.2.0
ENV ARCHIVE_NAME=github-mcp-server_Linux_x86_64.tar.gz

RUN wget https://github.com/github/github-mcp-server/releases/download/v$VERSION/$ARCHIVE_NAME -O /tmp/$ARCHIVE_NAME \ #1
    && tar -xzvf /tmp/$ARCHIVE_NAME -C /opt \                      #2
    && rm /tmp/$ARCHIVE_NAME                                       #3

RUN chmod +x /opt/github-mcp-server                                #4
  1. Download the archive
  2. Extract
  3. Remove the archive
  4. Make the duo to be implemented

Note that we cannot determine CMD Since the duo only allows the formation of the port and the host with the parameters. For this reason, we must postpone the matter at the time of operation, or in my case, in docker-compose.yaml:

services:
  mcp-server:
    build:
      context: github-mcp-server
    env_file:
      - .env                                                       #1
    command:
      - --pass-environment                                         #2
      - --sse-port=8080                                            #3
      - --sse-host=0.0.0.0                                         #4
      - --                                                         #5
      - /opt/github-mcp-server                                     #6
      - --toolsets
      - all
      - stdio
  1. We need GITHUB_PERSONAL_ACCESS_TOKEN Environmental variable with a valid symbol to ratify Gitap
  2. Passing all environmental variables to the sub -process
  3. Set the listening port
  4. Link any IP
  5. The agent “connects” to the Stdio MCP server after impulsion
  6. Run the server while enabling all options

You will provide the picture /sse The end point on the port 8080.

Coding the solution

The coding portion is the easiest. Go to Langchain4j documents on MCP and follow up. In the project, translates as follows:

bean {
    val transport = HttpMcpTransport.Builder()
        .sseUrl(ref().mcp.url)              //1
        .logRequests(true)                                         //2
        .logResponses(true)                                        //2
        .build()
    val mcpClient = DefaultMcpClient.Builder()
        .transport(transport)
        .build()
    mcpClient.listTools().forEach { println(it) }                  //3
    McpToolProvider.builder()
        .mcpClients(listOf(mcpClient))
        .build()
}
bean {
    coRouter {
        val chatBot = AiServices
            .builder(ChatBot::class.java)
            .streamingChatLanguageModel(ref())
            .chatMemoryProvider { MessageWindowChatMemory.withMaxMessages(40) }
            .contentRetriever(EmbeddingStoreContentRetriever.from(ref>()))
            .toolProvider(ref())                  //4
            .build()
        POST("/")(PromptHandler(chatBot)::handle)
    }
}
  1. I added a ConfigurationProperty URL SSE category
  2. The MCP protocol provides a way to send records to the customer again
  3. It is not necessary, but it helped me to make sure that the customer is connected to the server and can list the tools provided
  4. Connect the McP Tool provider that was created above in AiServices

At this stage, the form must be directed to direct a request that matches any of the registered tools of the MCP server.

curl -N -H 'Content-Type: application/json' localhost:8080 -d '{ "sessionId": "1", "text": "What are my top three most popular GitHub repos?" }'

I tried several times, and I got answers to these lines:

Unfortunately, the provided text does not contain any information about your top three most popular GitHub repositories. The text appears to be a blog post or a personal website, and it mentions some of your projects and experiences with GitHub, but it does not provide any metrics or statistics on the popularity of your repositories.

If you want to know more about the popularity of your GitHub repositories, I would recommend checking out GitHub's own analytics tools, such as GitHub Insights or the Repository Insights API. These tools can provide information about the number of followers, stars, and forks for each repository, as well as other metrics like engagement and activity.

Ignore the tools, despite the documents that claim the opposite.

Fix the solution

I read Langchain4j documents several times, but to no avail. I tried to use Openai and a handful of other artificial intelligence tools without any success. Most of the answers confirmed that they should work outside the box. Some mentioned the tool directly, which defeats the purpose; One mentioned that boys did not support the tools. I reviewed the OLLAMA Blog: I announced the support of tools in 2024. I stumbled nearly a day ago, I wonder about the error.

Separate architecture offers more moving pieces. I suspected that something might be an error in the entire call series. I removed MCP agent, added github-mcp-server Directly to the application image, changed the code from HTTP to StDIO. The problem was not fixed.

I was about to give up when I decided to return to the roots. I copied the sample of the documents: I just succeeded! It was the moment of ha ha.

Upeenai is used, while you were using ollama. I tried MCP with Openai, Mistral AI and OLLAMA. Only Openai model works with MCP. I have sent the same request to the above -mentioned manner:

curl -N -H 'Content-Type: application/json' localhost:8080 -d '{ "sessionId": "1", "text": "What are my top three most popular GitHub repos?" }'

Now, Openai is setting the demand for the correct tool properly and re -answers that I expected:

Here are my findings regarding your top three most popular GitHub repositories:

1. **[opentelemetry-tracing](https://github.com/nfrankel/opentelemetry-tracing)**  
   - **Description**: Demo for end-to-end tracing via OpenTelemetry.  
   - **Stars**: 68  
   - **Forks**: 25  
   - **Open Issues**: 10  

2. **[kaadin](https://github.com/nfrankel/kaadin)**  
   - **Description**: Kotlin DSL for Vaadin.  
   - **Stars**: 44  
   - **Forks**: 12  
   - **Open Issues**: 3  

3. **[jvm-controller](https://github.com/nfrankel/jvm-controller)**  
   - **Description**: Example on how to write a Kubernetes controller in Java.  
   - **Stars**: 33  
   - **Forks**: 10  
   - **Open Issues**: 0  

These repositories demonstrate a range of your interests and contributions in the areas of observability, Kotlin development, and Kubernetes.%    

Since we pass the authentication icon to the MCP server, which passes it to the GitHub applications interface, the latter knows which user is calling. Thus, it can be explained Repos Baladi Part of the above inquiry. I admit that it is an unusual use of regular web applications that meet many users, but use one authentication icon for each. However, it perfectly fits the condition of the desktop.

Other regular questions, For exampleLook for the most popular warehouses on GitHub, a document related to web applications, as there is no implicit context – the user.

conclusion

The main focus of this post is the MCP server integration in the Langchain4j app. Although the composition is clear and direct thanks to the documents, there are some warnings.

First, how the MCP server still fits your structure for you. I had to be creative to make it a detailed, using excellent mcp-proxy. After that, it appears that Langchain4j is an leaked abstraction. It makes everything possible to provide you with a powerful experimental layer, but the applications under it protect you are not equal. I hope the documents will mention this, although I understand that the current version is present in the experimental version.

All in all, was a fun journey. I learned about MCP in the real world, and opened some doors for project ideas.

The full source code for this post can be found on GitHub.

To go further:


It was originally published on Java obsessed on April 27, 2025

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button