Scala MCP

Integrates with
Scala

MCP-scala

Model Context Protocol server written in Scala 3

Development stage

This software is currently ALPHA state.

  • Automatic derivation of JSON Schema
  • Define your tool
  • Text content part
  • Other content parts
  • Notification handling
  • Capability handling
  • stdio transport
  • HTTP transport
  • Authorization feature

This implementation needs your attention and contribution.

Feel free to open Issue / PR to contribute this project.

Demo

First, build server into JS:

sbt fastLinkJS

Then, utilize server in your MCP client:

// Example for Cline
{
  "mcpServers": {
    "mcpscala": {
      "disabled": false,
      "timeout": 30,
      "command": "sh",
      "args": ["/path/to/run.sh"],
      "transportType": "stdio"
    }
  }
}

You can run some tool:

  • randomNumber
    • generate random number between min to max.
  • iota
    • generate a sequence of numbers from min to max.
  • sum
    • calculate the sum of a sequence of numbers.

Implement your tool

See StdioMain.scala for details.

package dev.capslock.mcpscala

import cats.effect.IO
import cats.effect.IOApp
import dev.capslock.mcpscala.transport.StdioServer
import dev.capslock.mcpscala.mcp.ContentPart
import sttp.tapir.Schema.annotations.description

case class RandomNumberInput(
    @description("Minimum value (inclusive)") min: Int,
    @description("Maximum value (exclusive)") max: Int
) derives io.circe.Decoder,
      sttp.tapir.Schema

def randomNumber(input: RandomNumberInput): IO[Seq[ContentPart]] = IO {
  val random = scala.util.Random.between(input.min, input.max)
  Seq(ContentPart.TextContentPart(random.toString))
}

/** Entry point for the Stdio server.
  */
object StdioMain extends IOApp.Simple {
  val tools = Map(
    "randomNumber" -> server.Tool(randomNumber),
  )

  def run: IO[Unit] = {
    StdioServer.serve(Handler.methodHandlers(tools))
  }
}