kotlin-sdk/samples/kotlin-mcp-client
Repository files (latest commit first)
Filename Latest commit message Latest commit date
木葉 Scarlet b1ba0cd371
Remove dependency on CIO engine (#223)
<!-- Provide a brief summary of your changes -->
Replace the CIO dependency with only server/client core dependency.

## Motivation and Context
<!-- Why is this change needed? What problem does it solve? -->
In order to minimize the size of package. There are probably other
existing engines like Netty/Tomcat(server) or Java/OkHttp/Apache(client)
in the classpath. So we can reuse existing infrastructure.

## How Has This Been Tested?
<!-- Have you tested this in a real application? Which scenarios were
tested? -->
I have run:
- kotlin-sdk-client: jvmTest, jsTest
- kotlin-sdk-test: jvmTest, jsTest
- kotlin-sdk-server: allTests
- All `build` of samples

I have an issue about `samples/kotlin-mcp-server`.
`io.modelcontextprotocol` is not found in `commonMain`.

## Breaking Changes
<!-- Will users need to update their code or configurations? -->
Yes. They need to `implementation` CIO engine manually if they use it in
their app.

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to change)
- [x] Documentation update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [ ] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [ ] My code follows the repository's style guidelines
- [x] New and existing tests pass locally
- [ ] I have added appropriate error handling
- [ ] I have added or updated documentation as needed

## Additional context
<!-- Add any other context, implementation notes, or design decisions
-->

closes https://github.com/modelcontextprotocol/kotlin-sdk/issues/111

---------

Co-authored-by: Konstantin Pavlov <1517853+kpavlov@users.noreply.github.com>
2025-09-11 14:30:36 +03:00
..
src/main/kotlin/io/modelcontextprotocol/sample/client add client sample (#55) 2025-03-18 17:49:04 +00: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