modelcontextprotocol / modelcontextprotocol/python-sdk

support logging to stderr in Jupyter Notebook Environments.

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

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

bug good first issue P3 ready for work
主要言語
Python
スター
24.3k
フォーク
4k
平均マージ
1日 1時間
マージ済み PR(30日)
31

説明

Is your feature request related to a problem? Please describe.
I want to make mcp python-sdk jupyter notebook compatible. When running in a notebook environment, MCP work but do not output stderr as it does normally.

For instance in jupyter notebook:

import mcp
import os
from mcp.client.stdio import stdio_client

serverparams = mcp.StdioServerParameters(
    command="uv",
    args=["--quiet", "run", "../src/echo.py"],
    env={"UV_PYTHON": "3.12", **os.environ},
)

async with stdio_client(serverparams) as (read, write):
    async with mcp.ClientSession(read, write) as session:
        await session.initialize()
        tools = await session.list_tools()
        print(tools)

Outputs:

meta=None nextCursor=None tools=[Tool(name='echo_tool', description='Echo the input text\n\n    Args:\n        text (str): The text to echo\n\n    Returns:\n        str: The echoed text\n    ', inputSchema={'properties': {'text': {'title': 'Text', 'type': 'string'}}, 'required': ['text'], 'title': 'echo_toolArguments', 'type': 'object'})]

while running the server without jupyter notebook:

❯ uv run --quiet src/echo.py
starting echo server

stderr is correctly displayed.

This is a big problem mostly because if the server is crashing or the command is wrong you have no way to know what's wrong: nothing is logged and the jupyter notebook cell just hangs.

Describe the solution you'd like
I found the culprit being the use of:

process = await anyio.open_process(
        [server.command, *server.args],
        env=server.env if server.env is not None else get_default_environment(),
        stderr=sys.stderr,
 )

In particular sys.stderr here is not working in the jupyter / ipython context. Instead I would suggest a working change as follow:

  1. remove the stderr params from the process and handle process.stderr in an async function as stdout / stdin is handled.
  2. to that effect, use a stderr_reader async function like the following:
    async def stderr_reader():
        assert process.stderr, "Opened process is missing stderr"
        try:
            async for line in process.stderr:
                if is_jupyter_notebook():
                    print(f"\033[91m {line.decode().strip()}")
                else:
                    # redirect to stderr as before
                    print(line.decode().strip(), file=sys.stderr)
        except anyio.ClosedResourceError:
            await anyio.lowlevel.checkpoint()

This would result in the same behavior as before while allowing the stderr to be logged in the jupyter notebook context.

Additional context
Jupyter notebook support support is also requested for mcpadapt which bring MCP server tools in any agentic framework, as many agentic framework demonstrate usage in jupyter notebooks.
https://github.com/grll/mcpadapt

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

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

はじめの一歩

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

調査の方向性

stdio_client とその anyio.open_process 呼び出しから始め、既存の stdout と stdin の処理が process.stderr とどのように連携しているかを比較します。完了条件は、Jupyter または IPython のノートブックでサーバーエラーが表示され、プロセスが失敗した場合やコマンドが無効な場合も含め、それ以外では stderr が通常どおり動作することです。

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

評価

技術スタック
jupyter-notebook, python
領域
backend, cli, developer-experience
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
58/100

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

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