Skip to content

Commit 5f35711

Browse files
committed
Fix an issue with mounting using a FastAPI router with a root path
- Fix a bug where the mount path was not correctly appended to the root path of the FastAPI router - Replace a raise statement with an assert, because the code is unreachable if the input attribute has correct type
1 parent 1ec91a8 commit 5f35711

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

fastapi_mcp/server.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ def mount(
299299
str,
300300
Doc(
301301
"""
302-
Path where the MCP server will be mounted. Defaults to '/mcp'.
302+
Path where the MCP server will be mounted.
303+
Mount path is appended to the root path of FastAPI router, or to the prefix of APIRouter.
304+
Defaults to '/mcp'.
303305
"""
304306
),
305307
] = "/mcp",
@@ -328,14 +330,9 @@ def mount(
328330
router = self.fastapi
329331

330332
# Build the base path correctly for the SSE transport
331-
if isinstance(router, FastAPI):
332-
base_path = router.root_path
333-
elif isinstance(router, APIRouter):
334-
base_path = self.fastapi.root_path + router.prefix
335-
else:
336-
raise ValueError(f"Invalid router type: {type(router)}")
337-
338-
messages_path = f"{base_path}{mount_path}/messages/"
333+
assert isinstance(router, (FastAPI, APIRouter)), f"Invalid router type: {type(router)}"
334+
base_path = mount_path if isinstance(router, FastAPI) else router.prefix + mount_path
335+
messages_path = f"{base_path}/messages/"
339336

340337
sse_transport = FastApiSseTransport(messages_path)
341338

0 commit comments

Comments
 (0)