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
Sergey Ignatov d8a8f89465
0.2.0 (#27)
2024-12-22 11:58:23 +01:00
.github/workflows Cleanup gradle configuration 2024-12-17 17:43:08 +01:00
.idea Enable Explicit Api (#21) 2024-12-16 20:59:53 +01:00
docs Update package name across the project (#2) 2024-12-17 07:28:51 +01:00
gradle Introduce jreleaser to make the release (#19) 2024-12-17 15:55:27 +01:00
samples/kotlin-mcp-server 0.2.0 (#27) 2024-12-22 11:58:23 +01:00
src Accept requestID as a string #25 (#26) 2024-12-22 08:43:46 +01:00
.gitignore Initial commit 2024-12-15 20:17:55 +01:00
build.gradle.kts 0.2.0 (#27) 2024-12-22 11:58: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 Initial commit 2024-12-15 20:17:55 +01:00
gradlew first commit 2024-12-12 15:41:03 +01:00
gradlew.bat first commit 2024-12-12 15:41:03 +01:00
LICENSE Update license 2024-12-16 21:20:35 +01:00
README.md 0.2.0 (#27) 2024-12-22 11:58:23 +01:00
settings.gradle.kts Update package name across the project (#2) 2024-12-17 07:28:51 +01:00

MCP Kotlin SDK

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

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 Kotlin SDK implements the full MCP specification, making it easy to:

  • 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: shows how to set up a Kotlin MCP server with different tools and other features.

Installation

Add the new repository to your build file:

repositories {
    mavenCentral()
}

Add the dependency:

dependencies {
    implementation("io.modelcontextprotocol:kotlin-sdk:0.2.0")
}

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
            )
        )
    )
)

// 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

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)
                )
            )
        )
    }
}

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.