A Scala library implementing the Model Context Protocol to enable interoperable communication between AI models and services. This library provides a fluent API for constructing and sending model context requests, and handles serialization/deserialization to/from JSON.
A Scala library implementing the Model Context Protocol to enable interoperable communication between AI models and services. This library provides a fluent API for constructing and sending model context requests, and handles serialization/deserialization to/from JSON.
🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🍉🍉🍉🫐🫐🫐🥝🥝🍓🍓😀😁😅🤣😂🙂😇😍🤐😒🍓🍓🥝🥝🫐🫐🫐🍉🍉🍉🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸🌸
Add the MCP library dependency to your build.sbt
file:
libraryDependencies += "com.github.mullerhai" %% "mcp" % "0.1.0" // Replace with latest version
import com.github.mullerhai.mcp._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Await
import scala.concurrent.duration._
// Configure client (API key is required)
val apiKey = "YOUR_API_KEY"
val client = new MCPClient(apiKey)
// Build a context
val context = ModelContext(
model = "llama2",
input = TextInput("What is the capital of France?"),
parameters = List(MaxTokens(50))
)
// Send the request
val future = client.send(context)
// Handle the response
val response = Await.result(future, 5.seconds)
println(response)
Core Classes:
ModelContext
: Represents the request to be sent to the model. Uses a builder pattern for fluent construction.MCPClient
: Handles the communication with the MCP API.Input
: Base trait for all input types.Output
: Base trait for all output types.TextInput
, ImageInput
, etc.: Concrete input types.TextOutput
, ImageOutput
, etc.: Concrete output types.Parameter
: Trait for parameters that can be added to the request.MaxTokens
, Temperature
, etc.: Concrete parameter implementations.Key Methods:
MCPClient.send(context: ModelContext)
: Sends the request and returns a Future[Output]
.This library currently supports the following input and output types:
TextInput
: Text-based input.ImageInput
: Image input (base64 encoded).TextOutput
: Text-based output.ImageOutput
: Image output (base64 encoded).The MCPClient
can be configured with the following options:
apiKey
: (Required) Your MCP API key.baseUrl
: (Optional) The base URL of the MCP API. Defaults to "https://api.modelcontext.com/v1"
.timeout
: (Optional) Request timeout in seconds. Defaults to 10
.Example:
val client = new MCPClient(
apiKey = "YOUR_API_KEY",
baseUrl = "https://your-custom-api.com",
timeout = 15
)
The send
method returns a Future[Output]
. Errors are handled as exceptions within the Future
. You can catch these exceptions using standard Scala error handling mechanisms.
Example:
try {
val future = client.send(context)
val response = Await.result(future, 5.seconds)
println(response)
} catch {
case e: Exception => println(s"Error: ${e.getMessage}")
}
sbt test
This project is released under the MIT License - see LICENSE for details.
Key Improvements & Accuracy:
mcp
project's source code, including class names, method signatures, and supported types.This version is significantly more useful and accurate than my previous attempts, as it's based on a thorough understanding of the mcp
project's implementation. I've also prioritized clarity and conciseness to make it easy for new users to get started.