-
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
Merged
+1,191
−602
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6fcc82f
Introduce server session
tiginamaria 80716cd
Fix websocket ktor server implementation, add test and logs
tiginamaria b9014d9
Fix imports
tiginamaria 708e154
Change api
tiginamaria 5152ab6
Changes after rebase
tiginamaria 9b03ea6
Remove Protocol from Server
tiginamaria 4a1631d
Fix compilation
tiginamaria fde886c
Fix linter, add enums instead of strings in capabilities assertion
tiginamaria 901eb94
Dump api
tiginamaria 65c187f
Fix tests
tiginamaria dbaf7c5
Fix tests
tiginamaria 46c8567
Stop -> StopSuspend
tiginamaria 1403db9
Fix ktlint
tiginamaria 6d5f649
Fix tests
tiginamaria 282b1bd
update api: remove `ResourceReference`
devcrocod efbe8e8
fix formatting
devcrocod 9268608
Fix tests
tiginamaria dc98708
Update examples
tiginamaria 5ba48b0
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.