modelcontextprotocol / modelcontextprotocol/python-sdk

FastMCP server with SSE transport fails to shut down on a signal

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

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

bug P3 ready for work
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ả

A very simple server fails to shut down on a signal if it processed at least one request:

❯ python test-server.py --port=8085
INFO:     Started server process [216035]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8085 (Press CTRL+C to quit)
INFO:     127.0.0.1:55188 - "GET /sse HTTP/1.1" 200 OK
INFO:     127.0.0.1:55192 - "POST /messages/?session_id=e690de9733914d09aebf2b0e8c78191c HTTP/1.1" 202 Accepted
INFO:     127.0.0.1:55192 - "POST /messages/?session_id=e690de9733914d09aebf2b0e8c78191c HTTP/1.1" 202 Accepted
INFO:     127.0.0.1:55192 - "POST /messages/?session_id=e690de9733914d09aebf2b0e8c78191c HTTP/1.1" 202 Accepted
Processing request of type CallToolRequest
^CINFO:     Shutting down
INFO:     Waiting for background tasks to complete. (CTRL+C to force quit)

The expected behaviour would be:

❯ python test-server.py --port=8085
INFO:     Started server process [216006]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8085 (Press CTRL+C to quit)
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [216006]

Here is the code for both the server and the client.

What do I miss?

the server

import click
import sys
from pydantic import Field
from mcp.server.fastmcp import FastMCP

@click.command()
@click.option("--port", default=8085, help="Port to listen", type=int)
def main(port: int):
    mcp = FastMCP(
        "mcp-echo-tool",
        debug=True,
        log_level="INFO",
        port=port,
    )

    @mcp.tool(name="echo")
    async def echo_tool(string: str = Field(description="A string to echo back")) -> str:
        """Echoes back the input string"""
        return string

    mcp.run(transport='sse')
    return 0

if __name__ == "__main__":
    sys.exit(main())

and the client

import asyncio
import click
import sys
from mcp.client.session import ClientSession
from mcp.client.sse import sse_client

async def client(url, string):
    async with sse_client(url) as (read, write):
        async with ClientSession(read, write) as session:
            await session.initialize()
            result = await session.call_tool("echo", {"string": string})
            print(result.content)

@click.command()
@click.option('--url', default='http://localhost:8085', help='MCP Server URL', type=str)
@click.argument('string', type=str)
def main(url: str, string: str):
    asyncio.run(client(url, string))

if __name__ == "__main__":
    sys.exit(main())

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

Tái hiện hành vi shutdown bằng server và client FastMCP SSE được cung cấp, trước tiên xác nhận rằng một request đã được xử lý rồi mới gửi signal. Sau đó, truy vết vòng đời của FastMCP SSE server và đường dẫn shutdown của Uvicorn; được xem là hoàn tất khi process đi đến application shutdown và kết thúc mà không cần signal thứ hai.

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
api, backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
48/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.