GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Client hangs when ctx.info() logging with pure Python client · Issue #201 · modelcontextprotocol/python-sdk

Client hangs when ctx.info() logging with pure Python client #201

New issue
Copy link
New issue
Copy link
Closed
Bug
Closed
Client hangs when ctx.info() logging with pure Python client#201
Bug
Copy link

Description

@sheffler
sheffler
opened on Feb 10, 2025
Issue body actions

Describe the bug
When a tool includes client-side logging with, the client seems to hang.
The following tool works ok with Claude Desktop and Inspector, but does not complete with a Python client.

@mcp.tool()
async def simple_tool_with_logging(x:float, y:float, ctx:Context) -> str:
    await ctx.info(f"Processing Simple Tool")
    logger.debug("IN SIMPLE_TOOL")
    await ctx.report_progress(1, 2)
    return x*y

To Reproduce
Will check in a PR with test and proposed fix.

Expected behavior
I would expect the tool to run to completion.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: OSX
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Reacted by Junpei Kawamoto and Micael Jarniac

Activity

  1. sheffler
    added a commit that references this issue on Feb 10, 2025

    Test and example MCP server to illustrate Issue modelcontextprotocol#201

    d69d94e
  2. dsp-ant
    added 3 commits that reference this issue on Mar 3, 2025

    refactor: improve typing with memory stream type aliases

    6dea29b

    refactor: update test files to use ParsedMessage

    09af7e1
  3. dsp-ant
    added 3 commits that reference this issue on Mar 13, 2025

    refactor: improve typing with memory stream type aliases

    8ea8bf5

    refactor: update test files to use ParsedMessage

    5be7a25

    refactor: reorganize message handling for better type safety and clar…

    9d0f2da
  4. samuelcolvin commented on Mar 16, 2025

    @samuelcolvin
    samuelcolvin
    on Mar 16, 2025
    Member
    More actions

    I've spent most of my day held up by this bug, see pydantic/pydantic-ai#1140 (comment)

    On main the problem is at

    python-sdk/src/mcp/shared/session.py

    Lines 344 to 346 in c897868

    await self._incoming_message_stream_writer.send(
    notification
    )

    If I change these lines to

    if not isinstance(notification.root, LoggingMessageNotification):
        await self._incoming_message_stream_writer.send(
            notification
        )

    Everything works correctly, obviously that's not a proper solution, but it demonstrates the problem.

    In case it helps, to reproduce this problem, run

    npx @pydantic/mcp-run-python sse

    (see pydantic/pydantic-ai#1140 for context)

    Then run

    import logging
    from logging import basicConfig
    
    from mcp import ClientSession, StdioServerParameters
    from mcp.client.sse import sse_client
    from mcp.client.stdio import stdio_client
    from devtools import debug
    
    import logfire
    
    
    logfire.configure()
    logfire.instrument_httpx(capture_all=True)
    basicConfig(handlers=[logfire.LogfireLoggingHandler()], level=logging.DEBUG)
    # disable httpx logs
    logging.getLogger('httpcore.http11').setLevel(logging.CRITICAL)
    logging.getLogger('httpx').setLevel(logging.CRITICAL)
    logging.getLogger('urllib3.connectionpool').setLevel(logging.CRITICAL)
    
    code = """
    import numpy
    a = numpy.array([1, 2, 3])
    print(a)
    a
    """
    
    
    async def call_tools(session: ClientSession):
        await session.initialize()
        await session.set_logging_level('debug')
        tools = await session.list_tools()
        debug(tools)
        result = await session.call_tool('run_python_code', {'python_code': code})
        debug(result)
    
    
    async def sse():
        async with sse_client('http://localhost:3001/sse') as (read, write):
            async with ClientSession(read, write) as session:
                await call_tools(session)
    
    
    async def stdio():
        server_params = StdioServerParameters(command='npx', args=['@pydantic/mcp-run-python', 'stdio'])
        async with stdio_client(server_params) as (read, write):
            async with ClientSession(read, write) as session:
                await call_tools(session)
    
    
    if __name__ == '__main__':
        import asyncio
    
        with logfire.span('running sse'):
            asyncio.run(sse())

    Obviously you can disable the logfire bit, I was using it to see the raw http requests, I see the same hanging behaviour with both SSE and stdio.

    As I mentioned in the comment linked above, you can also comment out await session.set_logging_level('debug') to avoid the issue.

    Reacted by Peter Hanzo and Thierry Jean
  5. sheffler commented on Mar 16, 2025

    @sheffler
    sheffler
    on Mar 16, 2025
    ContributorAuthor
    More actions

    Those are the same three lines i identified, and I believe is deadlock. PR #202 is an end-to-end test that illustrates the condition.

  6. dsp-ant
    added a commit that references this issue on Mar 19, 2025

    Move incoming message stream from BaseSession to ServerSession

    85514b4
  7. dsp-ant
    closed this as completedin 568cbd1on Mar 24, 2025
  8. hwaxxer
    added a commit that references this issue on Mar 24, 2025

    Fix modelcontextprotocol#201: Move incoming message stream from BaseS…

    This issue will close once commit 568cbd1 is merged into the 'main' branch.
    568cbd1
  9. tanhaipeng
    marked sse client read progress notification #261 as a duplicate of this issue on Mar 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.