|
| 1 | +# MCP Kotlin SDK |
| 2 | + |
| 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. |
| 7 | + |
| 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): |
| 12 | + |
| 13 | +```kotlin |
| 14 | +dependencies { |
| 15 | + // Convenience bundle with everything you need to start |
| 16 | + implementation("io.modelcontextprotocol:kotlin-sdk:<version>") |
| 17 | + |
| 18 | + // Or pick modules explicitly |
| 19 | + implementation("io.modelcontextprotocol:kotlin-sdk-client:<version>") |
| 20 | + implementation("io.modelcontextprotocol:kotlin-sdk-server:<version>") |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Module kotlin-sdk-core |
| 27 | + |
| 28 | +Foundational, platform‑agnostic pieces: |
| 29 | + |
| 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 |
| 34 | + |
| 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. |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Module kotlin-sdk-client |
| 41 | + |
| 42 | +High‑level client API for connecting to an MCP server and invoking its tools, prompts, and resources. Ships with several |
| 43 | +transports: |
| 44 | + |
| 45 | +- WebSocketClientTransport – low latency, full‑duplex |
| 46 | +- SSEClientTransport – Server‑Sent Events over HTTP |
| 47 | +- StdioClientTransport – CLI‑friendly stdio bridge |
| 48 | +- StreamableHttpClientTransport – simple HTTP streaming |
| 49 | + |
| 50 | +A minimal client: |
| 51 | + |
| 52 | +```kotlin |
| 53 | +val client = Client( |
| 54 | + clientInfo = Implementation(name = "sample-client", version = "1.0.0") |
| 55 | +) |
| 56 | + |
| 57 | +client.connect(WebSocketClientTransport("ws://localhost:8080/mcp")) |
| 58 | + |
| 59 | +val tools = client.listTools() |
| 60 | +val result = client.callTool( |
| 61 | + name = "echo", |
| 62 | + arguments = mapOf("text" to "Hello, MCP!") |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Module kotlin-sdk-server |
| 69 | + |
| 70 | +Lightweight server toolkit for hosting MCP tools, prompts, and resources. It provides a small, composable API and |
| 71 | +ready‑to‑use transports: |
| 72 | + |
| 73 | +- StdioServerTransport – integrates well with CLIs and editors |
| 74 | +- SSE/WebSocket helpers for Ktor – easy HTTP deployment |
| 75 | + |
| 76 | +Register tools and run over stdio: |
| 77 | + |
| 78 | +```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 | + /* ... */ |
| 92 | +} |
| 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. |
| 100 | +``` |
| 101 | + |
| 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. |
0 commit comments