Skip to content

fix(server): respect include_router prefix when mounting MCP endpoints (adopted from #329, fixes #204) - #341

Closed
K4bain wants to merge 1 commit into
tadata-org:mainfrom
K4bain:fix/include-router-prefix
Closed

K4bain wants to merge 1 commit into
tadata-org:mainfrom
K4bain:fix/include-router-prefix

Conversation

@K4bain

@K4bain K4bain commented Aug 27, 2026

Copy link
Copy Markdown

Adopted from #329 (original patch by @wakqasahmed) — rebased onto current main and verified. Fixes #204.

Why this is needed

The documented FastAPI pattern — define routes on a sub-router, then app.include_router(router, prefix=/api) — is broken for MCP mounting: mount_http()/mount_sse() registered the MCP endpoints under the app root because the include-time prefix was never recovered. Issue #204's exact repro (a pure mount-point router with zero routes of its own) is the worst case: there is nothing to diff paths against.

The fix resolves the include prefix in two tiers:

  1. FastAPI >= 0.137: read FastAPI's own include-time bookkeeping (_IncludedRouter.original_router identity + include_context.prefix) — exact, works even for empty routers, immune to endpoint reuse across routers.
  2. Older FastAPI: diff the router's routes against their included counterparts (same endpoint fn + methods + path suffix), requiring every route to agree before accepting a prefix.

Ambiguity (router included at multiple prefixes, shared endpoints on old FastAPI) falls back to the safe empty prefix rather than guessing wrong.

Verification

  • 4 new tests (incl. the literal [BUG] router prefix is ignored #204 repro, shared-endpoint-across-routers, multi-prefix inclusion, and the plain prefixed-router case) — all fail on main, pass with the fix (FastAPI 0.141.1, metadata path).
  • Full unit suite: 73 passed (69 existing + 4 new).

Credit to @wakqasahmed for the original report and patch in #329.

mount_http()/mount_sse() ignored the prefix a router was included with
via app.include_router(router, prefix=...), mounting MCP endpoints at
the app root instead (tadata-org#204). Resolve the include prefix from FastAPI's
own include-time bookkeeping (>= 0.137 _IncludedRouter metadata) with a
path-diffing fallback for older FastAPI; ambiguous or unresolvable
prefixes fall back to the safe empty prefix instead of guessing.

Adopted from tadata-org#329 (original patch by @wakqasahmed).
@K4bain

K4bain commented Sep 14, 2026

Copy link
Copy Markdown
Author

Closing for housekeeping - the repo had no reviewer engagement and we are re-approaching these areas through a narrower pipeline. Happy to reopen on request.

@K4bain K4bain closed this Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] router prefix is ignored

1 participant