-
Notifications
You must be signed in to change notification settings - Fork 209
sample: kotlinlang mcp server #713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devcrocod
wants to merge
1
commit into
main
Choose a base branch
from
devcrocod/sample-kotlinlang-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,149
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| FROM gradle:jdk21-alpine AS build | ||
| WORKDIR /project | ||
| COPY gradle/ gradle/ | ||
| COPY gradlew settings.gradle.kts build.gradle.kts ./ | ||
| COPY src/ src/ | ||
| RUN ./gradlew shadowJar --no-daemon | ||
|
|
||
| FROM eclipse-temurin:21-jre-alpine | ||
| RUN addgroup -S appgroup && adduser -S appuser -G appgroup | ||
| WORKDIR /app | ||
| COPY --from=build /project/build/libs/*-all.jar app.jar | ||
| USER appuser | ||
| EXPOSE 8080 | ||
| ENTRYPOINT ["java", "-jar", "app.jar"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # Kotlinlang MCP Server | ||
|
|
||
| A Streamable HTTP MCP server that exposes the official | ||
| [Kotlin documentation](https://kotlinlang.org/docs/) to LLM clients — full-text search via | ||
| [Algolia](https://www.algolia.com/) plus page retrieval in markdown through kotlinlang.org's | ||
| `_llms` endpoints. | ||
|
|
||
| ## Overview | ||
|
|
||
| This sample demonstrates an MCP server that wraps a real external documentation source. It uses the | ||
| recommended Streamable HTTP transport, supports multiple concurrent client sessions, and caches | ||
| responses in memory to reduce upstream load. The server is intentionally read-only — it exposes two | ||
| tools, no prompts, no resources. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - JDK 21+ | ||
| - Algolia credentials for `kotlinlang.org` (exported as environment variables, see | ||
| [Configuration](#configuration)) | ||
|
|
||
| ## Build & Run | ||
|
|
||
| ```shell | ||
| export ALGOLIA_APP_ID=... | ||
| export ALGOLIA_API_KEY=... | ||
| export ALGOLIA_INDEX_NAME=... | ||
|
|
||
| ./gradlew run | ||
| ``` | ||
|
|
||
| The server starts on `http://localhost:8080/mcp` by default. | ||
|
|
||
| Connect with the [MCP Inspector](https://modelcontextprotocol.io/docs/tools/inspector): | ||
|
|
||
| ```shell | ||
| npx @modelcontextprotocol/inspector | ||
| ``` | ||
|
|
||
| In the Inspector UI, select **Streamable HTTP** transport and enter `http://localhost:8080/mcp`. | ||
|
|
||
| ## MCP Capabilities | ||
|
|
||
| ### Tools | ||
|
|
||
| | Name | Description | | ||
| |-----------------------|----------------------------------------------------------------------------------------------------------------------------------------| | ||
| | `search_kotlinlang` | Full-text search across Kotlin documentation. Returns up to 5 results filtered to `/docs/` pages. Responses cached for 10 minutes. | | ||
| | `get_kotlinlang_page` | Fetches a documentation page as markdown via kotlinlang.org's `_llms` endpoint. Accepts a path relative to `/docs/`. Cached for 1 hour. | | ||
|
|
||
| Example paths accepted by `get_kotlinlang_page`: `coroutines-overview`, | ||
| `multiplatform/compose-multiplatform-and-jetpack-compose`. Leading/trailing slashes and an optional | ||
| `.html` suffix are normalized before resolution. | ||
|
|
||
| ## Configuration | ||
|
|
||
| | Variable | Required | Default | Description | | ||
| |----------------------|----------|-----------|--------------------------| | ||
| | `ALGOLIA_APP_ID` | yes | — | Algolia application ID | | ||
| | `ALGOLIA_API_KEY` | yes | — | Algolia search API key | | ||
| | `ALGOLIA_INDEX_NAME` | yes | — | Algolia index name | | ||
| | `SERVER_PORT` | no | `8080` | HTTP server port | | ||
| | `SERVER_HOST` | no | `0.0.0.0` | HTTP server bind address | | ||
|
|
||
| The server does not provide fallback values for `ALGOLIA_*`. Startup fails if any of those | ||
| variables are missing. | ||
|
|
||
| ## Docker | ||
|
|
||
| Multi-stage build with Alpine-based images. Runs as a non-root user. | ||
|
|
||
| ```shell | ||
| docker build -t kotlinlang-mcp-server . | ||
|
|
||
| docker run \ | ||
| -p 8080:8080 \ | ||
| -e ALGOLIA_APP_ID=... \ | ||
| -e ALGOLIA_API_KEY=... \ | ||
| -e ALGOLIA_INDEX_NAME=... \ | ||
| kotlinlang-mcp-server | ||
| ``` | ||
|
|
||
| ## Connecting an MCP client | ||
|
|
||
| Any MCP client that supports Streamable HTTP transport can connect: | ||
|
|
||
| ```json | ||
| { | ||
| "mcpServers": { | ||
| "kotlinlang": { | ||
| "url": "http://localhost:8080/mcp" | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Limitations | ||
|
|
||
| This sample is intended **for demonstration only**. Before production use, consider: | ||
|
|
||
| - **In-memory cache only** — cached data is lost on server restart. | ||
| - **No authentication or authorization** — run behind a trusted proxy or restrict network access. | ||
| - **No rate limiting** on outgoing requests to Algolia and kotlinlang.org. | ||
| - **Permissive CORS** (`anyHost()`) — restrict to specific origins for any non-local deployment. | ||
| - **Streamable HTTP only** — no STDIO transport for local-only usage. | ||
| - **Tools only** — no MCP resources or prompts are exposed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| plugins { | ||
| alias(libs.plugins.kotlin.jvm) | ||
| alias(libs.plugins.kotlin.serialization) | ||
| alias(libs.plugins.shadow) | ||
| application | ||
| } | ||
|
|
||
| group = "org.example" | ||
| version = "0.1.0" | ||
|
|
||
| application { | ||
| mainClass.set("org.kotlinlang.mcp.ApplicationKt") | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation(platform(libs.ktor.bom)) | ||
| implementation(libs.mcp.kotlin.server) | ||
| implementation(libs.ktor.server.netty) | ||
| implementation(libs.ktor.server.sse) | ||
| implementation(libs.ktor.server.content.negotiation) | ||
| implementation(libs.ktor.server.cors) | ||
| implementation(libs.ktor.client.cio) | ||
| implementation(libs.ktor.client.content.negotiation) | ||
| implementation(libs.ktor.serialization.kotlinx.json) | ||
| implementation(libs.kotlinx.serialization.json) | ||
| implementation(libs.kotlinx.coroutines.core) | ||
| implementation(libs.logback.classic) | ||
|
|
||
| testImplementation(kotlin("test")) | ||
| testImplementation(libs.kotlinx.coroutines.test) | ||
| testImplementation(libs.ktor.client.mock) | ||
| testImplementation(libs.mcp.kotlin.client) | ||
| testImplementation(libs.mcp.kotlin.testing) | ||
| } | ||
|
|
||
| tasks.test { | ||
| useJUnitPlatform() | ||
| } | ||
|
|
||
| kotlin { | ||
| jvmToolchain(21) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| org.gradle.configuration-cache=true | ||
| org.gradle.parallel=true | ||
| org.gradle.caching=true | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| [versions] | ||
| kotlin = "2.3.20" | ||
| ktor = "3.3.3" | ||
| mcp-kotlin = "0.11.1" | ||
| serialization = "1.10.0" | ||
| coroutines = "1.10.2" | ||
| logback = "1.5.32" | ||
| shadow = "9.4.1" | ||
|
|
||
| [libraries] | ||
| ktor-bom = { group = "io.ktor", name = "ktor-bom", version.ref = "ktor" } | ||
| ktor-server-netty = { group = "io.ktor", name = "ktor-server-netty" } | ||
| ktor-server-sse = { group = "io.ktor", name = "ktor-server-sse" } | ||
| ktor-server-content-negotiation = { group = "io.ktor", name = "ktor-server-content-negotiation" } | ||
| ktor-server-cors = { group = "io.ktor", name = "ktor-server-cors" } | ||
| ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio" } | ||
| ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation" } | ||
| ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json" } | ||
| mcp-kotlin-server = { group = "io.modelcontextprotocol", name = "kotlin-sdk-server", version.ref = "mcp-kotlin" } | ||
| mcp-kotlin-client = { group = "io.modelcontextprotocol", name = "kotlin-sdk-client", version.ref = "mcp-kotlin" } | ||
| mcp-kotlin-testing = { group = "io.modelcontextprotocol", name = "kotlin-sdk-testing", version.ref = "mcp-kotlin" } | ||
| kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-json", version.ref = "serialization" } | ||
| kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" } | ||
| logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback" } | ||
| kotlinx-coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" } | ||
| ktor-client-mock = { group = "io.ktor", name = "ktor-client-mock" } | ||
|
|
||
| [plugins] | ||
| kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } | ||
| kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } | ||
| shadow = { id = "com.gradleup.shadow", version.ref = "shadow" } | ||
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
samples/kotlinlang-mcp-server/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip | ||
| networkTimeout=10000 | ||
| validateDistributionUrl=true | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sample pins Kotlin to
2.3.20, while the other samples in this repo currently standardize on2.2.21(e.g.,samples/kotlin-mcp-server/gradle/libs.versions.toml:2,samples/simple-streamable-server/gradle/libs.versions.toml:2). Using a different Kotlin version for a single sample increases maintenance overhead and can introduce inconsistent behavior across samples; consider aligning this version unless there’s a specific need to demonstrate a newer Kotlin release.