fix(mcp): make the streamable-HTTP endpoint reachable across the LAN - #79
Open
jaymeklein wants to merge 5 commits into
Open
fix(mcp): make the streamable-HTTP endpoint reachable across the LAN#79jaymeklein wants to merge 5 commits into
jaymeklein wants to merge 5 commits into
Conversation
FastMCP defaults its host to localhost, so the MCP SDK auto-enabled DNS-rebinding Host validation with a loopback-only allowlist. Every non-loopback Host was then refused with 421 Invalid Host header, making /api/mcp unreachable from other machines on the network. EmbedBase authenticates every MCP request with an API key, so authorization never depends on network position: a DNS-rebinding request carries no key and is refused with 401 regardless. Disable the transport Host allowlist and let the API key be the sole boundary, so the endpoint is reachable from any machine by any address or name. Content-Type validation on POST is unaffected.
mcp 2.0.0 restructured the SDK (moved mcp.server.fastmcp, changed the transport config), so the unbounded mcp>=1.0.0 requirement pulled 2.0.0 on a fresh CI install and broke conftest import (No module named mcp.server.fastmcp), failing the unit and integration jobs. Pin to 1.x and raise the floor to the verified 1.28.1; the API the code uses is unchanged through 1.29.0 (full suite green).
# Conflicts: # api/requirements.txt # api/services/mcp/server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
The embedded MCP streamable-HTTP endpoint (
/api/mcp/) was unreachable from any machine other than the server host. This makes it reachable across the LAN.Why it was broken
FastMCP defaults its
hostto127.0.0.1. When notransport_securityis passed and the host is loopback, the MCP SDK auto-enables DNS-rebinding protection with a loopback-only Host allowlist (mcp/server/fastmcp/server.py). So:Hostlocalhost:3636/127.0.0.1:3636192.168.x.y:3636(LAN IP)Every LAN client sends the server's own address as its
Host, so the endpoint 421'd for everyone off-box — even though the client config, token, and network path were all correct.The fix
build_mcp_server()now passes an explicitTransportSecuritySettings(enable_dns_rebinding_protection=False), disabling the Host allowlist so anyHostis accepted.Why this is safe
Authorization on the MCP endpoint has never depended on network position — every request is authenticated by an API key (
Authorization: Bearer/X-API-Key) inapi/services/mcp/middleware.py, ahead of the transport. DNS-rebinding protection guards unauthenticated, network-position-trusting services; here:Content-Type: application/jsoncheck on POST is unaffected (it runs before the disabled gate).The API key is now the sole boundary — operators exposing this on a shared network should keep a strong
MASTER_API_KEY, prefer scoped user keys over sharing the master, and setEMBEDBASE_SECURE_HEADERS=true(as the README already advises for LAN).Testing
tests/unit/test_mcp_transport_security.py: asserts the built server disables protection; that the disabled setting reaches the livesession_manager(the wiring FastMCP feeds to the request-path middleware — not just the settings object, so an SDK change that stopped forwarding it fails here instead of silently 421'ing LAN clients); plus a non-tautological check that the same LAN Host is rejected (421) under a loopback-only allowlist but accepted under our settings — exercised over the POST path real tool calls use.ruff✅ ·mypy✅ (123 files) ·pytest✅ (973 passed, 13 skipped).Host→ HTTP 200 (protection off), and the livesession_manager.security_settingscarriesenable_dns_rebinding_protection=False.Deploying / client impact
LAN_HOSTis not needed for MCP (it only affects the console's advertised address).http://<host-ip>:3636/api/mcp/URL now works directly (no SSH tunnel needed).Merged onto current
main(conflicts resolved)This branch was based on an older
main; it's now merged up to currentmain(db18cfa, PR #78 — self-service MCP tools,get_checksum, migrations 0010–0012). Two conflicts, both resolved:api/requirements.txt—mainindependently pinnedmcp==1.28.1(6f5372d); resolved to that exact pin and dropped this branch's now-redundantmcp>=1.28.1,<2. The PR therefore no longer changes requirements vsmain.api/services/mcp/server.py—mainsplit tool registration into per-domain groups and added the tool catalogue; that rework is combined with the transport fix, so both coexist: theTransportSecuritySettingsimport, the_transport_security()helper, thetransport_security=arg on theFastMCP(...)constructor, and main's_DOMAIN_REGISTRARSregistration loop.Also — LAN file downloads (docs)
S3/MinIO file downloads hit the same class of bug: presigned URLs are signed against
storage.public_endpoint_url, which defaults tolocalhost:9000, so the API 302's the browser to its own localhost from any other machine (both the parse download and the "download original" button). The real fix is per-deployment runtime config in the gitignoredconfig.yaml/.env, so this PR documents it in the tracked templates:.env.example—S3__MINIO__PUBLIC_ENDPOINT_URL(the browser-facing presign host → set to the LAN IP) plus aCORS_ORIGINSnote (add the LAN console origin so the cross-origin presigned GET passes CORS).config.example.yaml— a note onpublic_endpoint_urlfor multi-machine access.