Skip to content

Commit

Permalink
Adds a GitHub MCP Server sample (#141)
Browse files Browse the repository at this point in the history
@dliubarskyi once this sample is merge I will write about it in the
LangChain4j documentation

See langchain4j/langchain4j#2512

---------

Co-authored-by: Julien Dubois <[email protected]>
  • Loading branch information
agoncal and jdubois authored Feb 18, 2025
1 parent 69f2e28 commit ee7c8a4
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
51 changes: 51 additions & 0 deletions mcp-github-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.langchain4j</groupId>
<artifactId>mcp-github-example</artifactId>
<version>1.0.0-beta1</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-mcp</artifactId>
<version>1.0.0-beta1</version>
</dependency>

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>1.0.0-beta1</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.16</version>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.langchain4j.example.mcp.github;

public interface Bot {

String chat(String prompt);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package dev.langchain4j.example.mcp.github;

import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.stdio.StdioMcpTransport;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.tool.ToolProvider;

import java.util.List;

public class McpGithubToolsExample {

/**
* This example uses the GitHub MCP server to showcase how
* to use an LLM to summarize the last commits of a public GitHub repo.
* Being a public repository (the LangChain4j repository is used as an example), you don't need any
* authentication to access the data.
* <p>
* Running this example requires Docker to be installed on your machine,
* because it spawns the GitHub MCP Server as a subprocess via Docker:
* `docker run -i mcp/github`.
* <p>
* You first need to build the Docker image of the GitHub MCP Server that is available at `mcp/github`.
* See https://github.com/modelcontextprotocol/servers/tree/main/src/github to build the image.
* <p>
* The communication with the GitHub MCP server is done directly via stdin/stdout.
*/
public static void main(String[] args) throws Exception {

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

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

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

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

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

try {
String response = bot.chat("Summarize the last 3 commits of the LangChain4j GitHub repository");
System.out.println("RESPONSE: " + response);
} finally {
mcpClient.close();
}
}
}
12 changes: 12 additions & 0 deletions mcp-github-example/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- Set the level to `debug` to see more detailed logs.-->
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<module>jakartaee-microprofile-example</module>
<module>jlama-examples</module>
<module>mcp-example</module>
<module>mcp-github-example</module>
<module>milvus-example</module>
<module>mistral-ai-examples</module>
<module>neo4j-example</module>
Expand Down

0 comments on commit ee7c8a4

Please sign in to comment.