modelcontextprotocol / modelcontextprotocol/python-sdk

STDIO hangs forever when the using multiprocessing in tools

未關閉
#817 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

enhancement needs decision P3
主要語言
Python
星號
24.3k
分支
4k
平均合併
1 天 1 小時
30 天內合併 PR
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

首先執行 test_client.py 與 test_mcp.py,並在 CallToolRequest 被記錄後重現卡住的問題。追蹤 sum 工具中的 multiprocessing pool 與 stdio 用戶端/伺服器進入點之間的互動。完成的標準是範例能夠結束並回傳工具結果,而不是一直卡住。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
4/5
預估耗時
3-5 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。