11# MCP Kotlin SDK
22
3- Kotlin SDK for the Model Context Protocol (MCP).
4- This is a Kotlin Multiplatform library that helps you build MCP clients and servers that speak the same protocol and
5- share the same types.
6- The SDK focuses on clarity, small building blocks, and first‑class coroutine support.
3+ Kotlin Multiplatform implementation of the Model Context Protocol (MCP). The SDK focuses on clear, explicit APIs,
4+ small building blocks, and first-class coroutine support so clients and servers share the same well-typed messages and
5+ transports.
76
8- Use the umbrella ` kotlin-sdk ` artifact when you want a single dependency that brings the core types plus both client and
9- server toolkits. If you only need one side, depend on ` kotlin-sdk-client ` or ` kotlin-sdk-server ` directly.
10-
11- Gradle (Kotlin DSL):
7+ Use the umbrella ` kotlin-sdk ` artifact to bring in everything at once, or depend on the focused modules directly:
128
139``` kotlin
1410dependencies {
15- // Convenience bundle with everything you need to start
11+ // All-in-one bundle
1612 implementation(" io.modelcontextprotocol:kotlin-sdk:<version>" )
1713
18- // Or pick modules explicitly
14+ // Or pick sides explicitly
1915 implementation(" io.modelcontextprotocol:kotlin-sdk-client:<version>" )
2016 implementation(" io.modelcontextprotocol:kotlin-sdk-server:<version>" )
2117}
@@ -25,79 +21,73 @@ dependencies {
2521
2622## Module kotlin-sdk-core
2723
28- Foundational, platform‑agnostic pieces :
24+ Foundation shared by both sides :
2925
30- - Protocol data model and JSON serialization (kotlinx.serialization)
31- - Request/response and notification types used by both sides of MCP
32- - Coroutine‑friendly protocol engine and utilities
33- - Transport abstractions shared by client and server
26+ - Protocol data model for MCP requests, results, notifications, capabilities, and content types.
27+ - ` McpJson ` (kotlinx.serialization) with MCP-friendly defaults plus helpers for converting native values to JSON.
28+ - Transport abstractions ( ` Transport ` , ` AbstractTransport ` , ` WebSocketMcpTransport ` ) and streaming ` ReadBuffer ` .
29+ - ` Protocol ` base class that handles JSON-RPC framing, correlation, progress tokens, and capability assertions.
3430
35- You typically do not use ` core ` directly in application code; it is pulled in by the client/server modules. Use it
36- explicitly if you only need the protocol types or plan to implement a custom transport .
31+ Use ` core ` when you need the raw types or want to author a custom transport. Public APIs are explicit to keep the shared
32+ surface stable across platforms .
3733
3834---
3935
4036## Module kotlin-sdk-client
4137
42- High‑level client API for connecting to an MCP server and invoking its tools, prompts, and resources. Ships with several
43- transports:
38+ High-level client for connecting to MCP servers and invoking their features:
4439
45- - WebSocketClientTransport – low latency, full‑duplex
46- - SSEClientTransport – Server‑Sent Events over HTTP
47- - StdioClientTransport – CLI‑friendly stdio bridge
48- - StreamableHttpClientTransport – simple HTTP streaming
40+ - ` Client ` runtime (or ` mcpClient ` helper) performs the MCP handshake and exposes ` serverCapabilities ` ,
41+ ` serverVersion ` , and ` serverInstructions ` .
42+ - Typed operations for tools, prompts, resources, completion, logging, roots, sampling, and elicitation with capability
43+ enforcement.
44+ - Transports: ` StdioClientTransport ` , ` SSEClientTransport ` , ` WebSocketClientTransport ` , and ` StreamableHttpClientTransport ` ,
45+ plus Ktor client extensions for quick wiring.
4946
50- A minimal client:
47+ Minimal WebSocket client:
5148
5249``` kotlin
53- val client = Client (
54- clientInfo = Implementation (name = " sample-client" , version = " 1.0.0" )
50+ val client = mcpClient(
51+ clientInfo = Implementation (" sample-client" , " 1.0.0" ),
52+ clientOptions = ClientOptions (ClientCapabilities (tools = ClientCapabilities .Tools ())),
53+ transport = WebSocketClientTransport (" ws://localhost:8080/mcp" )
5554)
5655
57- client.connect(WebSocketClientTransport (" ws://localhost:8080/mcp" ))
58-
5956val tools = client.listTools()
60- val result = client.callTool(
61- name = " echo" ,
62- arguments = mapOf (" text" to " Hello, MCP!" )
63- )
57+ val result = client.callTool(" echo" , mapOf (" text" to " Hello, MCP!" ))
58+ println (result.content)
6459```
6560
6661---
6762
6863## Module kotlin-sdk-server
6964
70- Lightweight server toolkit for hosting MCP tools, prompts, and resources. It provides a small, composable API and
71- ready‑to‑use transports:
65+ Server toolkit for exposing MCP tools, prompts, and resources:
7266
73- - StdioServerTransport – integrates well with CLIs and editors
74- - SSE/WebSocket helpers for Ktor – easy HTTP deployment
67+ - ` Server ` runtime coordinates sessions, initialization flow, and capability enforcement with registries for tools,
68+ prompts, resources, and templates.
69+ - Transports: ` StdioServerTransport ` for CLI/editor bridges; Ktor extensions (` mcp ` for SSE + POST back-channel and
70+ ` mcpWebSocket ` for WebSocket) for HTTP hosting.
71+ - Built-in notifications for list changes and resource subscriptions when capabilities enable them.
7572
76- Register tools and run over stdio :
73+ Minimal Ktor SSE server :
7774
7875``` kotlin
79-
80- val server = Server (
81- serverInfo = Implementation (name = " sample-server" , version = " 1.0.0" ),
82- options = ServerOptions (ServerCapabilities ())
83- )
84-
85- server.addTool(
86- name = " echo" ,
87- description = " Echoes the provided text"
88- ) { request ->
89- // Build and return a CallToolResult from request.arguments
90- // (see CallToolResult and related types in kotlin-sdk-core)
91- /* ... */
76+ fun Application.module () {
77+ mcp {
78+ Server (
79+ serverInfo = Implementation (" sample-server" , " 1.0.0" ),
80+ options = ServerOptions (ServerCapabilities (
81+ tools = ServerCapabilities .Tools (listChanged = true ),
82+ resources = ServerCapabilities .Resources (listChanged = true , subscribe = true ),
83+ )),
84+ ) {
85+ addTool(name = " echo" , description = " Echo text back" ) { request ->
86+ CallToolResult (content = listOf (TextContent (" You said: ${request.params.arguments[" text" ]} " )))
87+ }
88+ }
89+ }
9290}
93-
94- // Bridge the protocol over stdio
95- val transport = StdioServerTransport (
96- inputStream = kotlinx.io.files.Path (" /dev/stdin" ).source(),
97- outputStream = kotlinx.io.files.Path (" /dev/stdout" ).sink()
98- )
99- // Start transport and wire it with the server using provided helpers in the SDK.
10091```
10192
102- For HTTP deployments, use the Ktor extensions included in the module to expose an MCP WebSocket or SSE endpoint with a
103- few lines of code.
93+ Pick the module that matches your role, or use the umbrella artifact to get both sides with the shared core.
0 commit comments