Back to MCP directory
publicPublicdnsLocal runtime

JSON-RPC 2.0

一个用Scala 3编写的MCP协议服务器,支持多种工具功能。

article

README

🚀 MCP-Scala

一个基于模型上下文协议(Model Context Protocol)的服务器,使用 Scala 3 编写,能够为相关应用提供强大的支持。

🚀 快速开始

构建服务器

首先,使用以下命令构建服务器:

sbt fastLinkJS

客户端配置

然后,在你的 MCP 客户端中使用该服务器:

// 示例配置
{
  "mcpServers": {
    "mcpscala": {
      "disabled": false,
      "timeout": 30,
      "command": "sh",
      "args": ["/path/to/run.sh"],
      "transportType": "stdio"
    }
  }
}

运行工具

你可以运行以下工具:

  • randomNumber
    • minmax 之间生成一个随机数。
  • iota
    • 生成从 minmax 的数字序列。
  • sum
    • 计算一组数字的和。

✨ 主要特性

开发阶段信息

此软件目前处于ALPHA版本,以下是开发进度:

  • [x] 自动推导 JSON 模式
  • [x] 定义你的工具
  • [x] 文本内容部分
  • [ ] 其他内容部分
  • [ ] 通知处理
  • [ ] 功能处理
  • [x] 标准输入输出(stdio)传输
  • [ ] HTTP 传输
  • [ ] 授权功能

此实现需要你的关注和贡献。欢迎通过打开问题或提交拉取请求来参与此项目。

💻 使用示例

基础用法

查看 StdioMain.scala 文件以获取详细信息。

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("最小值(包含在内)") min: Int,
    @description("最大值(不包含)") 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))
}

/** 标准输入输出服务器的主入口点。
  */
object StdioMain extends IOApp.Simple {
  val tools = Map(
    "randomNumber" -> server.Tool(randomNumber),
  )

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

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client