modelcontextprotocol / modelcontextprotocol/python-sdk

STDIO hangs forever when the using multiprocessing in tools

Đang mở
#817 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

enhancement needs decision P3
Ngôn ngữ chính
Python
Star
24.3k
Fork
4k
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
31

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách chạy test_client.py cùng với test_mcp.py và tái hiện trạng thái bị treo sau khi CallToolRequest được ghi log. Theo dõi tương tác giữa pool multiprocessing trong công cụ sum và các entry point của client/server stdio. Hoàn tất khi ví dụ chạy xong và trả về kết quả của công cụ thay vì tiếp tục bị treo.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.