Mirror of https://github.com/modelcontextprotocol/kotlin-sdk — The official Kotlin SDK for Model Context Protocol servers and clients. Maintained in collaboration with JetBrains
  • Kotlin 98%
  • TypeScript 1.7%
  • Shell 0.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
dependabot[bot] e026069d2f
Bump com.anthropic:anthropic-java from 2.7.0 to 2.9.0 (#323)
Bumps
[com.anthropic:anthropic-java](https://github.com/anthropics/anthropic-sdk-java)
from 2.7.0 to 2.9.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.9.0</h2>
<h2>2.9.0 (2025-10-15)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.8.1...v2.9.0">v2.8.1...v2.9.0</a></p>
<h3>Features</h3>
<ul>
<li>add more helper methods to instantiate union objects (<a
href="3e3a4bbab3">3e3a4bb</a>)</li>
<li><strong>api:</strong> manual updates (<a
href="d2170e9bf1">d2170e9</a>)</li>
</ul>
<h2>v2.8.1</h2>
<h2>2.8.1 (2025-09-29)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.8.0...v2.8.1">v2.8.0...v2.8.1</a></p>
<h3>Chores</h3>
<ul>
<li><strong>client:</strong> add context-management-2025-06-27 beta
header (<a
href="b6c56838bc">b6c5683</a>)</li>
<li><strong>client:</strong> add
model-context-window-exceeded-2025-08-26 beta header (<a
href="d9d0493fc7">d9d0493</a>)</li>
</ul>
<h2>v2.8.0</h2>
<h2>2.8.0 (2025-09-29)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.7.0...v2.8.0">v2.7.0...v2.8.0</a></p>
<h3>Features</h3>
<ul>
<li><strong>api:</strong> adds support for Claude Sonnet 4.5 and context
management features (<a
href="c286ab85ad">c286ab8</a>)</li>
<li><strong>client:</strong> expose sleeper option (<a
href="ca9ce4f05f">ca9ce4f</a>)</li>
</ul>
<h3>Bug Fixes</h3>
<ul>
<li><strong>client:</strong> deserialization of empty objects (<a
href="af93581b6a">af93581</a>)</li>
<li><strong>client:</strong> ensure single timer is created per client
(<a
href="ca9ce4f05f">ca9ce4f</a>)</li>
<li><strong>client:</strong> incorrect <code>getPackageVersion</code>
impl (<a
href="a8b170b391">a8b170b</a>)</li>
</ul>
<h3>Chores</h3>
<ul>
<li>improve formatter performance (<a
href="c57630b565">c57630b</a>)</li>
<li>improve formatter performance (<a
href="100e6adc25">100e6ad</a>)</li>
<li><strong>internal:</strong> change some comment formatting (<a
href="f725d582e1">f725d58</a>)</li>
<li><strong>internal:</strong> codegen related update (<a
href="14a3c9cc52">14a3c9c</a>)</li>
<li><strong>internal:</strong> codegen related update (<a
href="e6fddb403c">e6fddb4</a>)</li>
<li><strong>internal:</strong> fix tests (<a
href="cc05673958">cc05673</a>)</li>
</ul>
</blockquote>
</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.9.0 (2025-10-15)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.8.1...v2.9.0">v2.8.1...v2.9.0</a></p>
<h3>Features</h3>
<ul>
<li>add more helper methods to instantiate union objects (<a
href="3e3a4bbab3">3e3a4bb</a>)</li>
<li><strong>api:</strong> manual updates (<a
href="d2170e9bf1">d2170e9</a>)</li>
</ul>
<h2>2.8.1 (2025-09-29)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.8.0...v2.8.1">v2.8.0...v2.8.1</a></p>
<h3>Chores</h3>
<ul>
<li><strong>client:</strong> add context-management-2025-06-27 beta
header (<a
href="b6c56838bc">b6c5683</a>)</li>
<li><strong>client:</strong> add
model-context-window-exceeded-2025-08-26 beta header (<a
href="d9d0493fc7">d9d0493</a>)</li>
</ul>
<h2>2.8.0 (2025-09-29)</h2>
<p>Full Changelog: <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.7.0...v2.8.0">v2.7.0...v2.8.0</a></p>
<h3>Features</h3>
<ul>
<li><strong>api:</strong> adds support for Claude Sonnet 4.5 and context
management features (<a
href="c286ab85ad">c286ab8</a>)</li>
<li><strong>client:</strong> expose sleeper option (<a
href="ca9ce4f05f">ca9ce4f</a>)</li>
</ul>
<h3>Bug Fixes</h3>
<ul>
<li><strong>client:</strong> deserialization of empty objects (<a
href="af93581b6a">af93581</a>)</li>
<li><strong>client:</strong> ensure single timer is created per client
(<a
href="ca9ce4f05f">ca9ce4f</a>)</li>
<li><strong>client:</strong> incorrect <code>getPackageVersion</code>
impl (<a
href="a8b170b391">a8b170b</a>)</li>
</ul>
<h3>Chores</h3>
<ul>
<li>improve formatter performance (<a
href="c57630b565">c57630b</a>)</li>
<li>improve formatter performance (<a
href="100e6adc25">100e6ad</a>)</li>
<li><strong>internal:</strong> change some comment formatting (<a
href="f725d582e1">f725d58</a>)</li>
<li><strong>internal:</strong> codegen related update (<a
href="14a3c9cc52">14a3c9c</a>)</li>
<li><strong>internal:</strong> codegen related update (<a
href="e6fddb403c">e6fddb4</a>)</li>
<li><strong>internal:</strong> fix tests (<a
href="cc05673958">cc05673</a>)</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="f93cdaa53f"><code>f93cdaa</code></a>
release: 2.9.0</li>
<li><a
href="3a37cfc3a8"><code>3a37cfc</code></a>
feat(api): manual updates</li>
<li><a
href="d581f2aad2"><code>d581f2a</code></a>
feat: add more helper methods to instantiate union objects</li>
<li><a
href="5a0a815be5"><code>5a0a815</code></a>
codegen metadata</li>
<li><a
href="4f816b21fb"><code>4f816b2</code></a>
release: 2.8.1</li>
<li><a
href="dec4738aea"><code>dec4738</code></a>
chore(client): add model-context-window-exceeded-2025-08-26 beta
header</li>
<li><a
href="1479b2c5bf"><code>1479b2c</code></a>
chore(client): add context-management-2025-06-27 beta header</li>
<li><a
href="ef6a706593"><code>ef6a706</code></a>
release: 2.8.0</li>
<li><a
href="618bb972e8"><code>618bb97</code></a>
feat(api): adds support for Claude Sonnet 4.5 and context management
features</li>
<li><a
href="1c6be51c97"><code>1c6be51</code></a>
fix(client): deserialization of empty objects</li>
<li>Additional commits viewable in <a
href="https://github.com/anthropics/anthropic-sdk-java/compare/v2.7.0...v2.9.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=2.7.0&new-version=2.9.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>
2025-10-22 15:04:32 +03:00
.github #302 [part 1] Add GitHub workflow to validate samples (#327) 2025-10-22 14:20:52 +03:00
buildSrc Add StreamableHttpClientTest with test infrastructure setup and dependencies (#316) 2025-10-17 16:03:52 +03:00
docs Add module documentation and enhance Dokka configuration (#261) 2025-09-15 17:44:43 +02:00
gradle Bump com.anthropic:anthropic-java from 2.7.0 to 2.9.0 (#323) 2025-10-22 15:04:32 +03:00
kotlin-sdk Prerelease 0.7.2-alpha1 (#278) 2025-09-18 15:18:12 +02:00
kotlin-sdk-client Fix Streamable handling to support JSON-only responses (#309) 2025-10-21 14:05:39 +02:00
kotlin-sdk-core Bump org.openapi.generator from 7.15.0 to 7.16.0 (#305) 2025-10-13 22:47:35 +03:00
kotlin-sdk-server Adds 'instructions' string to the Server and propagates to the InitializeResult (#290) 2025-10-03 18:12:37 +02:00
kotlin-sdk-test Add StreamableHttpClientTest with test infrastructure setup and dependencies (#316) 2025-10-17 16:03:52 +03:00
samples Change sample server host from 0.0.0.0 to 127.0.0.1 (#307) 2025-10-08 11:59:04 +03:00
.editorconfig Add ktlint (#213) 2025-08-05 13:10:44 +00:00
.gitignore Add module documentation and enhance Dokka configuration (#261) 2025-09-15 17:44:43 +02:00
build.gradle.kts Update badges in README and set version to 0.7.4-SNAPSHOT (#326) 2025-10-22 13:59:17 +03:00
CODE_OF_CONDUCT.md Update license 2024-12-16 21:20:35 +01:00
CONTRIBUTING.md Update package name across the project (#2) 2024-12-17 07:28:51 +01:00
gradle.properties #196 Update build configuration and workflows for enhanced local publ… (#275) 2025-09-16 21:43:50 +03:00
gradlew Fix release (#155) 2025-07-18 14:08:05 +00:00
gradlew.bat Fix release (#155) 2025-07-18 14:08:05 +00:00
LICENSE Update license 2024-12-16 21:20:35 +01:00
README.md Update badges in README and set version to 0.7.4-SNAPSHOT (#326) 2025-10-22 13:59:17 +03:00
settings.gradle.kts Conditionally imports the samples only if its the root project (#292) 2025-09-27 14:54:39 +03:00

MCP Kotlin SDK

Maven Central Build Kotlin Kotlin Multiplatform JVM License

Kotlin Multiplatform implementation of the Model Context Protocol (MCP), providing both client and server capabilities for integrating with LLM surfaces across various platforms.

Overview

The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This SDK implements the MCP specification for Kotlin, enabling you to build applications that can communicate using MCP on the JVM, WebAssembly and iOS.

  • Build MCP clients that can connect to any MCP server
  • Create MCP servers that expose resources, prompts and tools
  • Use standard transports like stdio, SSE, and WebSocket
  • Handle all MCP protocol messages and lifecycle events

Samples

  • kotlin-mcp-server: demonstrates a multiplatform (JVM, Wasm) MCP server setup with various features and transports.
  • weather-stdio-server: shows how to build a Kotlin MCP server providing weather forecast and alerts using STDIO transport.
  • kotlin-mcp-client: demonstrates building an interactive Kotlin MCP client that connects to an MCP server via STDIO and integrates with Anthropic’s API.

Installation

Add the new repository to your build file:

repositories {
    mavenCentral()
}

Add the dependency:

dependencies {
    // See the badge above for the latest version
    implementation("io.modelcontextprotocol:kotlin-sdk:$mcpVersion")
}

MCP SDK uses Ktor, but does not come with a specific engine dependency. You should add Ktor client and/or Ktor server dependency to your project yourself, e.g.:

dependencies {
    // for client:
    implementation("io.ktor:ktor-client-cio:$ktorVersion")
    // for server:
    implementation("io.ktor:ktor-server-netty:$ktorVersion")
    
}

Quick Start

Creating a Client

import io.modelcontextprotocol.kotlin.sdk.client.Client
import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport
import io.modelcontextprotocol.kotlin.sdk.Implementation

val client = Client(
    clientInfo = Implementation(
        name = "example-client",
        version = "1.0.0"
    )
)

val transport = StdioClientTransport(
    inputStream = processInputStream,
    outputStream = processOutputStream
)

// Connect to server
client.connect(transport)

// List available resources
val resources = client.listResources()

// Read a specific resource
val resourceContent = client.readResource(
    ReadResourceRequest(uri = "file:///example.txt")
)

Creating a Server

import io.modelcontextprotocol.kotlin.sdk.server.Server
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities

val server = Server(
    serverInfo = Implementation(
        name = "example-server",
        version = "1.0.0"
    ),
    options = ServerOptions(
        capabilities = ServerCapabilities(
            resources = ServerCapabilities.Resources(
                subscribe = true,
                listChanged = true
            )
        )
    )
){ 
    "This server provides example resources and demonstrates MCP capabilities." 
}

// Add a resource
server.addResource(
    uri = "file:///example.txt",
    name = "Example Resource",
    description = "An example text file",
    mimeType = "text/plain"
) { request ->
    ReadResourceResult(
        contents = listOf(
            TextResourceContents(
                text = "This is the content of the example resource.",
                uri = request.uri,
                mimeType = "text/plain"
            )
        )
    )
}

// Start server with stdio transport
val transport = StdioServerTransport()
server.connect(transport)

Using SSE Transport

Directly in Ktor's Application:

import io.ktor.server.application.*
import io.modelcontextprotocol.kotlin.sdk.server.mcp

fun Application.module() {
    mcp {
        Server(
            serverInfo = Implementation(
                name = "example-sse-server",
                version = "1.0.0"
            ),
            options = ServerOptions(
                capabilities = ServerCapabilities(
                    prompts = ServerCapabilities.Prompts(listChanged = null),
                    resources = ServerCapabilities.Resources(subscribe = null, listChanged = null)
                )
            ),
        ) { 
            "This SSE server provides prompts and resources via Server-Sent Events."
        }
    }
}

Inside a custom Ktor's Route:

import io.ktor.server.application.*
import io.ktor.server.sse.SSE
import io.modelcontextprotocol.kotlin.sdk.server.mcp

fun Application.module() {
    install(SSE)

    routing {
        route("myRoute") {
            mcp {
                Server(
                    serverInfo = Implementation(
                        name = "example-sse-server",
                        version = "1.0.0"
                    ),
                    options = ServerOptions(
                        capabilities = ServerCapabilities(
                            prompts = ServerCapabilities.Prompts(listChanged = null),
                            resources = ServerCapabilities.Resources(subscribe = null, listChanged = null)
                        )
                    ),
                ) {
                    "Connect via SSE to interact with this MCP server."
                }
            }
        }
    }
}

Contributing

Please see the contribution guide and the Code of conduct before contributing.

License

This project is licensed under the MIT License—see the LICENSE file for details.