|
| 1 | +# Kotlin MCP Client |
| 2 | + |
| 3 | +This project demonstrates how to build a Model Context Protocol (MCP) client in Kotlin that interacts with an MCP server |
| 4 | +via a STDIO transport layer while leveraging Anthropic's API for natural language processing. The client uses the MCP |
| 5 | +Kotlin SDK to communicate with an MCP server that exposes various tools, and it uses Anthropic's API to process user |
| 6 | +queries and integrate tool responses into the conversation. |
| 7 | + |
| 8 | +For more information about the MCP SDK and protocol, please refer to |
| 9 | +the [MCP documentation](https://modelcontextprotocol.io/introduction). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +- **Java 17 or later** |
| 14 | +- **Gradle** (or the Gradle wrapper provided with the project) |
| 15 | +- An Anthropic API key set in your environment variable `ANTHROPIC_API_KEY` |
| 16 | +- Basic understanding of MCP concepts and Kotlin programming |
| 17 | + |
| 18 | +## Overview |
| 19 | + |
| 20 | +The client application performs the following tasks: |
| 21 | + |
| 22 | +- **Connecting to an MCP server** — |
| 23 | + launches an MCP server process (implemented in JavaScript, Python, or Java) using STDIO transport. |
| 24 | + It connects to the server, retrieves available tools, and converts them to Anthropic’s tool format. |
| 25 | +- **Processing queries** — |
| 26 | + accepts user queries, sends them to Anthropic’s API along with the registered tools, and handles responses. |
| 27 | + If the response indicates a tool should be called, it invokes the corresponding MCP tool and continues the |
| 28 | + conversation based on the tool’s result. |
| 29 | +- **Interactive chat loop** — |
| 30 | + runs an interactive command-line loop, allowing users to continuously submit queries and receive responses. |
| 31 | + |
| 32 | +## Building and Running |
| 33 | + |
| 34 | +Use the Gradle wrapper to build the application. In a terminal, run: |
| 35 | + |
| 36 | +```shell |
| 37 | +./gradlew clean build -x test |
| 38 | +``` |
| 39 | + |
| 40 | +To run the client, execute the jar file and provide the path to your MCP server script. |
| 41 | + |
| 42 | +To run the client with any MCP server: |
| 43 | + |
| 44 | +```shell |
| 45 | +java -jar build/libs/<your-jar-name>.jar path/to/server.jar # jvm server |
| 46 | +java -jar build/libs/<your-jar-name>.jar path/to/server.py # python server |
| 47 | +java -jar build/libs/<your-jar-name>.jar path/to/build/index.js # node server |
| 48 | +``` |
| 49 | + |
| 50 | +> [!NOTE] |
| 51 | +> The client uses STDIO transport, so it launches the MCP server as a separate process. |
| 52 | +> Ensure the server script is executable and is a valid `.js`, `.py`, or `.jar` file. |
| 53 | +
|
| 54 | +## Configuration for Anthropic |
| 55 | + |
| 56 | +Ensure your Anthropic API key is available in your environment: |
| 57 | + |
| 58 | +```shell |
| 59 | +export ANTHROPIC_API_KEY=your_anthropic_api_key_here |
| 60 | +``` |
| 61 | + |
| 62 | +The client uses `AnthropicOkHttpClient.fromEnv()` to automatically load the API key from `ANTHROPIC_API_KEY` and |
| 63 | +`ANTHROPIC_AUTH_TOKEN` environment variables. |
| 64 | + |
| 65 | +## Additional Resources |
| 66 | + |
| 67 | +- [MCP Specification](https://spec.modelcontextprotocol.io/) |
| 68 | +- [Kotlin MCP SDK](https://github.com/modelcontextprotocol/kotlin-sdk) |
| 69 | +- [Anthropic Java SDK](https://github.com/anthropics/anthropic-sdk-java/tree/main) |
0 commit comments