|
| 1 | +import com.surrus.galwaybus.common.GalwayBusRepository |
| 2 | +import com.surrus.galwaybus.common.di.initKoin |
| 3 | +import com.surrus.galwaybus.common.model.Result |
| 4 | +import io.ktor.server.cio.* |
| 5 | +import io.ktor.server.engine.* |
| 6 | +import io.ktor.utils.io.streams.* |
| 7 | +import io.modelcontextprotocol.kotlin.sdk.* |
| 8 | +import io.modelcontextprotocol.kotlin.sdk.server.* |
| 9 | +import kotlinx.coroutines.Job |
| 10 | +import kotlinx.coroutines.runBlocking |
| 11 | +import kotlinx.io.asSink |
| 12 | +import kotlinx.io.buffered |
| 13 | +import kotlinx.serialization.json.JsonObject |
| 14 | +import kotlinx.serialization.json.JsonPrimitive |
| 15 | +import kotlinx.serialization.json.jsonPrimitive |
| 16 | + |
| 17 | + |
| 18 | +private val koin = initKoin(enableNetworkLogs = true).koin |
| 19 | + |
| 20 | +fun configureServer(): Server { |
| 21 | + val galwayBusRepository = koin.get<GalwayBusRepository>() |
| 22 | + |
| 23 | + val server = Server( |
| 24 | + Implementation( |
| 25 | + name = "GalwayBus MCP Server", |
| 26 | + version = "1.0.0" |
| 27 | + ), |
| 28 | + ServerOptions( |
| 29 | + capabilities = ServerCapabilities( |
| 30 | + tools = ServerCapabilities.Tools(listChanged = true) |
| 31 | + ) |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | + server.addTool( |
| 37 | + name = "get-bus-routes", |
| 38 | + description = "List of bus routes" |
| 39 | + ) { |
| 40 | + val busRoutes = galwayBusRepository.fetchBusRoutes() |
| 41 | + CallToolResult( |
| 42 | + content = |
| 43 | + busRoutes.map { TextContent("${it.timetableId}, ${it.longName}") } |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + server.addTool( |
| 50 | + name = "get-nearest-stops", |
| 51 | + description = "List nearest bus stops", |
| 52 | + ) { request -> |
| 53 | + val latitude = 53.2743394 |
| 54 | + val longitude = -9.0514163 |
| 55 | + val routeStopsResult = galwayBusRepository.fetchNearestStops(latitude, longitude) |
| 56 | + if (routeStopsResult is Result.Success) { |
| 57 | + val routeStops = routeStopsResult.data |
| 58 | + CallToolResult( |
| 59 | + content = routeStops.map { TextContent(it.toString()) } |
| 60 | + ) |
| 61 | + } else { |
| 62 | + CallToolResult( |
| 63 | + content = listOf(TextContent("Error getting route stops.")) |
| 64 | + ) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + server.addTool( |
| 70 | + name = "get-bus-departures", |
| 71 | + description = "List", |
| 72 | + inputSchema = Tool.Input( |
| 73 | + properties = JsonObject( |
| 74 | + mapOf("stopRef" to JsonPrimitive("string")) |
| 75 | + ), |
| 76 | + required = listOf("stopRef") |
| 77 | + ) |
| 78 | + |
| 79 | + ) { request -> |
| 80 | + val stopRef = request.arguments["stopRef"] |
| 81 | + if (stopRef == null) { |
| 82 | + return@addTool CallToolResult( |
| 83 | + content = listOf(TextContent("The 'stopRef' parameter is required.")) |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + val busDeparturesResult = galwayBusRepository.fetchBusStopDepartures(stopRef.jsonPrimitive.content) |
| 88 | + if (busDeparturesResult is Result.Success) { |
| 89 | + val routeStops = busDeparturesResult.data |
| 90 | + CallToolResult( |
| 91 | + content = routeStops.map { TextContent(it.toString()) } |
| 92 | + ) |
| 93 | + } else { |
| 94 | + CallToolResult( |
| 95 | + content = listOf(TextContent("Error getting bus departures.")) |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + server.addTool( |
| 102 | + name = "get-route-stops", |
| 103 | + description = "List or stops for particular bus route", |
| 104 | + inputSchema = Tool.Input( |
| 105 | + properties = JsonObject( |
| 106 | + mapOf("routeId" to JsonPrimitive("string")) |
| 107 | + ), |
| 108 | + required = listOf("routeId") |
| 109 | + ) |
| 110 | + |
| 111 | + ) { request -> |
| 112 | + val routeId = request.arguments["routeId"] |
| 113 | + if (routeId == null) { |
| 114 | + return@addTool CallToolResult( |
| 115 | + content = listOf(TextContent("The 'routeId' parameter is required.")) |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + val routeStopsResult = galwayBusRepository.fetchRouteStops(routeId.jsonPrimitive.content) |
| 120 | + if (routeStopsResult is Result.Success) { |
| 121 | + val routeStops = routeStopsResult.data |
| 122 | + CallToolResult( |
| 123 | + content = routeStops.map { TextContent(it.toString()) } |
| 124 | + ) |
| 125 | + } else { |
| 126 | + CallToolResult( |
| 127 | + content = listOf(TextContent("Error getting route stops.")) |
| 128 | + ) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return server |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Runs an MCP (Model Context Protocol) server using standard I/O for communication. |
| 137 | + * |
| 138 | + * This function initializes a server instance configured with predefined tools and capabilities. |
| 139 | + * It sets up a transport mechanism using standard input and output for communication. |
| 140 | + * Once the server starts, it listens for incoming connections, processes requests, |
| 141 | + * and executes the appropriate tools. The server shuts down gracefully upon receiving |
| 142 | + * a close event. |
| 143 | + */ |
| 144 | +fun `run mcp server using stdio`() { |
| 145 | + val server = configureServer() |
| 146 | + val transport = StdioServerTransport( |
| 147 | + System.`in`.asInput(), |
| 148 | + System.out.asSink().buffered() |
| 149 | + ) |
| 150 | + |
| 151 | + runBlocking { |
| 152 | + server.connect(transport) |
| 153 | + val done = Job() |
| 154 | + server.onClose { |
| 155 | + done.complete() |
| 156 | + } |
| 157 | + done.join() |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Launches an SSE (Server-Sent Events) MCP (Model Context Protocol) server on the specified port. |
| 163 | + * This server enables clients to connect via SSE for real-time communication and provides endpoints |
| 164 | + * for handling specific messages. |
| 165 | + * |
| 166 | + * @param port The port number on which the SSE server should be started. |
| 167 | + */ |
| 168 | +fun `run sse mcp server`(port: Int): Unit = runBlocking { |
| 169 | + val server = configureServer() |
| 170 | + embeddedServer(CIO, host = "0.0.0.0", port = port) { |
| 171 | + mcp { |
| 172 | + server |
| 173 | + } |
| 174 | + }.start(wait = true) |
| 175 | +} |
0 commit comments