Fix a bug with handling FastAPI root_path
parameter
#163
+58
−50
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe your changes
Issue resolving
The current version of the library does not handle FastAPI's
root_path
parameter correctly.FastAPI.router
includesroot_path
in all its routes by default, so there is no need to addroot_path
to/messages
again, as is done in the current version offastapi_mcp
. The problem is located here.Because of these duplicated
root_path
annotations, all requests to the/messages
endpoints are processed incorrectly. This issue can be easily reproduced using the following snippet:When you run MCP Inspector with
http://127.0.0.1:8101/mcp
connection string you will see the following log in the uvicorn output:When you run MCP Inspector with
http://127.0.0.1:8101/api/mcp
connection string you will see the following log in the uvicorn output:This PR fixes this issue.
Tests
This PR introduces some new test to prevent this issue in the future and also it refactors some basic fixtures to enhance flexibility by supporting multiple configurations for test servers.
I think there were a better way to add new tests for this
root_path
issue, but unfortunately I couldn't find an easy way to resolve a full URL to FastAPI's endpoint. If you can give me an advice how it could be enhanced - please do it and I'll prepare an update.Issue ticket number and link (if applicable)
N/A
Screenshots of the feature / bugfix
FastAPI's
root_path
part duplication in the POST request tomessages
endpoint:An example of the broken unit tests, that were added in this PR in the case if we do not apply the main changes to
fastapi_mcp/server.py
(with the changes from this PR all tests are passed correctly).With the changes from this PR all tests are passed correctly:
Checklist before requesting a review