modelcontextprotocol / modelcontextprotocol/python-sdk

STDIO hangs forever when the using multiprocessing in tools

オープン
#817 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

enhancement needs decision P3
主要言語
Python
スター
24.3k
フォーク
4k
平均マージ
1日 1時間
マージ済み PR(30日)
31

説明

Describe the bug
when multiprocessing in a tool, and start mcp client by STDIO, the mcp server never response.

To Reproduce
Steps to reproduce the behavior:

  1. write mcp server:
## test_mcp.py
from mcp.server.fastmcp import FastMCP
import multiprocessing as mp


mcp = FastMCP()

@mcp.tool()
def sum() -> int:
    "count number"
    with mp.Pool(processes=2) as pool:
        res =  pool.imap(str, range(10))
        res = list(res)
    return res

if __name__ == '__main__':
    mcp.run()


  1. write mcp client usig stdio
## test_client.py
from mcp import ClientSession, StdioServerParameters, types
from mcp.client.stdio import stdio_client

# Create server parameters for stdio connection
server_params = StdioServerParameters(
    command="python",  # Executable
    args=["test_mcp.py"],  # Optional command line arguments
    env=None,  # Optional environment variables
)


async def run():
    async with stdio_client(server_params) as (read, write):
        async with ClientSession(
            read, write
        ) as session:
            # Initialize the connection
            await session.initialize()
            resources = await session.list_resources()
            # List available tools
            tools = await session.list_tools()
            print(tools)
            result = await session.call_tool(
                "sum", 
            )
            print(result)
asyncio.run(run())
  1. test running
$ python test_client.py
[05/27/25 15:42:13] INFO     Processing request of type ListResourcesRequest                             server.py:551
                    INFO     Processing request of type ListToolsRequest                                 server.py:551
meta=None nextCursor=None tools=[Tool(name='sum', description='count number', inputSchema={'properties': {}, 'title': 'sumArguments', 'type': 'object'}, annotations=None)]
                    INFO     Processing request of type CallToolRequest                                  server.py:551

The program is stuck here and won't continue running

Version

Name: mcp
Version: 1.9.0
Summary: Model Context Protocol SDK
Home-page: 
Author: Anthropic, PBC.
Author-email: 
License: MIT
Location: /data/software/micromamba/envs/gbi/lib/python3.10/site-packages
Requires: anyio, httpx, httpx-sse, pydantic, pydantic-settings, python-multipart, sse-starlette, starlette, uvicorn
Required-by: camel-ai, cellrank-mcp, fast-agent-mcp, fastmcp, liana-mcp, mcp-server-calculator, scmcp, scmcp-utils, scmcp_shared

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず test_mcp.py とともに test_client.py を実行し、CallToolRequest がログに記録された後にハングする状態を再現します。sum ツール内の multiprocessing プールと stdio クライアント/サーバーのエントリポイント間の相互作用を追跡します。例が完了し、停止したままにならずにツールの結果を返せば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。