MCP ExplorerExplorer

Scala Mcp

@indoorvivantson 9 months ago
10 Apache-2.0
FreeCommunity
AI Systems
#jsonrpc#mcp#scala#scala-js#scala-native#scala3
Model Context Protocol for Scala 3

Overview

What is Scala Mcp

scala-mcp is a library implementing the Model Context Protocol for Scala 3, designed to facilitate communication between tools and applications using a structured context.

Use cases

Use cases for scala-mcp include building language servers, integrating development tools, and creating applications that require efficient data exchange in a structured format.

How to use

To use scala-mcp, you need to have Scala 3.7.0 or above. You can create a minimal MCP server by importing the library, defining request handlers, and running the server using the MCP Inspector.

Key features

Key features include a code generator that implements a subset of the Model Context Protocol, a minimal runtime for JSON serialization, and a basic JSON-RPC 2.0 implementation.

Where to use

scala-mcp can be used in software development environments where structured communication between tools is necessary, such as IDEs, code analysis tools, and other development utilities.

Content

Model Context Protocol for Scala 3

This is a WIP library implementing the Model Context Protocol.

At the moment the library consists of three parts:

  1. Code generator that uses JSON schema from MCP to implement a subset of the protocol
  2. Minimal runtime to help serialising structures in JSON
  3. Minimal jsonrpc runtime implementing JSONRPC 2.0 protocol

Note that the jsonrpc implementation does not support cancellation, and is generally not designed for serious usage – but it’s great to get things off the ground quickly! In the future, this library will provide an integration with jsonrpclib, once that library is published for Scala Native 0.5 (or may be earlier, if someone works on that integration).

Getting Started

Note that the library is using Scala 3.7 – which is generally fine for apps, as you can use any version of Scala above 3.7

//> using scala 3.7.0
//> using dep com.indoorvivants::mcp-quick::latest.release

import mcp.*

@main def hello =
  val mcp = MCPBuilder
    .create()
    .handle(initialize): req =>
      InitializeResult(
        capabilities =
          ServerCapabilities(tools = Some(ServerCapabilities.Tools())),
        protocolVersion = req.protocolVersion,
        serverInfo = Implementation("scala-mcp", "0.0.1")
      )
    .run(SyncTransport.default)
end hello

Save it in a mcp.scala file and run it with MCP Inspector:

npx @modelcontextprotocol/inspector scala-cli run mcp.scala

For a more involved example, containing tool usage and bi-directional communication, see the sample.

See it in action

You can run the provided sample using MCP inspector as long as you have Scala CLI and npx installed:

npx @modelcontextprotocol/inspector make run-sample

The sample loads and works in the MCP inspector:

CleanShot 2025-05-05 at 09 24 48

And can be configured in Claude desktop:

CleanShot 2025-05-05 at 09 20 02

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers