Generate a simple MCP (Model Context Protocol) server for receiving command strings and…
- User
Generate a simple MCP (Model Context Protocol) server for receiving command strings and responds with a different string. Implement it in Kotlin/KTOR and packaged with a bash shell script.
- Assistant
To implement a simple MCP (Model Context Protocol) server in Kotlin using the Ktor framework, we will create a server that listens for incoming command strings and responds with a predefined string. Additionally, we will package the application with a bash shell script to run the server.
Step 1: Set Up the Ktor Server
We will use the Ktor framework to create a lightweight HTTP server. The server will accept POST requests containing a command string and respond with a different string.
Directory Structure:
mcp-server/ ├── build.gradle.kts ├── src/ │ └── main/ │ └── kotlin/ │ └── com/example/mcpservice/ │ └── Application.kt └── run_server.sh
Step 2:
build.gradle.ktsThis file defines the project dependencies and configurations.
plugins { kotlin("jvm") version "1.9.0" application } repositories { mavenCentral() } dependencies { implementation("io.ktor:ktor-server-core:2.3.4") implementation("io.ktor:ktor-server-netty:2.3.4") implementation("ch.qos.logback:logback-classic:1.4.11") implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.4") implementation("io.ktor:ktor-server-content-negotiation:2.3.4") } application { mainClass.set("com.example.mcpservice.ApplicationKt") }
Step 3:
Application.ktThis is the main server implementation.
package com.example.mcpservice import io.ktor.application.* import io.ktor.features.ContentNegotiation import io.ktor.http.* import io.ktor.request.* import io.ktor.response.* import io.ktor.routing.* import io.ktor.serialization.kotlinx.json.* import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty data class CommandRequest(val command: String) fun main() { embeddedServer(Netty, port = 8080) { install(ContentNegotiation) { json() } routing { post("/mcp/command") { val request = call.receive<CommandRequest>() val responseMessage = when (request.command.lowercase()) { "start" -> "Starting process..." "stop" -> "Stopping process..." else -> "Unknown command received." } call.respond(HttpStatusCode.OK, mapOf("response" to responseMessage)) } } }.start(wait = true) }
Step 4:
run_server.shThis bash script compiles and runs the server.
#!/bin/bash # Navigate to the project directory cd "$(dirname "$0")" # Build the project using Gradle echo "Building the MCP server..." ./gradlew clean build # Check if the build was successful if [ $? -ne 0 ]; then echo "Build failed. Exiting..." exit 1 fi # Run the server echo "Starting the MCP server on port 8080..." java -jar build/libs/mcp-server.jarMake sure to give execute permissions to the script:
chmod +x run_server.sh
Step 5: Running the Server
- Clone or create the project directory with the above files.
- Run the server using the bash script:
./run_server.sh
The server will start listening on
http://localhost:8080.
Step 6: Testing the Server
You can test the server using
curl:Example 1: Send "start" command
curl -X POST http://localhost:8080/mcp/command \ -H "Content-Type: application/json" \ -d '{"command": "start"}'Response:
{ "response": "Starting process..." }Example 2: Send "stop" command
curl -X POST http://localhost:8080/mcp/command \ -H "Content-Type: application/json" \ -d '{"command": "stop"}'Response:
{ "response": "Stopping process..." }Example 3: Send an unknown command
curl -X POST http://localhost:8080/mcp/command \ -H "Content-Type: application/json" \ -d '{"command": "pause"}'Response:
{ "response": "Unknown command received." }
Summary
This implementation provides a simple MCP server using Kotlin and Ktor. The server listens for commands via HTTP POST requests and responds with predefined messages. The bash script simplifies the build and execution process. You can extend this implementation to handle more complex commands or integrate it with other systems as needed.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.