Skip to content

SSE transport for server does not work inside custom Ktor's Route #94 (#236 and #237)#314

Draft
IainHull wants to merge 3 commits intomodelcontextprotocol:mainfrom
IainHull:ktor-api-fix
Draft

SSE transport for server does not work inside custom Ktor's Route #94 (#236 and #237)#314
IainHull wants to merge 3 commits intomodelcontextprotocol:mainfrom
IainHull:ktor-api-fix

Conversation

@IainHull
Copy link

This should also fix Issues #236 and #237

Changes the Ktor API from

fun Routing.mcp(block: ServerSSESession.() -> Server)

to

fun Route.mcp(block: suspend ServerSSESession.() -> Server)

Motivation and Context

This fixes

  • mcp inside a route is bound to the path specified in the route not the root path.
  • ability to publish multiple mcp servers
  • can create the server asynchronously
  • together this enables developers to created multi tenanted mcp servers

This enables code the following style of code

routing {
    route("mcp/{application}") {
        mcp {
            val authHeader = call.request.header("Authorization") ?: throw IllegalArgumentException("Authorization header required")
            val application = call.parameters["application"] ?: throw IllegalArgumentException("Application required")
            return@mcpFixed mcpServerManager.getOrCreateServer(authHeader, application)
        }
    }
}

How Has This Been Tested?

Tested this with npx @modelcontextprotocol/inspector and Flowise

Breaking Changes

This maintains source compatibility, but not binary compatibility.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

Address

@IainHull
Copy link
Author

Sorry for the churn, there were two pieces missing from my initial commit.

@kpavlov would you be able to trigger another build?

kpavlov added a commit that referenced this pull request Feb 18, 2026
… (#531)

# Migrate Ktor extension functions from `Routing` to `Route` (#237)

- Replaced `Routing` with `Route` in MCP-related functions for better
composability in nested contexts.
- Updated tests to verify compatibility with nested routes and subpaths.
- Deprecated legacy signatures and removed redundant overloads.

## How Has This Been Tested?
Unit test

## Breaking Changes
`Routing.mcp(...) `extension was replaced with `Route.mcp(...)`

## Types of changes

- [X] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update

## Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply. -->
- [x] I have read the [MCP
Documentation](https://modelcontextprotocol.io)
- [x] My code follows the repository's style guidelines
- [ ] New and existing tests pass locally
- [ ] I have added appropriate error handling
- [ ] I have added or updated documentation as needed

## Additional context
Based on #314 by @IainHull but that PR is too outdated.
@kpavlov
Copy link
Contributor

kpavlov commented Feb 18, 2026

Incorporated in #531

@kpavlov kpavlov marked this pull request as draft February 18, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants