Kotlin implementation of the Model Context Protocol (MCP), providing both client and server capabilities for integrating with LLM surfaces.
The Model Context Protocol allows applications to provide context for LLMs in a standardized way, separating the concerns of providing context from the actual LLM interaction. This Kotlin SDK implements the full MCP specification, making it easy to:
Add the new repository to your build file:
repositories {
mavenCentral()
}
Add the dependency:
dependencies {
implementation("io.modelcontextprotocol:kotlin-sdk:0.1.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)
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)
)
)
)
}
}
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型