Skip to content

Commit 58f2bba

Browse files
authored
add client sample (#55)
* add mcp Client example * Add sample links for weather-stdio-server and kotlin-mcp-client to main readme
1 parent 7d2e993 commit 58f2bba

File tree

12 files changed

+697
-0
lines changed

12 files changed

+697
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ The Model Context Protocol allows applications to provide context for LLMs in a
1414
## Samples
1515

1616
- [kotlin-mcp-server](./samples/kotlin-mcp-server): shows how to set up a Kotlin MCP server with different tools and other features.
17+
- [weather-stdio-server](./samples/weather-stdio-server): shows how to build a Kotlin MCP server providing weather forecast and alerts using STDIO transport.
18+
- [kotlin-mcp-client](./samples/kotlin-mcp-client): demonstrates building an interactive Kotlin MCP client that connects to an MCP server via STDIO and integrates with Anthropic’s API.
1719

1820
## Installation
1921

samples/kotlin-mcp-client/.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.gradle
2+
build/
3+
!gradle/wrapper/gradle-wrapper.jar
4+
!**/src/main/**/build/
5+
!**/src/test/**/build/
6+
7+
### IntelliJ IDEA ###
8+
.idea/
9+
*.iws
10+
*.iml
11+
*.ipr
12+
out/
13+
!**/src/main/**/out/
14+
!**/src/test/**/out/
15+
16+
### Kotlin ###
17+
.kotlin
18+
19+
### Eclipse ###
20+
.apt_generated
21+
.classpath
22+
.factorypath
23+
.project
24+
.settings
25+
.springBeans
26+
.sts4-cache
27+
bin/
28+
!**/src/main/**/bin/
29+
!**/src/test/**/bin/
30+
31+
### NetBeans ###
32+
/nbproject/private/
33+
/nbbuild/
34+
/dist/
35+
/nbdist/
36+
/.nb-gradle/
37+
38+
### VS Code ###
39+
.vscode/
40+
41+
### Mac OS ###
42+
.DS_Store

samples/kotlin-mcp-client/README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
kotlin("jvm") version "2.1.10"
3+
application
4+
id("com.github.johnrengelman.shadow") version "8.1.1"
5+
}
6+
7+
application {
8+
mainClass.set("io.modelcontextprotocol.sample.client.MainKt")
9+
}
10+
11+
12+
group = "org.example"
13+
version = "0.1.0"
14+
15+
val mcpVersion = "0.3.0"
16+
val slf4jVersion = "2.0.9"
17+
val anthropicVersion = "0.8.0"
18+
19+
dependencies {
20+
implementation("io.modelcontextprotocol:kotlin-sdk:$mcpVersion")
21+
implementation("org.slf4j:slf4j-nop:$slf4jVersion")
22+
implementation("com.anthropic:anthropic-java:$anthropicVersion")
23+
}
24+
25+
tasks.test {
26+
useJUnitPlatform()
27+
}
28+
29+
kotlin {
30+
jvmToolchain(17)
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kotlin.code.style=official
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)