- Notifications
You must be signed in to change notification settings - Fork175
The official Kotlin SDK for Model Context Protocol servers and clients. Maintained in collaboration with JetBrains
License
modelcontextprotocol/kotlin-sdk
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Kotlin Multiplatform implementation of theModel Context Protocol (MCP),providing both client and server capabilities for integrating with LLM surfaces across various platforms.
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 SDK implements the MCP specification for Kotlin,enabling you to build applications that can communicate using MCP on the JVM, WebAssembly and iOS.
- Build MCP clients that can connect to any MCP server
- Create MCP servers that expose resources, prompts and tools
- Use standard transports like stdio, SSE, and WebSocket
- Handle all MCP protocol messages and lifecycle events
- kotlin-mcp-server: demonstrates a multiplatform (JVM, Wasm) MCP server setup with various features and transports.
- weather-stdio-server: shows how to build a Kotlin MCP server providing weather forecast and alerts using STDIO transport.
- kotlin-mcp-client: demonstrates building an interactive Kotlin MCP client that connects to an MCP server via STDIO and integrates with Anthropic’s API.
Add the new repository to your build file:
repositories { mavenCentral()}Add the dependency:
dependencies {// See the badge above for the latest version implementation("io.modelcontextprotocol:kotlin-sdk:$mcpVersion")}MCP SDK usesKtor, but does not come with a specific engine dependency.You should addKtor clientand/orKtor server dependencyto your project yourself, e.g.:
dependencies {// for client: implementation("io.ktor:ktor-client-cio:$ktorVersion")// for server: implementation("io.ktor:ktor-server-netty:$ktorVersion") }importio.modelcontextprotocol.kotlin.sdk.client.Clientimportio.modelcontextprotocol.kotlin.sdk.client.StdioClientTransportimportio.modelcontextprotocol.kotlin.sdk.Implementationval client=Client( clientInfo=Implementation( name="example-client", version="1.0.0" ))val transport=StdioClientTransport( inputStream= processInputStream, outputStream= processOutputStream)// Connect to serverclient.connect(transport)// List available resourcesval resources= client.listResources()// Read a specific resourceval resourceContent= client.readResource(ReadResourceRequest(uri="file:///example.txt"))
importio.modelcontextprotocol.kotlin.sdk.server.Serverimportio.modelcontextprotocol.kotlin.sdk.server.ServerOptionsimportio.modelcontextprotocol.kotlin.sdk.server.StdioServerTransportimportio.modelcontextprotocol.kotlin.sdk.ServerCapabilitiesval server=Server( serverInfo=Implementation( name="example-server", version="1.0.0" ), options=ServerOptions( capabilities=ServerCapabilities( resources=ServerCapabilities.Resources( subscribe=true, listChanged=true ) ) )){"This server provides example resources and demonstrates MCP capabilities." }// Add a resourceserver.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 transportval transport=StdioServerTransport()server.connect(transport)
Directly in Ktor'sApplication:
importio.ktor.server.application.*importio.modelcontextprotocol.kotlin.sdk.server.mcpfun 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) ) ), ) {"This SSE server provides prompts and resources via Server-Sent Events." } }}
Inside a custom Ktor'sRoute:
importio.ktor.server.application.*importio.ktor.server.sse.SSEimportio.modelcontextprotocol.kotlin.sdk.server.mcpfun Application.module() { install(SSE) routing { route("myRoute") { 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) ) ), ) {"Connect via SSE to interact with this MCP server." } } } }}
Please see thecontribution guide and theCode of conduct before contributing.
This project is licensed under the MIT License—see theLICENSE file for details.
About
The official Kotlin SDK for Model Context Protocol servers and clients. Maintained in collaboration with JetBrains
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.