mcp4k

mcp4k is a compiler-driven framework for building both clients and servers using the Model Context Protocol (MCP) in Kotlin. It implements a vast majority of the MCP specification, including resources, prompts, tools, sampling, and more. mcp4k automatically generates JSON-RPC handlers, schema metadata, and manages the complete lifecycle for you.

GitHub Stars

62

User Rating

Not Rated

Favorites

0

Views

33

Forks

3

Issues

0

Installation
Difficulty
Intermediate
Estimated Time
10-20 minutes
Requirements
Kotlin 2.2.0

Installation

Installation

Prerequisites

Please specify required software and versions:
Kotlin: 2.2.0

Installation Steps

1. Add mcp4k to your project

kotlin
plugins {
  kotlin("multiplatform") version "2.2.0"
  kotlin("plugin.serialization") version "2.2.0"

  id("sh.ondr.mcp4k") version "0.4.2" // <-- Add this
}

Troubleshooting

Common Issues

Issue: Compiler errors occur Solution: Ensure that the Kotlin version matches the requirements of mcp4k.

Configuration

Configuration

Basic Configuration

Example Server Configuration

kotlin
@McpTool
fun reverseString(input: String): ToolContent {
  return "Reversed: ${input.reversed()}".toTextContent()
}

fun main() = runBlocking {
  val server = Server.Builder()
    .withTool(::reverseString)
    .withTransport(StdioTransport())
    .build()
  
  server.start()
  
  // Keep server running
  while (true) { 
    delay(1000)
  }
}

Examples

Examples

Creating a Server

kotlin
@McpTool
fun reverseString(input: String): ToolContent {
  return "Reversed: ${input.reversed()}".toTextContent()
}

fun main() = runBlocking {
  val server = Server.Builder()
    .withTool(::reverseString)
    .withTransport(StdioTransport())
    .build()
  
  server.start()
  
  // Keep server running
  while (true) { 
    delay(1000)
  }
}

Creating a Client

kotlin
fun main() = runBlocking {
  val client = Client.Builder()
    .withClientInfo("MyClient", "1.0.0")
    .withTransport(StdioTransport())
    .build()
  
  client.start()
  client.initialize()
}

Use Cases

Developing a client application that calls specific tools using an MCP server
Implementing custom tools on an MCP server for data processing and analysis
Building applications that operate across multiple platforms
Creating agents for real-time data processing

Additional Resources