用Scala 3编写的Model Context Protocol服务器
此软件目前处于ALPHA状态。
此实现需要您的关注和贡献。
请随意打开Issue/PR来为此项目做出贡献。
首先,构建服务器到JS:
sbt example/fastLinkJS
然后,在您的MCP客户端中使用服务器:
// Cline示例
{
"mcpServers": {
"mcpscala": {
"disabled": false,
"timeout": 30,
"command": "sh",
"args": ["/path/to/run.sh"],
"transportType": "stdio"
}
}
}
您可以运行一些工具:
randomNumber
min到max之间生成随机数。iota
min到max生成一系列数字。sum
详情见Main.scala。
//> 使用scala 3
//> 使用toolkit typelevel:default
//> 使用依赖dev.capslock::mcpscala:0.1.0
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))
}
/** 标准I/O服务器的入口点。
*/
object StdioMain extends McpIOApp(
name = "random-number",
header = "生成一个随机数",
):
val handlers = Handler.methodHandlers(
Map(
"randomNumber" -> server.Tool(randomNumber),
)
)