-
Notifications
You must be signed in to change notification settings - Fork 235
HttpRequest connecting to the /sse
endpoint hits (number of tools + 1) times while building a custom auth middleware
#316
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
Comments
LLM translation was used Quick ExplanationOnly the initial request will use the URL you provided. For example, if you enter Root Cause AnalysisThrough browser packet inspection and comparison with the Python SDK, I've analyzed the SSE communication protocol: When using
Behavior branches:
In this case, the server responds with:
This causes all subsequent client requests to be sent to The client then makes two POST requests to the message endpoint (with the sessionId):
This explains why you observe 3 requests total - not Recommended SolutionI suggest modifying the middleware to:
This approach maintains security while accommodating the protocol's expected flow. |
Generally we'd advise against putting an API key in the query string because the query string tends to get logged a lot. We'd encourage you to configure your MCP client to set a request header with the API key. If you're working with the dotnet client, you can use SseClientTransportOptions.AdditionalHeaders, and that should be sent with all request for a given session. As for why If you absolutely must control the query string, you can work at a lower layer with the SseResponseStreamTransport which is what csharp-sdk/tests/ModelContextProtocol.AspNetCore.Tests/SseIntegrationTests.cs Lines 225 to 283 in 0c9e91f
|
Describe the bug
A clear and concise description of what the bug is.
I was adding a middleware that checks the request header whether an API key exists or not. Here's the code that I wrote:
https://github.com/microsoft/mcp-dotnet-samples/blob/f2131feb5ae79fe4491e20dee4f17089ff5b2096/youtube-subtitles-extractor-containerapp/src/McpYouTubeSubtitlesExtractor.ContainerApp/Middlewares/ApiKeyValidationMiddleware.cs#L13-L40
When I added this middleware, I found a strange behaviour that:
null
.null
value from the request header.To Reproduce
Steps to reproduce the behavior:
youtube-subtitles-extractor-containerapp
directory.http://0.0.0.0:5202/sse?code=abcde
and see it hits 3 times (because this server has 2 tools) - the first one has the key but the rest doesn't have it.Expected behavior
A clear and concise description of what you expected to happen.
Logs
If applicable, add logs to help explain your problem.
Additional context
Add any other context about the problem here.
If it's not the proper approach to add custom authentication logic, which will be the best one?
The text was updated successfully, but these errors were encountered: