-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a GitHub MCP Server sample (#141)
@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
Showing
5 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
6 changes: 6 additions & 0 deletions
6
mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/Bot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
66 changes: 66 additions & 0 deletions
66
...ithub-example/src/main/java/dev/langchain4j/example/mcp/github/McpGithubToolsExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters