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
Jeff Clyne 2f1b659efd
Adds 'instructions' string to the Server and propagates to the InitializeResult (#290)
<!-- Provide a brief summary of your changes -->

## Motivation and Context
The
[schema](cca15204f6/schema/2025-03-26/schema.ts (L192))
for the InitializeResult contains an optional 'instructions' string

## Additional context
The instructions string is included in the schema for for version
"2025-03-26"

The instructions in the Server class are given as a provider function
with the result of the provider propagated to the underlying
ServerSession. As the ServerSession change is meant to allow for a
'Singleton scoped' server class, this provides the ability to
dynamically modify the instructions at the server level but have them be
immutable per session.
 
This also refactors the `serverInfo` and `instructions` properties to be
protected. This `Server` class is open and meant to be overridden, there
is no harm in having these immutable properties available to subclasses.

This adds the serverInstructions to the client as returned from the
InitializeResult. This is used to verify the response in the Server
tests.
2025-10-03 18:12:37 +02:00
.github migration from jreleaser to mavenPublish plugin (#277) 2025-09-17 13:07:07 +02:00
buildSrc Update signing key environment variable (#283) 2025-09-19 15:42:51 +02:00
docs Add module documentation and enhance Dokka configuration (#261) 2025-09-15 17:44:43 +02:00
gradle Generate MCP classes with OpenAPI Generator (#294) 2025-09-29 12:34:32 +03:00
kotlin-sdk Prerelease 0.7.2-alpha1 (#278) 2025-09-18 15:18:12 +02:00
kotlin-sdk-client Adds 'instructions' string to the Server and propagates to the InitializeResult (#290) 2025-10-03 18:12:37 +02:00
kotlin-sdk-core Adds 'instructions' string to the Server and propagates to the InitializeResult (#290) 2025-10-03 18:12:37 +02: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 Adds 'instructions' string to the Server and propagates to the InitializeResult (#290) 2025-10-03 18:12:37 +02:00
samples Introduce server sessions (#198) 2025-09-29 10:10:07 +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 Generate MCP classes with OpenAPI Generator (#294) 2025-09-29 12:34:32 +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 Adds 'instructions' string to the Server and propagates to the InitializeResult (#290) 2025-10-03 18:12:37 +02: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

Kotlin Multiplatform Platforms Maven Central 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.