Add ServerSSESession as a receiver for mcp {} dsl (#190)

* Add ServerSSESession as a receiver for mcp {} dsl

* update api

---------

Co-authored-by: devcrocod <devcrocod@gmail.com>
This commit is contained in:
Artem Bukhonov 2025-08-04 20:13:21 +02:00 • committed by GitHub
commit 111d31b1fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,7 +19,7 @@ import io.ktor.utils.io.KtorDsl
private val logger = KotlinLogging.logger {}
@KtorDsl
public fun Routing.mcp(path: String, block: () -> Server) {
public fun Routing.mcp(path: String, block: ServerSSESession.() -> Server) {
route(path) {
mcp(block)
}
@ -29,7 +29,7 @@ public fun Routing.mcp(path: String, block: () -> Server) {
* Configures the Ktor Application to handle Model Context Protocol (MCP) over Server-Sent Events (SSE).
*/
@KtorDsl
public fun Routing.mcp(block: () -> Server) {
public fun Routing.mcp(block: ServerSSESession.() -> Server) {
val transports = ConcurrentMap<String, SseServerTransport>()
sse {
@ -43,12 +43,12 @@ public fun Routing.mcp(block: () -> Server) {
@Suppress("FunctionName")
@Deprecated("Use mcp() instead", ReplaceWith("mcp(block)"), DeprecationLevel.WARNING)
public fun Application.MCP(block: () -> Server) {
public fun Application.MCP(block: ServerSSESession.() -> Server) {
mcp(block)
}
@KtorDsl
public fun Application.mcp(block: () -> Server) {
public fun Application.mcp(block: ServerSSESession.() -> Server) {
val transports = ConcurrentMap<String, SseServerTransport>()
install(SSE)
@ -67,7 +67,7 @@ public fun Application.mcp(block: () -> Server) {
private suspend fun ServerSSESession.mcpSseEndpoint(
postEndpoint: String,
transports: ConcurrentMap<String, SseServerTransport>,
block: () -> Server,
block: ServerSSESession.() -> Server,
) {
val transport = mcpSseTransport(postEndpoint, transports)