Skip to content
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

progress_token is 0 on first tool-call and the return is taken mistakenly #176

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mcp/server/fastmcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ async def report_progress(
else None
)

if not progress_token:
if progress_token is None:
return

await self.request_context.session.send_progress_notification(
Expand Down
46 changes: 46 additions & 0 deletions tests/issues/test_176_progress_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from unittest.mock import AsyncMock, MagicMock

import pytest

from mcp.server.fastmcp import Context
from mcp.shared.context import RequestContext

pytestmark = pytest.mark.anyio


async def test_progress_token_zero_first_call():
"""Test that progress notifications work when progress_token is 0 on first call."""

# Create mock session with progress notification tracking
mock_session = AsyncMock()
mock_session.send_progress_notification = AsyncMock()

# Create request context with progress token 0
mock_meta = MagicMock()
mock_meta.progressToken = 0 # This is the key test case - token is 0

request_context = RequestContext(
request_id="test-request", session=mock_session, meta=mock_meta
)

# Create context with our mocks
ctx = Context(request_context=request_context, fastmcp=MagicMock())

# Test progress reporting
await ctx.report_progress(0, 10) # First call with 0
await ctx.report_progress(5, 10) # Middle progress
await ctx.report_progress(10, 10) # Complete

# Verify progress notifications
assert (
mock_session.send_progress_notification.call_count == 3
), "All progress notifications should be sent"
mock_session.send_progress_notification.assert_any_call(
progress_token=0, progress=0.0, total=10.0
)
mock_session.send_progress_notification.assert_any_call(
progress_token=0, progress=5.0, total=10.0
)
mock_session.send_progress_notification.assert_any_call(
progress_token=0, progress=10.0, total=10.0
)
2 changes: 1 addition & 1 deletion tests/shared/test_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ async def http_client(server, server_url) -> AsyncGenerator[httpx.AsyncClient, N
async def test_raw_sse_connection(http_client: httpx.AsyncClient) -> None:
"""Test the SSE connection establishment simply with an HTTP client."""
async with anyio.create_task_group():

async def connection_test() -> None:
async with http_client.stream("GET", "/sse") as response:
assert response.status_code == 200
Expand Down Expand Up @@ -211,7 +212,6 @@ async def initialized_sse_client_session(
yield session



@pytest.mark.anyio
async def test_sse_client_happy_request_and_response(
initialized_sse_client_session: ClientSession,
Expand Down