|
| 1 | +import com.surrus.common.di.initKoin |
| 2 | +import com.surrus.common.repository.PeopleInSpaceRepository |
| 3 | +import io.ktor.server.application.* |
| 4 | +import io.ktor.server.cio.* |
| 5 | +import io.ktor.server.engine.* |
| 6 | +import io.ktor.server.routing.* |
| 7 | +import io.ktor.server.sse.* |
| 8 | +import io.ktor.util.collections.* |
| 9 | +import io.ktor.utils.io.streams.* |
| 10 | +import io.modelcontextprotocol.kotlin.sdk.* |
| 11 | +import io.modelcontextprotocol.kotlin.sdk.server.Server |
| 12 | +import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions |
| 13 | +import io.modelcontextprotocol.kotlin.sdk.server.SseServerTransport |
| 14 | +import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport |
| 15 | +import kotlinx.coroutines.Job |
| 16 | +import kotlinx.coroutines.runBlocking |
| 17 | +import kotlinx.io.asSink |
| 18 | +import kotlinx.io.buffered |
| 19 | + |
| 20 | + |
| 21 | +private val koin = initKoin(enableNetworkLogs = true).koin |
| 22 | + |
| 23 | +fun configureServer(): Server { |
| 24 | + val peopleInSpaceRepository = koin.get<PeopleInSpaceRepository>() |
| 25 | + |
| 26 | + val server = Server( |
| 27 | + Implementation( |
| 28 | + name = "mcp-kotlin PeopleInSpace server", |
| 29 | + version = "1.0.0" |
| 30 | + ), |
| 31 | + ServerOptions( |
| 32 | + capabilities = ServerCapabilities( |
| 33 | + prompts = ServerCapabilities.Prompts(listChanged = true), |
| 34 | + resources = ServerCapabilities.Resources(subscribe = true, listChanged = true), |
| 35 | + tools = ServerCapabilities.Tools(listChanged = true) |
| 36 | + ) |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | + server.addTool( |
| 42 | + name = "get-people-in-space", |
| 43 | + description = "The list of people in space endpoint returns the list of people in space right now" |
| 44 | + ) { |
| 45 | + val people = peopleInSpaceRepository.fetchPeople() |
| 46 | + CallToolResult( |
| 47 | + content = |
| 48 | + people.map { TextContent(it.name) } |
| 49 | + ) |
| 50 | + } |
| 51 | + |
| 52 | + return server |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Runs an MCP (Model Context Protocol) server using standard I/O for communication. |
| 57 | + * |
| 58 | + * This function initializes a server instance configured with predefined tools and capabilities. |
| 59 | + * It sets up a transport mechanism using standard input and output for communication. |
| 60 | + * Once the server starts, it listens for incoming connections, processes requests, |
| 61 | + * and executes the appropriate tools. The server shuts down gracefully upon receiving |
| 62 | + * a close event. |
| 63 | + */ |
| 64 | +fun `run mcp server using stdio`() { |
| 65 | + val server = configureServer() |
| 66 | + val transport = StdioServerTransport( |
| 67 | + System.`in`.asInput(), |
| 68 | + System.out.asSink().buffered() |
| 69 | + ) |
| 70 | + |
| 71 | + runBlocking { |
| 72 | + server.connect(transport) |
| 73 | + val done = Job() |
| 74 | + server.onClose { |
| 75 | + done.complete() |
| 76 | + } |
| 77 | + done.join() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Launches an SSE (Server-Sent Events) MCP (Model Context Protocol) server on the specified port. |
| 83 | + * This server enables clients to connect via SSE for real-time communication and provides endpoints |
| 84 | + * for handling specific messages. |
| 85 | + * |
| 86 | + * @param port The port number on which the SSE server should be started. |
| 87 | + */ |
| 88 | +fun `run sse mcp server`(port: Int): Unit = runBlocking { |
| 89 | + val servers = ConcurrentMap<String, Server>() |
| 90 | + |
| 91 | + val server = configureServer() |
| 92 | + embeddedServer(CIO, host = "0.0.0.0", port = port) { |
| 93 | + install(SSE) |
| 94 | + routing { |
| 95 | + sse("/sse") { |
| 96 | + val transport = SseServerTransport("/message", this) |
| 97 | + |
| 98 | + servers[transport.sessionId] = server |
| 99 | + |
| 100 | + server.onClose { |
| 101 | + servers.remove(transport.sessionId) |
| 102 | + } |
| 103 | + |
| 104 | + server.connect(transport) |
| 105 | + } |
| 106 | + post("/message") { |
| 107 | + val sessionId: String = call.request.queryParameters["sessionId"]!! |
| 108 | + val transport = servers[sessionId]?.transport as? SseServerTransport |
| 109 | + if (transport == null) { |
| 110 | + call.respond("Session not found", null) |
| 111 | + return@post |
| 112 | + } |
| 113 | + |
| 114 | + transport.handlePostMessage(call) |
| 115 | + } |
| 116 | + } |
| 117 | + }.start(wait = true) |
| 118 | +} |
0 commit comments