Integrating MCP (Model Context Protocol) with LangChain4j to access GitHub

Integrating MCP (Model Context Protocol) with LangChain4j to access GitHub

Large Language Models (LLMs) have transformed how we interact with software, but they often lack direct access to real-world data and services. The Model Context Protocol (MCP) addresses this limitation by providing a standardised way for LLMs to interact with external systems.

In this article, you'll learn how to use LangChain4j to connect an LLM with GitHub through MCP: it shows how to fetch and summarise GitHub repository commits.

What is MCP?

The Model Context Protocol (MCP) is an open protocol that standardises how Large Language Model (LLM) applications communicate with external data sources and tools. It enables integration between LLMs and various services, allowing them to access real-world data and perform actions through a well-defined interface. MCP follows a client-server architecture built on JSON-RPC where:

  • Hosts are LLM applications (like a Java application using LangChain4j) that initiate connections
  • MCP clients maintain 1:1 connections with MCP servers, inside the host application
  • MCP servers provide context, tools, and prompts to the MCP clients
  • The transport layer handles the actual communication between MCP clients and MCP servers. All transports use JSON-RPC 2.0 to exchange messages. MCP supports multiple transport mechanisms: Standard Input/Output (stdio) and HTTP with Server-Sent Events (SSE)
  • External datasources can be anything from databases to APIs, file systems, GitHub APIs, or other services

MCP architecture overview
MCP Architecture Overview

MCP servers can expose three main types of capabilities:

  • Resources: File-like data that can be read by clients, such as API responses, file contents, or database records. Each resource is identified by a unique URI.
  • Tools: Functions that can be called by the client. These could include operations like querying databases, performing computations or calling APIs (e.g. GitHub APIs).
  • Prompts: Pre-defined templates or instructions that guide language model interactions.

Each of these server capability is accessed through specific protocol messages. For example, to use a tool:

  • The client discovers available tools through a tools/list request
  • Tools are called using the tools/call endpoint with specific arguments
  • The server executes the tool and returns results in a standardised format

The GitHub MCP Server

The MCP ecosystem includes numerous servers, primarily implemented in TypeScript and Python, each providing specific functionality to LLMs. These servers enable LLMs to interact with various services and data sources, from file systems to databases, and from search engines to version control systems.

The GitHub MCP server exposes GitHub's functionality through a set of standardised tools that LLMs can discover and use. It can be executed locally on your machine using Docker. When an LLM connects to this MCP server, it can retrieve a list of available tools through the tools/list endpoint. These tools include operations such as:

  • Repository management (create, fork, search repositories)
  • File operations (read, create, update files)
  • Issue and pull request handling
  • Commit history retrieval

When an LLM decides to use a tool, it sends a tools/call request with the appropriate parameters (the name of the tool and the parameters of this tool). The GitHub MCP server then executes the corresponding GitHub API operation and returns the results to the MCP client in a standardised format, that the LLM can then process.

MCP in LangChain4j

LangChain4j has introduced a new module that simplifies the integration of MCP servers into Java applications. This module provides a clean API for establishing connections with MCP servers and executing tools through them.

The core components of the LangChain4j MCP implementation include:

  • McpTransport: Handles the underlying transport mechanisms for server communication
  • McpClient: Manages the connection and communication with MCP servers
  • McpToolProvider: Converts MCP server capabilities into LangChain4j tools

Here's how you create an MCP client in LangChain4j:

McpTransport transport = new StdioMcpTransport.Builder()
    .command(List.of("your-command"))
    .logEvents(true)
    .build();

McpClient mcpClient = new DefaultMcpClient.Builder()
    .transport(transport)
    .build();

McpToolProvider toolProvider = McpToolProvider.builder()
    .mcpClients(List.of(mcpClient))
    .build();        

Using LangChain4j and MCP to Connect to GitHub

Let's examine a practical example that uses the GitHub MCP server to summarise commits from a public repository. First of all, to interact with GitHub, you need to execute the GitHub MCP server in Docker. To build the Docker image, you can follow these instructions.

