-
Notifications
You must be signed in to change notification settings - Fork 156
Introduce server sessions #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tiginamaria
wants to merge
19
commits into
main
Choose a base branch
from
tigina/server-session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
7415e24
Introduce server session
tiginamaria 24689c7
Fix websocket ktor server implementation, add test and logs
tiginamaria 66fbf29
Fix imports
tiginamaria 4f56c4f
Change api
tiginamaria 867bb6c
Changes after rebase
tiginamaria f8577b7
Remove Protocol from Server
tiginamaria 675c89e
Fix compilation
tiginamaria 5afd809
Fix linter, add enums instead of strings in capabilities assertion
tiginamaria beafed0
Dump api
tiginamaria 26c5d5b
Fix tests
tiginamaria c5c3d9a
Fix tests
tiginamaria 1f2120e
Stop -> StopSuspend
tiginamaria 8189e7d
Fix ktlint
tiginamaria 193ad6f
Fix tests
tiginamaria 9911bb2
update api: remove `ResourceReference`
devcrocod 1e29774
fix formatting
devcrocod 2de96a5
Fix tests
tiginamaria fd306c3
Update examples
tiginamaria 7c6e122
Fix api
tiginamaria File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ import kotlin.reflect.typeOf | |
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
private val LOGGER = KotlinLogging.logger { } | ||
private val logger = KotlinLogging.logger { } | ||
|
||
public const val IMPLEMENTATION_NAME: String = "mcp-ktor" | ||
|
||
|
@@ -211,6 +211,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
} | ||
} | ||
|
||
logger.info { "Starting transport" } | ||
return transport.start() | ||
} | ||
|
||
|
@@ -228,29 +229,29 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
} | ||
|
||
private suspend fun onNotification(notification: JSONRPCNotification) { | ||
LOGGER.trace { "Received notification: ${notification.method}" } | ||
logger.trace { "Received notification: ${notification.method}" } | ||
|
||
val handler = notificationHandlers[notification.method] ?: fallbackNotificationHandler | ||
|
||
if (handler == null) { | ||
LOGGER.trace { "No handler found for notification: ${notification.method}" } | ||
logger.trace { "No handler found for notification: ${notification.method}" } | ||
return | ||
} | ||
try { | ||
handler(notification) | ||
} catch (cause: Throwable) { | ||
LOGGER.error(cause) { "Error handling notification: ${notification.method}" } | ||
logger.error(cause) { "Error handling notification: ${notification.method}" } | ||
onError(cause) | ||
} | ||
} | ||
|
||
private suspend fun onRequest(request: JSONRPCRequest) { | ||
LOGGER.trace { "Received request: ${request.method} (id: ${request.id})" } | ||
logger.trace { "Received request: ${request.method} (id: ${request.id})" } | ||
|
||
val handler = requestHandlers[request.method] ?: fallbackRequestHandler | ||
|
||
if (handler === null) { | ||
LOGGER.trace { "No handler found for request: ${request.method}" } | ||
logger.trace { "No handler found for request: ${request.method}" } | ||
try { | ||
transport?.send( | ||
JSONRPCResponse( | ||
|
@@ -262,15 +263,15 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
), | ||
) | ||
} catch (cause: Throwable) { | ||
LOGGER.error(cause) { "Error sending method not found response" } | ||
logger.error(cause) { "Error sending method not found response" } | ||
onError(cause) | ||
} | ||
return | ||
} | ||
|
||
try { | ||
val result = handler(request, RequestHandlerExtra()) | ||
LOGGER.trace { "Request handled successfully: ${request.method} (id: ${request.id})" } | ||
logger.trace { "Request handled successfully: ${request.method} (id: ${request.id})" } | ||
|
||
transport?.send( | ||
JSONRPCResponse( | ||
|
@@ -279,7 +280,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
), | ||
) | ||
} catch (cause: Throwable) { | ||
LOGGER.error(cause) { "Error handling request: ${request.method} (id: ${request.id})" } | ||
logger.error(cause) { "Error handling request: ${request.method} (id: ${request.id})" } | ||
|
||
try { | ||
transport?.send( | ||
|
@@ -292,7 +293,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
), | ||
) | ||
} catch (sendError: Throwable) { | ||
LOGGER.error(sendError) { | ||
logger.error(sendError) { | ||
"Failed to send error response for request: ${request.method} (id: ${request.id})" | ||
} | ||
// Optionally implement fallback behavior here | ||
|
@@ -301,7 +302,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
} | ||
|
||
private fun onProgress(notification: ProgressNotification) { | ||
LOGGER.trace { | ||
logger.trace { | ||
"Received progress notification: token=${notification.params.progressToken}, progress=${notification.params.progress}/${notification.params.total}" | ||
} | ||
val progress = notification.params.progress | ||
|
@@ -314,7 +315,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
val error = Error( | ||
"Received a progress notification for an unknown token: ${McpJson.encodeToString(notification)}", | ||
) | ||
LOGGER.error { error.message } | ||
logger.error { error.message } | ||
onError(error) | ||
return | ||
} | ||
|
@@ -389,9 +390,9 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
* Do not use this method to emit notifications! Use notification() instead. | ||
*/ | ||
public suspend fun <T : RequestResult> request(request: Request, options: RequestOptions? = null): T { | ||
LOGGER.trace { "Sending request: ${request.method}" } | ||
logger.trace { "Sending request: ${request.method}" } | ||
val result = CompletableDeferred<T>() | ||
val transport = this@Protocol.transport ?: throw Error("Not connected") | ||
val transport = transport ?: throw Error("Not connected") | ||
|
||
if ([email protected]?.enforceStrictCapabilities == true) { | ||
assertCapabilityForMethod(request.method) | ||
|
@@ -401,7 +402,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
val messageId = message.id | ||
|
||
if (options?.onProgress != null) { | ||
LOGGER.trace { "Registering progress handler for request id: $messageId" } | ||
logger.trace { "Registering progress handler for request id: $messageId" } | ||
_progressHandlers.update { current -> | ||
current.put(messageId, options.onProgress) | ||
} | ||
|
@@ -451,12 +452,12 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
val timeout = options?.timeout ?: DEFAULT_REQUEST_TIMEOUT | ||
try { | ||
withTimeout(timeout) { | ||
LOGGER.trace { "Sending request message with id: $messageId" } | ||
logger.trace { "Sending request message with id: $messageId" } | ||
[email protected]?.send(message) | ||
} | ||
return result.await() | ||
} catch (cause: TimeoutCancellationException) { | ||
LOGGER.error { "Request timed out after ${timeout.inWholeMilliseconds}ms: ${request.method}" } | ||
logger.error { "Request timed out after ${timeout.inWholeMilliseconds}ms: ${request.method}" } | ||
cancel( | ||
McpError( | ||
ErrorCode.Defined.RequestTimeout.code, | ||
|
@@ -473,7 +474,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio | |
* Emits a notification, which is a one-way message that does not expect a response. | ||
*/ | ||
public suspend fun notification(notification: Notification) { | ||
LOGGER.trace { "Sending notification: ${notification.method}" } | ||
logger.trace { "Sending notification: ${notification.method}" } | ||
val transport = this.transport ?: error("Not connected") | ||
assertNotificationCapability(notification.method) | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.