-
Notifications
You must be signed in to change notification settings - Fork 13.5k
fix(mcp): migrate to MCP SDK 2.x so 2026-07-28 clients can connect #2363
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
afourney
merged 13 commits into
microsoft:main
from
cagdasyurekli:fix/mcp-sdk-2x-protocol-support
Sep 10, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c4e0195
fix(mcp): migrate to MCP SDK 2.x so 2026-07-28 clients can connect
cagdasyurekli 1d62c9d
Merge branch 'main' into fix/mcp-sdk-2x-protocol-support
afourney 94ff5d1
Restored plugins and non-localhost warning. Fixed CI integration of t…
afourney 34b912c
Updated mcp_tests to use a matrix.
afourney ad5202a
Ensure stable order of json-rpc responses
afourney a6a9d05
Merge branch 'main' into fix/mcp-sdk-2x-protocol-support
afourney a60d898
Merge branch 'main' into fix/mcp-sdk-2x-protocol-support
afourney 0d05656
Merge branch 'main' into fix/mcp-sdk-2x-protocol-support
afourney 1d18065
Merge upstream main into fix/mcp-sdk-2x-protocol-support
afourney 2219581
Pass tool errors via MCP
afourney 31b8b73
Added http transport tests.
afourney 0c904bf
State errors more generically.
afourney d74b64a
Update yield statement to use Path for URI
afourney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| """Exercise the combined HTTP app without a listening socket or subprocess.""" | ||
|
|
||
| import asyncio | ||
| import json | ||
| from contextlib import aclosing, asynccontextmanager | ||
| from urllib.parse import parse_qs, urlsplit | ||
|
|
||
| import httpx2 as httpx | ||
|
|
||
| from markitdown_mcp.__main__ import create_starlette_app, mcp | ||
|
|
||
|
|
||
| BASE_URL = "http://127.0.0.1:3001" | ||
| INITIALIZE = { | ||
| "jsonrpc": "2.0", | ||
| "id": 1, | ||
| "method": "initialize", | ||
| "params": { | ||
| "protocolVersion": "2025-06-18", | ||
| "capabilities": {}, | ||
| "clientInfo": {"name": "test", "version": "0"}, | ||
| }, | ||
| } | ||
| INITIALIZED = {"jsonrpc": "2.0", "method": "notifications/initialized"} | ||
| CONVERT = { | ||
| "jsonrpc": "2.0", | ||
| "id": 2, | ||
| "method": "tools/call", | ||
| "params": { | ||
| "name": "convert_to_markdown", | ||
| "arguments": {"uri": "data:text/plain,Hello%20from%20HTTP"}, | ||
| }, | ||
| } | ||
|
|
||
|
|
||
| class _SSEBody(httpx.AsyncByteStream): | ||
| def __init__(self, messages): | ||
| self.messages = messages | ||
|
|
||
| async def __aiter__(self): | ||
| while True: | ||
| message = await self.messages.get() | ||
| assert message["type"] == "http.response.body" | ||
| yield message.get("body", b"") | ||
| if not message.get("more_body", False): | ||
| return | ||
|
|
||
|
|
||
| @asynccontextmanager | ||
| async def sse_connection(app): | ||
| # ASGITransport buffers responses until completion, so drive the open SSE | ||
| # request directly and let httpx parse its streamed response body. | ||
| incoming = asyncio.Queue() | ||
| outgoing = asyncio.Queue() | ||
| await incoming.put({"type": "http.request", "body": b"", "more_body": False}) | ||
| scope = { | ||
| "type": "http", | ||
| "asgi": {"version": "3.0", "spec_version": "2.4"}, | ||
| "http_version": "1.1", | ||
| "method": "GET", | ||
| "scheme": "http", | ||
| "path": "/sse", | ||
| "raw_path": b"/sse", | ||
| "root_path": "", | ||
| "query_string": b"", | ||
| "headers": [ | ||
| (b"host", b"127.0.0.1:3001"), | ||
| (b"accept", b"text/event-stream"), | ||
| ], | ||
| "client": ("127.0.0.1", 1234), | ||
| "server": ("127.0.0.1", 3001), | ||
| } | ||
| task = asyncio.create_task(app(scope, incoming.get, outgoing.put)) | ||
| try: | ||
| start = await outgoing.get() | ||
| assert start["type"] == "http.response.start" | ||
| assert start["status"] == 200 | ||
| response = httpx.Response( | ||
| start["status"], | ||
| headers=start["headers"], | ||
| stream=_SSEBody(outgoing), | ||
| request=httpx.Request("GET", BASE_URL + "/sse"), | ||
| ) | ||
| assert response.headers["content-type"].startswith("text/event-stream") | ||
| async with aclosing(httpx.EventSource(response).__aiter__()) as events: | ||
| yield events | ||
| finally: | ||
| await incoming.put({"type": "http.disconnect"}) | ||
| await asyncio.wait_for(task, timeout=5) | ||
|
|
||
|
|
||
| def assert_conversion(response): | ||
| assert response["id"] == CONVERT["id"] | ||
| result = response["result"] | ||
| assert result["isError"] is False | ||
| assert result["content"][0]["text"] == "Hello from HTTP" | ||
|
|
||
|
|
||
| def test_streamable_http_initializes_with_app_lifespan(): | ||
| async def exercise(): | ||
| app = create_starlette_app(mcp) | ||
| async with app.router.lifespan_context(app): | ||
| async with httpx.AsyncClient( | ||
| transport=httpx.ASGITransport(app=app), | ||
| base_url=BASE_URL, | ||
| headers={"accept": "application/json, text/event-stream"}, | ||
| ) as client: | ||
| response = await client.post("/mcp", json=INITIALIZE) | ||
| assert response.status_code == 200 | ||
| assert response.headers["content-type"].startswith("application/json") | ||
| initialized = response.json() | ||
| assert initialized["id"] == INITIALIZE["id"] | ||
| assert initialized["result"]["serverInfo"]["name"] == "markitdown" | ||
| assert "mcp-session-id" not in response.headers | ||
|
|
||
| client.headers["mcp-protocol-version"] = initialized["result"][ | ||
| "protocolVersion" | ||
| ] | ||
| response = await client.post("/mcp", json=INITIALIZED) | ||
| assert response.status_code == 202 | ||
| response = await client.post("/mcp", json=CONVERT) | ||
| assert response.status_code == 200 | ||
| assert_conversion(response.json()) | ||
|
|
||
| asyncio.run(asyncio.wait_for(exercise(), timeout=15)) | ||
|
|
||
|
|
||
| def test_sse_session_uses_preserved_message_route(): | ||
| async def exercise(): | ||
| app = create_starlette_app(mcp) | ||
| async with app.router.lifespan_context(app): | ||
| async with httpx.AsyncClient( | ||
| transport=httpx.ASGITransport(app=app), base_url=BASE_URL | ||
| ) as client: | ||
| async with sse_connection(app) as events: | ||
| endpoint = await anext(events) | ||
| assert endpoint.event == "endpoint" | ||
| url = urlsplit(endpoint.data) | ||
| assert url.path == "/messages/" | ||
| assert parse_qs(url.query)["session_id"] | ||
|
|
||
| response = await client.post(endpoint.data, json=INITIALIZE) | ||
| assert response.status_code == 202 | ||
| event = await anext(events) | ||
| assert event.event == "message" | ||
| initialized = json.loads(event.data) | ||
| assert initialized["id"] == INITIALIZE["id"] | ||
| assert initialized["result"]["serverInfo"]["name"] == "markitdown" | ||
|
|
||
| response = await client.post(endpoint.data, json=INITIALIZED) | ||
| assert response.status_code == 202 | ||
| response = await client.post(endpoint.data, json=CONVERT) | ||
| assert response.status_code == 202 | ||
| event = await anext(events) | ||
| assert event.event == "message" | ||
| assert_conversion(json.loads(event.data)) | ||
|
|
||
| asyncio.run(asyncio.wait_for(exercise(), timeout=15)) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.