kotlin-sdk/samples/kotlin-mcp-client
Repository files (latest commit first)
Filename Latest commit message Latest commit date
dependabot[bot] 1d6cb24204
Bump com.anthropic:anthropic-java from 0.8.0 to 2.6.0 (#246)
Bumps
[com.anthropic:anthropic-java](https://github.com/anthropics/anthropic-sdk-java)
from 0.8.0 to 2.6.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/anthropics/anthropic-sdk-java/releases">com.anthropic:anthropic-java's
releases</a>.</em></p>
<blockquote>
<h2>v2.6.0</h2>
<h2>2.6.0 (2025-09-02)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.5.1...v2.6.0">v2.5.1...v2.6.0</a></p>
<h3>Features</h3>
<ul>
<li><strong>client:</strong> adds support for code-execution-2025-08-26
tool (<a
href="da4c57b12d">da4c57b</a>)</li>
</ul>
<h3>Bug Fixes</h3>
<ul>
<li><strong>client:</strong> ensure correct binary content type (<a
href="197fdd5433">197fdd5</a>)</li>
<li>fix casing issue (<a
href="c2c50097e4">c2c5009</a>)</li>
<li>update singularization rules (<a
href="afec494c85">afec494</a>)</li>
</ul>
<h3>Performance Improvements</h3>
<ul>
<li><strong>internal:</strong> make formatting faster (<a
href="7c36322f9f">7c36322</a>)</li>
</ul>
<h3>Chores</h3>
<ul>
<li><strong>api:</strong> remove unsupported endpoints (<a
href="d9b7be334b">d9b7be3</a>)</li>
<li><strong>ci:</strong> add build job (<a
href="061df8e702">061df8e</a>)</li>
<li><strong>ci:</strong> reduce log noise (<a
href="b91d7779f1">b91d777</a>)</li>
<li><strong>client:</strong> refactor closing / shutdown (<a
href="f0c796d723">f0c796d</a>)</li>
<li><strong>client:</strong> rename parameter (<a
href="197fdd5433">197fdd5</a>)</li>
<li><strong>internal:</strong> dynamically determine included projects
(<a
href="93db169c6c">93db169</a>)</li>
<li><strong>internal:</strong> run formatter (<a
href="06cd69ff9d">06cd69f</a>)</li>
<li><strong>internal:</strong> support passing arguments to test script
(<a
href="8528c63f15">8528c63</a>)</li>
<li><strong>internal:</strong> support running formatters directly (<a
href="e0de72e934">e0de72e</a>)</li>
<li>remove memory upper bound from publishing step (<a
href="cb2ac3b70f">cb2ac3b</a>)</li>
<li><strong>tests:</strong> setup cache creation in MessageAccumulator
test (<a
href="d5de4438df">d5de443</a>)</li>
</ul>
<h2>v2.5.1</h2>
<h2>2.5.1 (2025-08-13)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.5.0...v2.5.1">v2.5.0...v2.5.1</a></p>
<h3>Chores</h3>
<ul>
<li>deprecate older claude-3-5 sonnet models (<a
href="https://redirect.github.com/anthropics/anthropic-sdk-java/issues/591">#591</a>)
(<a
href="c7c4cc1a08">c7c4cc1</a>)</li>
<li><strong>internal:</strong> format identity methods (<a
href="483576abc4">483576a</a>)</li>
</ul>
<h2>v2.5.0</h2>
<h2>2.5.0 (2025-08-12)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.4.0...v2.5.0">v2.4.0...v2.5.0</a></p>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/anthropics/anthropic-sdk-java/blob/main/CHANGELOG.md">com.anthropic:anthropic-java's
changelog</a>.</em></p>
<blockquote>
<h2>2.6.0 (2025-09-02)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.5.1...v2.6.0">v2.5.1...v2.6.0</a></p>
<h3>Features</h3>
<ul>
<li><strong>client:</strong> adds support for code-execution-2025-08-26
tool (<a
href="da4c57b12d">da4c57b</a>)</li>
</ul>
<h3>Bug Fixes</h3>
<ul>
<li><strong>client:</strong> ensure correct binary content type (<a
href="197fdd5433">197fdd5</a>)</li>
<li>fix casing issue (<a
href="c2c50097e4">c2c5009</a>)</li>
<li>update singularization rules (<a
href="afec494c85">afec494</a>)</li>
</ul>
<h3>Performance Improvements</h3>
<ul>
<li><strong>internal:</strong> make formatting faster (<a
href="7c36322f9f">7c36322</a>)</li>
</ul>
<h3>Chores</h3>
<ul>
<li><strong>api:</strong> remove unsupported endpoints (<a
href="d9b7be334b">d9b7be3</a>)</li>
<li><strong>ci:</strong> add build job (<a
href="061df8e702">061df8e</a>)</li>
<li><strong>ci:</strong> reduce log noise (<a
href="b91d7779f1">b91d777</a>)</li>
<li><strong>client:</strong> refactor closing / shutdown (<a
href="f0c796d723">f0c796d</a>)</li>
<li><strong>client:</strong> rename parameter (<a
href="197fdd5433">197fdd5</a>)</li>
<li><strong>internal:</strong> dynamically determine included projects
(<a
href="93db169c6c">93db169</a>)</li>
<li><strong>internal:</strong> run formatter (<a
href="06cd69ff9d">06cd69f</a>)</li>
<li><strong>internal:</strong> support passing arguments to test script
(<a
href="8528c63f15">8528c63</a>)</li>
<li><strong>internal:</strong> support running formatters directly (<a
href="e0de72e934">e0de72e</a>)</li>
<li>remove memory upper bound from publishing step (<a
href="cb2ac3b70f">cb2ac3b</a>)</li>
<li><strong>tests:</strong> setup cache creation in MessageAccumulator
test (<a
href="d5de4438df">d5de443</a>)</li>
</ul>
<h2>2.5.1 (2025-08-13)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.5.0...v2.5.1">v2.5.0...v2.5.1</a></p>
<h3>Chores</h3>
<ul>
<li>deprecate older claude-3-5 sonnet models (<a
href="https://redirect.github.com/anthropics/anthropic-sdk-java/issues/591">#591</a>)
(<a
href="c7c4cc1a08">c7c4cc1</a>)</li>
<li><strong>internal:</strong> format identity methods (<a
href="483576abc4">483576a</a>)</li>
</ul>
<h2>2.5.0 (2025-08-12)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.4.0...v2.5.0">v2.4.0...v2.5.0</a></p>
<h3>Features</h3>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="42fce65f72"><code>42fce65</code></a>
release: 2.6.0</li>
<li><a
href="2b10838cc9"><code>2b10838</code></a>
feat(client): adds support for code-execution-2025-08-26 tool</li>
<li><a
href="ab1254a8ab"><code>ab1254a</code></a>
chore(internal): run formatter</li>
<li><a
href="5611d37f31"><code>5611d37</code></a>
chore(tests): setup cache creation in MessageAccumulator test</li>
<li><a
href="95918dc6c0"><code>95918dc</code></a>
codegen metadata</li>
<li><a
href="d4fffce28f"><code>d4fffce</code></a>
codegen metadata</li>
<li><a
href="f02db72ed9"><code>f02db72</code></a>
codegen metadata</li>
<li><a
href="2e258deed3"><code>2e258de</code></a>
codegen metadata</li>
<li><a
href="31c46ff041"><code>31c46ff</code></a>
fix: fix casing issue</li>
<li><a
href="6fdc91169f"><code>6fdc911</code></a>
fix: update singularization rules</li>
<li>Additional commits viewable in <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v0.8.0...v2.6.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=com.anthropic:anthropic-java&package-manager=gradle&previous-version=0.8.0&new-version=2.6.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Konstantin Pavlov <1517853+kpavlov@users.noreply.github.com>
2025-09-12 13:20:40 +02:00
..
src/main/kotlin/io/modelcontextprotocol/sample/client Bump com.anthropic:anthropic-java from 0.8.0 to 2.6.0 (#246) 2025-09-12 13:20:40 +02:00
build.gradle.kts Remove dependency on CIO engine (#223) 2025-09-11 14:30:36 +03:00
README.md add client sample (#55) 2025-03-18 17:49:04 +00:00
settings.gradle.kts modularize sdk (#208) 2025-08-04 11:53:38 +02:00

Kotlin MCP Client

This project demonstrates how to build a Model Context Protocol (MCP) client in Kotlin that interacts with an MCP server via a STDIO transport layer while leveraging Anthropic's API for natural language processing. The client uses the MCP Kotlin SDK to communicate with an MCP server that exposes various tools, and it uses Anthropic's API to process user queries and integrate tool responses into the conversation.

For more information about the MCP SDK and protocol, please refer to the MCP documentation.

Prerequisites

  • Java 17 or later
  • Gradle (or the Gradle wrapper provided with the project)
  • An Anthropic API key set in your environment variable ANTHROPIC_API_KEY
  • Basic understanding of MCP concepts and Kotlin programming

Overview

The client application performs the following tasks:

  • Connecting to an MCP server — launches an MCP server process (implemented in JavaScript, Python, or Java) using STDIO transport. It connects to the server, retrieves available tools, and converts them to Anthropic’s tool format.
  • Processing queries — accepts user queries, sends them to Anthropic’s API along with the registered tools, and handles responses. If the response indicates a tool should be called, it invokes the corresponding MCP tool and continues the conversation based on the tool’s result.
  • Interactive chat loop — runs an interactive command-line loop, allowing users to continuously submit queries and receive responses.

Building and Running

Use the Gradle wrapper to build the application. In a terminal, run:

./gradlew clean build -x test

To run the client, execute the jar file and provide the path to your MCP server script.

To run the client with any MCP server:

java -jar build/libs/<your-jar-name>.jar path/to/server.jar # jvm server
java -jar build/libs/<your-jar-name>.jar path/to/server.py # python server
java -jar build/libs/<your-jar-name>.jar path/to/build/index.js # node server

Note

The client uses STDIO transport, so it launches the MCP server as a separate process. Ensure the server script is executable and is a valid .js, .py, or .jar file.

Configuration for Anthropic

Ensure your Anthropic API key is available in your environment:

export ANTHROPIC_API_KEY=your_anthropic_api_key_here

The client uses AnthropicOkHttpClient.fromEnv() to automatically load the API key from ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN environment variables.

Additional Resources