From ee7c8a4662768dd0bc24d7912ce4b881f1184a86 Mon Sep 17 00:00:00 2001 From: Antonio Goncalves Date: Tue, 18 Feb 2025 08:44:43 +0100 Subject: [PATCH] Adds a GitHub MCP Server sample (#141) @dliubarskyi once this sample is merge I will write about it in the LangChain4j documentation See https://github.com/langchain4j/langchain4j/pull/2512 --------- Co-authored-by: Julien Dubois --- mcp-github-example/pom.xml | 51 ++++++++++++++ .../langchain4j/example/mcp/github/Bot.java | 6 ++ .../mcp/github/McpGithubToolsExample.java | 66 +++++++++++++++++++ .../src/main/resources/logback.xml | 12 ++++ pom.xml | 1 + 5 files changed, 136 insertions(+) create mode 100644 mcp-github-example/pom.xml create mode 100644 mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/Bot.java create mode 100644 mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/McpGithubToolsExample.java create mode 100644 mcp-github-example/src/main/resources/logback.xml diff --git a/mcp-github-example/pom.xml b/mcp-github-example/pom.xml new file mode 100644 index 0000000..d487aa3 --- /dev/null +++ b/mcp-github-example/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + dev.langchain4j + mcp-github-example + 1.0.0-beta1 + + + 17 + 17 + UTF-8 + + + + + + dev.langchain4j + langchain4j-mcp + 1.0.0-beta1 + + + + dev.langchain4j + langchain4j-open-ai + 1.0.0-beta1 + + + + org.junit.jupiter + junit-jupiter-engine + 5.10.0 + + + + org.assertj + assertj-core + 3.25.3 + + + + ch.qos.logback + logback-classic + 1.5.16 + + + + + diff --git a/mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/Bot.java b/mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/Bot.java new file mode 100644 index 0000000..8f317bf --- /dev/null +++ b/mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/Bot.java @@ -0,0 +1,6 @@ +package dev.langchain4j.example.mcp.github; + +public interface Bot { + + String chat(String prompt); +} diff --git a/mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/McpGithubToolsExample.java b/mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/McpGithubToolsExample.java new file mode 100644 index 0000000..406db07 --- /dev/null +++ b/mcp-github-example/src/main/java/dev/langchain4j/example/mcp/github/McpGithubToolsExample.java @@ -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. + *

+ * 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`. + *

+ * 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. + *

+ * 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(); + } + } +} diff --git a/mcp-github-example/src/main/resources/logback.xml b/mcp-github-example/src/main/resources/logback.xml new file mode 100644 index 0000000..8a7c97e --- /dev/null +++ b/mcp-github-example/src/main/resources/logback.xml @@ -0,0 +1,12 @@ + + + + %-5level %logger{36} - %msg%n + + + + + + + + diff --git a/pom.xml b/pom.xml index 5a74f31..00eb8a3 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,7 @@ jakartaee-microprofile-example jlama-examples mcp-example + mcp-github-example milvus-example mistral-ai-examples neo4j-example