Repository navigation
feat(mcp): support MCP Python SDK 2.x (#3643) - #3735
Merged
Merged
Conversation
MCP Python SDK 2 renamed FastMCP to MCPServer (mcp.server.fastmcp is gone) and moved transport_security from the constructor to sse_app(). Import MCPServer when available and fall back to FastMCP on 1.x, passing transport_security where each version expects it. Allow mcp < 3 in the mcp extra. 1.x stays supported because semgrep (dev group) pins mcp==1.29.0: a v2-only pin makes uv resolve semgrep down to 1.79.0. tests/test_mcp.py now passes plain strings to read_resource() and reads uriTemplate through model_dump(by_alias=True), so it runs on both SDKs.
Owner
|
Just merged into develop. Thanks for the PR @anandghegde ! |
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.
Description
Following the v1 to v2 migration guide, two changes in MCP Python SDK 2 affect
glances/outputs/glances_mcp.py:FastMCPwas renamed toMCPServerandmcp.server.fastmcpis gone. That import error is why--enable-mcpdid nothing with mcp 2.0 (mcp extra allows mcp 2.0.0 which breaks --enable-mcp (No module named 'mcp.server.fastmcp') #3640).transport_securitymoved from the constructor tosse_app(). If it's left in the constructor, the DNS rebinding allow-list built frommcp_allowed_hostsis no longer applied.The module now imports
MCPServerand falls back toFastMCPon 1.x. It passestransport_securitytosse_app()on 2.x and to the constructor on 1.x.TransportSecuritySettingsis imported frommcp.server.transport_security, where the guide documents it. That module exists in 1.x too. Nothing else in the resource and prompt code needed to change.The
mcpextra now allowsmcp>=1.0.0,<3. I kept 1.x working instead of requiring 2.x because semgrep in the dev group pinsmcp==1.29.0. Withmcp>=2,<3,uv lockstill succeeds, but it quietly drops semgrep to 1.79.0 (from 2024). With<3, the lock keeps semgrep 1.177.0 and mcp 1.29.0, so the generated requirements files stay valid and don't need regenerating.pip install glances[mcp]now gets mcp 2.x. Once semgrep allows mcp 2, the lower bound can go up and the 1.x branch can be removed.tests/test_mcp.pypasses plain strings toread_resource(), since v2 only acceptsstr. It readsuriTemplatethroughmodel_dump(by_alias=True)because v2 renamed the attributes to snake_case. With those changes the same tests run on both SDKs.How I tested (macOS, Python 3.12/3.13, uv venvs):
pytest tests/test_mcp.py: 25 passed with mcp 2.2.0, and 25 passed with mcp 1.30.0. On unmodifieddevelopwith mcp 2.2.0 the suite fails, because the server never mounts/mcp.uv pip install -e '.[web,mcp]'in a clean venv resolves mcp 2.2.0.pytest tests/test_mcp.py tests/test_core.pythen gives 76 passed, 4 skipped.pytest testswith mcp 2.2.0, skipping memoryleak, perf and webui: 929 passed. The 15 failures are all intest_actions_sanitize.pyand fail the same way on unmodifieddevelopin that venv, which doesn't have theactionextra (chevron).glances -w --disable-webui --enable-mcpwith mcp 2.2.0 and connected with an mcp 2.2.0 SSE client:curl -H "Host: evil.example" http://127.0.0.1:PORT/mcp/ssereturns 421, andlocalhostreturns 200. The server logs aRequest validation failedtraceback for the rejected request, but mcp 1.30 logs the same thing.ruff checkandruff format --check(0.15.4, the pre-commit version) pass on the changed files.One behaviour change comes from the SDK itself: in 2.x, sync resource and prompt handlers run on a worker thread instead of the event loop. They only read from the stats object, and the tests and manual calls above ran without problems.
Resume