|
| 1 | +# kotlin-mcp-sdk |
| 2 | + |
| 3 | +MCP Kotlin SDK — Kotlin Multiplatform implementation of the Model Context Protocol. |
| 4 | + |
| 5 | +## Repository Layout |
| 6 | +- `kotlin-sdk-core`: Core protocol types, transport abstractions, WebSocket implementation |
| 7 | +- `kotlin-sdk-client`: Client transports (STDIO, SSE, StreamableHttp, WebSocket) |
| 8 | +- `kotlin-sdk-server`: Server transports + Ktor integration (STDIO, SSE, WebSocket) |
| 9 | +- `kotlin-sdk`: Umbrella module that aggregates all three above |
| 10 | +- `kotlin-sdk-test`: Shared test utilities |
| 11 | +- `samples/`: Three sample projects (composite builds) |
| 12 | + |
| 13 | +## General Guidance |
| 14 | +- Avoid changing public API signatures. Run `./gradlew apiCheck` before every commit. |
| 15 | +- **Explicit API mode** is strict: all public APIs must have explicit visibility modifiers and return types. |
| 16 | +- Anything under an `internal` directory is not part of the public API and may change freely. |
| 17 | +- Package structure follows: `io.modelcontextprotocol.kotlin.sdk.*` |
| 18 | +- The SDK targets Kotlin 2.2+ and JVM 1.8+ as minimum. |
| 19 | + |
| 20 | +## Building and Testing |
| 21 | +1. Build the project: |
| 22 | + ```bash |
| 23 | + ./gradlew build |
| 24 | + ``` |
| 25 | +2. Run tests. To save time, run only the module you changed: |
| 26 | + ```bash |
| 27 | + ./gradlew :kotlin-sdk-core:test |
| 28 | + ./gradlew :kotlin-sdk-client:test |
| 29 | + ./gradlew :kotlin-sdk-server:test |
| 30 | + ``` |
| 31 | +3. For platform-specific tests: |
| 32 | + ```bash |
| 33 | + ./gradlew jsTest |
| 34 | + ./gradlew wasmJsTest |
| 35 | + ``` |
| 36 | +4. Check binary compatibility (required before commit): |
| 37 | + ```bash |
| 38 | + ./gradlew apiCheck |
| 39 | + ``` |
| 40 | +5. If you intentionally changed public APIs, update the API dump: |
| 41 | + ```bash |
| 42 | + ./gradlew apiDump |
| 43 | + ``` |
| 44 | + |
| 45 | +## Tests |
| 46 | +- All tests for each module are located in `src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/` |
| 47 | +- Platform-specific tests go in `src/jvmTest/`, `src/jsTest/`, etc. |
| 48 | +- Use Kotest assertions (`shouldBe`, `shouldContain`, etc.) for readable test failures. |
| 49 | +- Use `shouldMatchJson` from Kotest for JSON validation. |
| 50 | +- Mock Ktor HTTP clients using `MockEngine` from `io.ktor:ktor-client-mock`. |
| 51 | +- Always add tests for new features or bug fixes, even if not explicitly requested. |
| 52 | + |
| 53 | +## Code Conventions |
| 54 | + |
| 55 | +### Multiplatform Patterns |
| 56 | +- Use `expect`/`actual` pattern for platform-specific implementations in `utils.*` files. |
| 57 | +- Test changes on JVM first, then verify platform-specific behavior if needed. |
| 58 | +- Supported targets: JVM (1.8+), JS/Wasm, iOS, watchOS, tvOS. |
| 59 | + |
| 60 | +### Serialization |
| 61 | +- Use Kotlinx Serialization with explicit `@Serializable` annotations. |
| 62 | +- Custom serializers should be registered in the companion object. |
| 63 | +- JSON config is defined in `McpJson.kt` — use it consistently. |
| 64 | + |
| 65 | +### Concurrency and State |
| 66 | +- Use `kotlinx.atomicfu` for thread-safe operations across platforms. |
| 67 | +- Prefer coroutines over callbacks where possible. |
| 68 | +- Transport implementations extend `AbstractTransport` for consistent callback management. |
| 69 | + |
| 70 | +### Error Handling |
| 71 | +- Use sealed classes for representing different result states. |
| 72 | +- Map errors to JSON-RPC error codes in protocol handlers. |
| 73 | +- Log errors using `io.github.oshai.kotlinlogging.KotlinLogging`. |
| 74 | + |
| 75 | +### Logging |
| 76 | +- Use `KotlinLogging.logger {}` for structured logging. |
| 77 | +- Never log sensitive data (credentials, tokens). |
| 78 | + |
| 79 | +## Pull Requests |
| 80 | +- Base all PRs on the `main` branch. |
| 81 | +- PR title format: Brief description of the change |
| 82 | +- Include in PR description: |
| 83 | + - **What changed?** |
| 84 | + - **Why? Motivation and context** |
| 85 | + - **Breaking changes?** (if any) |
| 86 | +- Run `./gradlew apiCheck` before submitting. |
| 87 | +- Link PR to related issue (except for minor docs/typo fixes). |
| 88 | +- Follow [Kotlin Coding Conventions](https://kotlinlang.org/docs/reference/coding-conventions.html). |
| 89 | + |
| 90 | +## Review Checklist |
| 91 | +- `./gradlew apiCheck` must pass. |
| 92 | +- `./gradlew test` must succeed for affected modules. |
| 93 | +- New tests added for any new feature or bug fix. |
| 94 | +- Documentation updated for user-facing changes. |
| 95 | +- No breaking changes to public APIs without discussion. |
0 commit comments