modelcontextprotocol / modelcontextprotocol/python-sdk
support logging to stderr in Jupyter Notebook Environments.
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 24.3k
- 派生
- 4k
- 平均合并
- 1 天 1 小时
- 30 天内合并 PR
- 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:
- remove the stderr params from the process and handle process.stderr in an async function as stdout / stdin is handled.
- to that effect, use a
stderr_readerasync 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 stdio_client 及其对 anyio.open_process 的调用开始,然后比较现有的 stdout 和 stdin 处理方式与 process.stderr 的工作方式。完成的标准是:服务器错误能够显示在 Jupyter 或 IPython notebook 中,同时 stderr 在其他情况下保持正常行为,包括进程失败或命令无效时。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- jupyter-notebook, python
- 领域
- backend, cli, developer-experience
- Issue 类型
- 功能
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 活跃
- 描述清晰度
- 基本清楚
- 新手友好度
- 58/100