@@ -97,30 +97,30 @@ internal async Task<HttpResponseMessage> SendHttpRequestAsync(JsonRpcMessage mes
9797 }
9898
9999 var rpcRequest = message as JsonRpcRequest ;
100- JsonRpcMessage ? rpcResponseCandidate = null ;
100+ JsonRpcMessageWithId ? rpcResponseOrError = null ;
101101
102102 if ( response . Content . Headers . ContentType ? . MediaType == "application/json" )
103103 {
104104 var responseContent = await response . Content . ReadAsStringAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
105- rpcResponseCandidate = await ProcessMessageAsync ( responseContent , cancellationToken ) . ConfigureAwait ( false ) ;
105+ rpcResponseOrError = await ProcessMessageAsync ( responseContent , rpcRequest , cancellationToken ) . ConfigureAwait ( false ) ;
106106 }
107107 else if ( response . Content . Headers . ContentType ? . MediaType == "text/event-stream" )
108108 {
109109 using var responseBodyStream = await response . Content . ReadAsStreamAsync ( cancellationToken ) ;
110- rpcResponseCandidate = await ProcessSseResponseAsync ( responseBodyStream , rpcRequest , cancellationToken ) . ConfigureAwait ( false ) ;
110+ rpcResponseOrError = await ProcessSseResponseAsync ( responseBodyStream , rpcRequest , cancellationToken ) . ConfigureAwait ( false ) ;
111111 }
112112
113113 if ( rpcRequest is null )
114114 {
115115 return response ;
116116 }
117117
118- if ( rpcResponseCandidate is not JsonRpcMessageWithId messageWithId || messageWithId . Id != rpcRequest . Id )
118+ if ( rpcResponseOrError is null )
119119 {
120120 throw new McpException ( $ "Streamable HTTP POST response completed without a reply to request with ID: { rpcRequest . Id } ") ;
121121 }
122122
123- if ( rpcRequest . Method == RequestMethods . Initialize && rpcResponseCandidate is JsonRpcResponse )
123+ if ( rpcRequest . Method == RequestMethods . Initialize && rpcResponseOrError is JsonRpcResponse )
124124 {
125125 // We've successfully initialized! Copy session-id and start GET request if any.
126126 if ( response . Headers . TryGetValues ( "mcp-session-id" , out var sessionIdValues ) )
@@ -199,20 +199,20 @@ private async Task ReceiveUnsolicitedMessagesAsync()
199199 continue ;
200200 }
201201
202- var message = await ProcessMessageAsync ( sseEvent . Data , cancellationToken ) . ConfigureAwait ( false ) ;
202+ var rpcResponseOrError = await ProcessMessageAsync ( sseEvent . Data , relatedRpcRequest , cancellationToken ) . ConfigureAwait ( false ) ;
203203
204- // The server SHOULD end the response here anyway, but we won't leave it to chance. This transport makes
204+ // The server SHOULD end the HTTP response body here anyway, but we won't leave it to chance. This transport makes
205205 // a GET request for any notifications that might need to be sent after the completion of each POST.
206- if ( message is JsonRpcMessageWithId messageWithId && relatedRpcRequest ? . Id == messageWithId . Id )
206+ if ( rpcResponseOrError is not null )
207207 {
208- return messageWithId ;
208+ return rpcResponseOrError ;
209209 }
210210 }
211211
212212 return null ;
213213 }
214214
215- private async Task < JsonRpcMessage ? > ProcessMessageAsync ( string data , CancellationToken cancellationToken )
215+ private async Task < JsonRpcMessageWithId ? > ProcessMessageAsync ( string data , JsonRpcRequest ? relatedRpcRequest , CancellationToken cancellationToken )
216216 {
217217 try
218218 {
@@ -224,7 +224,12 @@ private async Task ReceiveUnsolicitedMessagesAsync()
224224 }
225225
226226 await WriteMessageAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
227- return message ;
227+ if ( message is JsonRpcResponse or JsonRpcError &&
228+ message is JsonRpcMessageWithId rpcResponseOrError &&
229+ rpcResponseOrError . Id == relatedRpcRequest ? . Id )
230+ {
231+ return rpcResponseOrError ;
232+ }
228233 }
229234 catch ( JsonException ex )
230235 {
0 commit comments