Once the GitHub MCP server is up and running, you can connect to it using LangChain4j. For that, you first need to set up the AI model (here we use OpenAI's GPT-4o-mini model):

ChatLanguageModel model = OpenAiChatModel.builder()
    .apiKey(System.getenv("OPENAI_API_KEY"))
    .modelName("gpt-4o-mini")
    .logRequests(true)
    .logResponses(true)
    .build();        

Next, you configure the MCP transport layer to communicate with the GitHub MCP server using Docker. Notice the docker command that starts the container mcp/github:

McpTransport transport = new StdioMcpTransport.Builder()
    .command(List.of("/usr/local/bin/docker", "run", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", "-i", "mcp/github"))
    .logEvents(true)
    .build();        

You then create an MCP client and tool provider:

McpClient mcpClient = new DefaultMcpClient.Builder()
    .transport(transport)
    .build();

McpToolProvider toolProvider = McpToolProvider.builder()
    .mcpClients(List.of(mcpClient))
    .build();        

Finally, you create an AI service and use it to interact with GitHub by asking to summarise the last 3 commits:

Bot bot = AiServices.builder(Bot.class)
    .chatLanguageModel(model)
    .toolProvider(toolProvider)
    .build();

String response = bot.chat("Summarize the last 3 commits of the LangChain4j GitHub repository");
System.out.println("RESPONSE: " + response);        

The response will be something like:

Here are the summaries of the last three commits in the LangChain4j GitHub repository:

* Commit SHA: 1a8a70e
  * Date: February 18, 2025
  * Author: Dmytro Liubarskyi
  * Message: "docu: added tools <-> MCP links"
  * Details: This commit added documentation links between tools and MCP.

* Commit SHA: d755ae5
  * Date: February 18, 2025
  * Author: minglu7
  * Message: "Fix deprecated methods in DefaultContentInjector (#2505)"
  * Details: This commit addressed deprecated methods in the `DefaultContentInjector`

* Commit SHA: 55484f2
  * Date: February 18, 2025
  * Author: SylvestrePinget
  * Message: "Removing amazon.titan-text-express-v1 and ai21.jamba-instruct-v1:0 in ITS (#2532)"
  * Details: This commit removed legacy models, `amazon.titan-text-express-v1`        

Under the Hood

For the LLM to be able to answer the user's question about the last commits, many interactions need to happen:

  • The MCP Client sends a tools/list request to the local GitHub MCP Server. The server sends back the list of the available tools, such as list_commits, list_issues, etc.
  • The LLM picks up the tools which it thinks is appropriate, here list_commits.
  • The MCP Client sends a tools/call request to the MCP Server passing the tool name list_commits and the needed parameters (the last 3 commits).
  • The GitHub MCP Server uses the GitHub API to remotely get the last 3 commits from the GitHub Server.
  • The Application sends a request to the GPT 4o Mini language model to summarize the last three commits passing the user's prompt as well as the last 3 commits.
  • The GPT 4o Mini model responds with the summaries of the last three commits.

MCP requests to remote GitHub and LLM
MCP Requests to Remote GitHub and LLM

Conclusion

MCP provides a powerful way to extend LLM capabilities by enabling standardised access to external tools and data sources. LangChain4j's implementation makes it straightforward to integrate MCP servers into Java applications, requiring minimal setup and configuration.

MCP faces the challenges of establishing itself in the rapidly evolving AI landscape. Wider adoption and community contributions will be crucial for MCP to become the de facto standard for connecting LLMs with external tools and services.

You can find all the code of this article in the LangChain4j Sample repository.

Where to Go Next

To learn more about MCP and LangChain4j, you can explore these resources:


Nikolay Kuznetsov

pgx-outbox | Senior Golang Software Engineer at Zalando Oy

2 周

Thanks for article. In the last flow diagram how the application decides to use MCP Server first before calling LLM? I guess this interaction is missing "The LLM picks up the tools which it thinks is appropriate, here list_commits."

回复
Cédrick Lunven

AI Startup co-founder, CTO, Senior Software Engineer, FF4j creator

3 周

Great article! For old dudes like me.. I am thinking of MCP as the Service Registry (Consult, Eureka, Zookeeper anyone ?) for AI Tools. (+ adding a protocol on top of it). Just for the meme...welcome TOA (tools oriented architecture)... Question open to debate what motivates the implementation of an MCP SERVER as a service provider, or as an application owner to open your tools to the IS.... work for consultants up to 2030.

回复
Alexandre de Pellegrin

Directeur des Systèmes d'Information, enthousiaste et accompagné d'une belle équipe

3 周

J'adore !!! Merci Antonio de m'avoir fait découvrir cela. Ca ouvre de belles perspectives pour la mise en place de RAG. Finalement, dans ce cas, pas de base vectorielle.

回复
John Thorstad

husband | father | musician | nerd stuff | duality

3 周

this is rad. love seeing MCP in action like this. feels like a real step toward making AI actually useful instead of just chatty. we've been using it with Nigel (Scott Silvi's AI system that turns natural language into all sorts of structured outputs) to pull in github context too, and it's been a game changer for making LLMs actually do something productive.

回复

要查看或添加评论,请登录

Antonio Goncalves的更多文章

  • Announcing My LangChain4j Book

    Announcing My LangChain4j Book

    ?? Exciting News! My New Book: Understanding LangChain4j is Now Available! ?? I’m thrilled to announce the launch of my…

    6 条评论
  • #DevoxxFrance #Backstage Being Interviewed

    #DevoxxFrance #Backstage Being Interviewed

    If you've followed along this series of blogs posts about the backstage of Devoxx France, then you already know that…

  • #DevoxxFrance #Backstage The RedCoat Team

    #DevoxxFrance #Backstage The RedCoat Team

    Devoxx France is co-organised by Nicolas Martignole, Zouheir Cadi and I. We created a company that deals with all the…

    3 条评论
  • #DevoxxFrance #Backstage Saturday

    #DevoxxFrance #Backstage Saturday

    Yesterday was the last day of the conference. For attendees it finished at 6pm, but for us it took slightly longer as…

    2 条评论
  • #DevoxxFrance #Backstage Friday

    #DevoxxFrance #Backstage Friday

    Yesterday was our longest day with our Meet and Greet party with lots of singing. Today is the last day of Devoxx…

    1 条评论
  • #DevoxxFrance #Backstage Thursday

    #DevoxxFrance #Backstage Thursday

    Yesterday was the first day of Devoxx France. Most of the attendees arrived in the morning and everything had to be…

    3 条评论
  • #DevoxxFrance #Backstage Wednesday

    #DevoxxFrance #Backstage Wednesday

    Yesterday was about setting up the exhibition hall, getting the RedCoat team ready, sorting out the venue, making sure…

    4 条评论
  • #DevoxxFrance #Backstage Tuesday

    #DevoxxFrance #Backstage Tuesday

    If Monday was about getting ready, Tuesday is our hardest backstage day. Booth are getting installed from 6am…

    9 条评论
  • #DevoxxFrance #Backstage Monday

    #DevoxxFrance #Backstage Monday

    If for attendees Devoxx France starts on Wednesday the 17th of April 2024 and lasts for 3 days, for us organisers, it's…

    2 条评论

社区洞察

其他会员也浏览了