Make supposedly unreachable code less reachable#178
Conversation
The spec permits only one or two message types before initialization is complete (logging and one I can't remember). The logging one is odd because the spec shows elsewhere that logging messages shouldn't be sent until after the client makes a request to set logging level. But if there's exceptions now more could happen, so even if it's nonsensical or not allowed the endpoints (still wish there was a better catchall for client and server) should be robust against it. |
|
I'm not taking a principled stance in favor of supporting sending messages using IMcpServer before receiving the initialize notification. We could prevent sending messages until initialization is complete similar to how we don't give you an IMcpClient until McpClient.ConnectAsync receives an InitializeResult, but we've never blocked on this before. This PR is not that ambitious. It's just preventing the regression I caused yesterday by moving InitializeSession out of the McpServer constructor in #160 which now makes it possible to get an exception that "should be unreachable from public API!" If we want to make it easier to delay sending messages until receiving the initialization notification, we can do that in a follow up PR, but I don't think it's too important considering it's unusual for the server to send messages before handling some sort of action initiated by the client. |
When I removed IServerTransport, I combined
StartSessionandInitializeSession, but I should have moved the call to StartSession into the McpServer constructor so you don't run into the following "unreachable" exception from McpServer.SendMessageAsync and SendRequestAsync called after McpServerFactory.Create but before McpServer.RunAsync.It's unusual to send a message before handling any client requests like the initialization request, which is why this wasn't caught by our earlier tests, but it's possible that something like a log is fired off as fire-and-forget message, and there's no reason that should cause any issues like the exception fixed by this PR.