This Kotlin SDK implements the Model Context Protocol, enabling developers to build MCP clients and servers for standardized LLM context management. It supports various transports and simplifies integration with LLM surfaces.
Kotlin implementation of the Model Context Protocol (MCP), designed for integrating with LLM surfaces by providing both client and server functionalities.
The MCP allows applications to provide context for LLMs in a standardized manner, separating context provision from LLM interaction. This SDK implements the MCP specification, facilitating the creation of MCP clients to connect to servers and MCP servers to expose resources, prompts, and tools. It supports standard transports like stdio, SSE, and WebSocket, handling MCP protocol messages and lifecycle events.
kotlin-mcp-server
: Demonstrates setting up a Kotlin MCP server with various tools.weather-stdio-server
: Illustrates building a Kotlin MCP server for weather forecasts using STDIO.kotlin-mcp-client
: Shows an interactive Kotlin MCP client connecting to an MCP server via STDIO and integrating with Anthropic’s API.Add the repository to your build file:
repositories {
mavenCentral()
}
Add the dependency:
dependencies {
implementation("io.modelcontextprotocol:kotlin-sdk:0.4.0")
}
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")
)
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)
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)
)
)
)
}
}
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)
)
)
)
}
}
}
}
Please see the contribution guide and the Code of conduct before contributing.
This project is licensed under the MIT License⌘see the LICENSE file for details.
modelcontextprotocol/kotlin-sdk
December 16, 2024
March 28, 2025
Kotlin