Upgrade MCP SDK to 2.3 and switch transport from SSE to Streamable HTTP

- Drop the unused fastmcp wrapper pin (mcp-requirements.txt) and depend on
  the official SDK directly: mcp>=2.3,<3. The code only ever imported
  mcp.server.* modules; fastmcp was pulled in solely as a dependency
  vehicle and its <4 pin kept mcp locked at 1.x.
- Migrate to the SDK 2.x API: FastMCP -> MCPServer
  (mcp.server.mcpserver); transport_security moves from the constructor
  into streamable_http_app().
- Replace the deprecated SSE transport (spec-deprecated 2025-06-18) with
  Streamable HTTP at POST/GET/DELETE /v3/mcp/transport, mounted at /v3/mcp.
- Run the StreamableHTTP session manager in the FastAPI lifespan
  (Starlette does not execute mounted sub-app lifespans), entered before
  startup finishes so no request passes the MCP ready gate first.
- Authenticate every request (the SSE wrapper only checked the opening
  GET and trusted POSTs by session). Add a 5-minute TTL cache for API-key
  resolutions so the stricter per-request check does not cost a bcrypt
  round on every call.
- Update docs to the new endpoint, auth flow, and client configuration.

Wire-format verified unchanged against SDK 1.30 (structured output
auto-detection and content conversion are byte-identical for the
list[dict] returns used by all tool handlers).
This commit is contained in:
yueguobin committed 2026-11-01 23:09:38 +08:00
1 parent a5e98e2109
commit fe1b2a5cb0
8 files changed
+160 -70

No files matched your search

+16 -5
View File
@@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import asyncio
import contextlib
import logging
from contextlib import asynccontextmanager
@@ -36,11 +37,21 @@ log = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
await startup(app)
try:
yield
finally:
await shutdown(app)
# The MCP Streamable HTTP session manager runs for the app's whole
# lifetime (mounted sub-app lifespans are not executed by Starlette).
# Entered before startup so it is running before the MCP ready gate opens.
from gns3server.agent import MCP_AVAILABLE
async with contextlib.AsyncExitStack() as stack:
if MCP_AVAILABLE:
from gns3server.agent.mcp import mcp_session_manager_lifespan
await stack.enter_async_context(mcp_session_manager_lifespan())
await startup(app)
try:
yield
finally:
await shutdown(app)
async def startup(app: FastAPI) -> None: