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
Pavel Gorgulov 21393e7ce3
New MCP Schema types (#386)
Implement the last version of the mcp schema and complete restructuring
of the mcp schema implementation from a monolithic `types.kt` file into
separate files.

- #158 
- #185 
- #353 
- #298 
- #248 

## Motivation and Context
The previous implementation of the mcp schema was outdated and contained
several issues. In addition, it stored all mcp types in a single large
file, which made maintenance and further development more difficult

## How Has This Been Tested?
All tests passed

## Breaking Changes
Yes. The new types are now located in a separate package. Additionally,
many types have new parameters and modified constructors

## Types of changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
- [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
- [x] I have added appropriate error handling
- [x] I have added or updated documentation as needed

## Additional context
to adopt the new structure:
**Before:**
```kotlin
  import io.modelcontextprotocol.kotlin.sdk.InitializeRequest
  import io.modelcontextprotocol.kotlin.sdk.Resource
```

**After:**
```kotlin
  import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequest
  import io.modelcontextprotocol.kotlin.sdk.types.Resource
```
2025-11-11 12:25:23 +01:00
.github Add Ktor BOM for dependency management across all modules, run Kotlin-MCP-Server Sample (#367) 2025-11-04 11:44:39 +02: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 Update Gradle to version 9.2.0 across all projects (#377) 2025-11-07 17:07:20 +02:00
kotlin-sdk Add Ktor BOM for dependency management across all modules, run Kotlin-MCP-Server Sample (#367) 2025-11-04 11:44:39 +02:00
kotlin-sdk-client New MCP Schema types (#386) 2025-11-11 12:25:23 +01:00
kotlin-sdk-core New MCP Schema types (#386) 2025-11-11 12:25:23 +01:00
kotlin-sdk-server New MCP Schema types (#386) 2025-11-11 12:25:23 +01:00
kotlin-sdk-test New MCP Schema types (#386) 2025-11-11 12:25:23 +01:00
samples New MCP Schema types (#386) 2025-11-11 12:25:23 +01:00
.editorconfig Add ktlint (#213) 2025-08-05 13:10:44 +00:00
.gitignore Update .gitignore to exclude kotlin-js-store directory (#343) 2025-10-30 13:19:06 +01:00
AGENTS.md Add AGENTS.md (#349) 2025-10-27 15:01:14 +01:00
build.gradle.kts New MCP Schema types (#386) 2025-11-11 12:25:23 +01: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 Bump version to 0.8.0-SNAPSHOT and update mcp-kotlin dependency in samples (#385) 2025-11-11 10:56:45 +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 badges in README and set version to 0.7.4-SNAPSHOT (#326) 2025-10-22 13:59:17 +03:00
settings.gradle.kts Exclude samples from composite gradle build (#337) 2025-10-23 18:41:57 +02: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.