modelcontextprotocol / modelcontextprotocol/kotlin-sdk

Passing info about user/tenant to tools

Open
#236 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

api enhancement P2 ready for work
Dominant language
Kotlin
Stars
1.5k
Forks
248
Avg merge
1d 20h
Merged PRs (30d)
23

Description

Describe the bug

I can not understand how we should get information about the currently logged in user in the MCP request. We have added bearer authentication with OAuth flow for logging in to MCP, but it does not seem to be possible to pass information about the Ktor Principal or the tenant (can derive this from ApplicationCall). It seems to get lost here and I don't understand how to pass on more info here

    public suspend fun handlePostMessage(call: ApplicationCall) {
        if (!initialized.load()) {
            val message = "SSE connection not established"
            call.respondText(message, status = HttpStatusCode.InternalServerError)
            _onError.invoke(IllegalStateException(message))
        }

        val body = try {
            val ct = call.request.contentType()
            if (ct != ContentType.Application.Json) {
                error("Unsupported content-type: $ct")
            }

            call.receiveText()
        } catch (e: Exception) {
            call.respondText("Invalid message: ${e.message}", status = HttpStatusCode.BadRequest)
            _onError.invoke(e)
            return
        }

        try {
            handleMessage(body)
        } catch (e: Exception) {
            call.respondText("Error handling message $body: ${e.message}", status = HttpStatusCode.BadRequest)
            return
        }

        call.respondText("Accepted", status = HttpStatusCode.Accepted)
    }

So here we have ApplicationCall, but in handleMessage() it seems like we only have the body.

So we need a way to pass more arbitrary data. In particular:

  • Ktor principal
  • Any attributes we have on the ApplicationCall

So having full access to ApplicationCall downstream would be great!

Many thanks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at handlePostMessage and trace how it calls handleMessage, noting where ApplicationCall context is reduced to only the request body. Define a way for downstream handling to access the Ktor Principal and arbitrary ApplicationCall attributes, and confirm that this context remains available for authenticated MCP requests.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
api, authentication, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.