Skip to content

Commit 8bdd815

Browse files
committed
Don't auto-initialize if the incoming request is an InitializationRequest
1 parent 859b50c commit 8bdd815

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/mcp_grafana/transports/http.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,23 @@ async def handle_post_message():
145145
await response(scope, receive, send)
146146
return
147147

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.
149150
# In a stateful flow (e.g. stdio or sse transports) the client would
150151
# send an initialize request to the server, and the server would send
151152
# 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)
154165

155166
# Alright, now we can send the client message.
156167
logger.debug("Sending client message")

0 commit comments

Comments
 (0)