@@ -145,12 +145,23 @@ async def handle_post_message():
145
145
await response (scope , receive , send )
146
146
return
147
147
148
- # As part of the MCP spec we need to initialize first.
148
+ # As part of the MCP spec we need to initialize before we can handle
149
+ # any other messages; the server has this assumption embedded in it.
149
150
# In a stateful flow (e.g. stdio or sse transports) the client would
150
151
# send an initialize request to the server, and the server would send
151
152
# a response back to the client. In this case we're trying to be stateless,
152
- # so we'll handle the initialization ourselves.
153
- await initialize (read_stream_writer , write_stream_reader )
153
+ # so we'll handle the initialization ourselves, unless this happens to be
154
+ # an initialization request.
155
+ try :
156
+ types .InitializeRequest .model_validate (
157
+ client_message .root .model_dump (
158
+ by_alias = True , mode = "json" , exclude_none = True
159
+ )
160
+ )
161
+ logger .debug ("Skipping automatic initialization" )
162
+ except ValidationError :
163
+ logger .debug ("Automatically handling initialization" )
164
+ await initialize (read_stream_writer , write_stream_reader )
154
165
155
166
# Alright, now we can send the client message.
156
167
logger .debug ("Sending client message" )
0 commit comments