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
Konstantin Pavlov 6ad9bf2122
fix(integration-test): refactor integration test infrastructure, fix and enable tests with ts-client (#478)
# Refactor integration test infrastructure, fix and enable tests with
ts-client

- Refactor TypeScript integration test infrastructure to support
parallel execution and real I/O operations.
- Extract reusable TypeScriptClient component
- Migrate some tests from `runTest` to `runBlocking` for tests that
spawn actual processes and communicate over real network connections.

## Key Changes 

  - Extract `TypeScriptClient` class for managing TS client processes
- Move client scripts to integration-test/src/jvmTest/typescript/client/
- Migrate typescript: `myClient.ts`(2) → `sse-client.ts`,
`stdio-client.ts`
  - Automate TypeScript dependency installation in static initializer
  
- Enable JUnit parallel execution (dynamic strategy, concurrent mode)
with test timeout of 2 minutes
- Replace `mutableListOf` with `ConcurrentLinkedQueue` for thread-safe
notification handling
  - Migrate integration tests from `runTest` to `runBlocking`
**Rationale:** Integration tests that spawn real TypeScript processes,
establish actual
HTTP/SSE connections, and manage server lifecycles require real-time I/O
behavior. The runTest framework's virtual time is designed for fast unit
tests with simulated timing, not for integration scenarios where
external processes and network communication must occur in real time.
Parallel execution requires thread-safe data structures
(ConcurrentLinkedQueue)
to safely collect notifications from multiple concurrent test instances.

  - Add `EmbeddedServer.actualPort()` extension to get bound port
  - Standardize kotlin.time.Duration usage across test utilities
  - Update processUtils.stopProcess() to accept Duration parameter
  - Replace RuntimeException checks with idiomatic check() calls
  - Revert `--rerun-tasks` flag to Gradle workflows for reliable builds

## Motivation and Context

Integration tests with typescript client are now broken. Enabling
parallel test execution will force writing thread-safe independent tests
and will highlight problems faster.

## How Has This Been Tested?
CI

## Breaking Changes
<!-- Will users need to update their code or configurations? -->

## Types of changes
<!-- What types of changes does your code introduce? Put an `x` in all
the boxes that apply: -->
- [x] CI Update
- [x] Test Fix

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] 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
Follow up for #477
2026-01-11 09:38:13 +02:00
.github fix(integration-test): refactor integration test infrastructure, fix and enable tests with ts-client (#478) 2026-01-11 09:38:13 +02:00
buildSrc Update JVM target to 11 in Gradle build configuration (#394) 2025-11-12 13:40:44 +02:00
conformance-test refactor(test): add centralized TypeScript server factory for integration tests (#477) 2026-01-10 20:46:51 +02:00
docs Add module documentation and enhance Dokka configuration (#261) 2025-09-15 17:44:43 +02:00
gradle Bump io.netty:netty-bom from 4.2.7.Final to 4.2.9.Final (#468) 2026-01-10 00:01:31 +02:00
integration-test fix(integration-test): refactor integration test infrastructure, fix and enable tests with ts-client (#478) 2026-01-11 09:38:13 +02:00
kotlin-sdk Kotlin-MCP can't be used without ktor-bom starting from 0.7.5 (#390) (#392) 2025-11-12 15:32:54 +02:00
kotlin-sdk-client Fix flaky StdioClientTransportErrorHandlingTest (#457) 2025-12-08 13:57:08 +02:00
kotlin-sdk-core Mark DSL functions for types as public (#472) 2026-01-10 01:12:29 +02:00
kotlin-sdk-server fix: Update/Fix build workflow (#474) 2026-01-09 22:48:31 +02:00
samples Fix kotlin-mcp-server sample (#459) 2025-12-08 23:25:55 +02:00
test-utils fix(integration-test): refactor integration test infrastructure, fix and enable tests with ts-client (#478) 2026-01-11 09:38:13 +02:00
.editorconfig Add ktlint (#213) 2025-08-05 13:10:44 +00:00
.gitignore Add MCP conformance test coverage (#435) 2025-12-01 18:02:34 +02:00
AGENTS.md Mark DSL functions for types as public (#472) 2026-01-10 01:12:29 +02:00
build.gradle.kts test: Refactor test utilities, fix conformance (#476) 2026-01-10 16:51:15 +02: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 Prepare for next development iteration (#447) 2025-12-04 18:22:48 +02:00
gradlew Update Gradle to version 9.2.0 across all projects (#377) 2025-11-07 17:07:20 +02:00
gradlew.bat Update Gradle to version 9.2.0 across all projects (#377) 2025-11-07 17:07:20 +02:00
LICENSE Update license 2024-12-16 21:20:35 +01:00
README.md Update README.md 2026-01-09 21:00:14 +02:00
settings.gradle.kts test: Refactor test utilities, fix conformance (#476) 2026-01-10 16:51:15 +02:00

MCP Kotlin SDK

Maven Central Build Conformance Tests 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.