Title: Issue · GitHub
Open Graph Title: Issue · modelcontextprotocol/python-sdk
X Title: Issue · modelcontextprotocol/python-sdk
Description: The official Python SDK for Model Context Protocol servers and clients - Issue · modelcontextprotocol/python-sdk
Open Graph Description: The official Python SDK for Model Context Protocol servers and clients - Issue · modelcontextprotocol/python-sdk
X Description: The official Python SDK for Model Context Protocol servers and clients - Issue · modelcontextprotocol/python-sdk
Opengraph URL: https://github.com/modelcontextprotocol/python-sdk
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"support logging to stderr in Jupyter Notebook Environments.","articleBody":"**Is your feature request related to a problem? Please describe.**\nI 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.\n\nFor instance in jupyter notebook:\n\n```python\nimport mcp\nimport os\nfrom mcp.client.stdio import stdio_client\n\nserverparams = mcp.StdioServerParameters(\n command=\"uv\",\n args=[\"--quiet\", \"run\", \"../src/echo.py\"],\n env={\"UV_PYTHON\": \"3.12\", **os.environ},\n)\n\nasync with stdio_client(serverparams) as (read, write):\n async with mcp.ClientSession(read, write) as session:\n await session.initialize()\n tools = await session.list_tools()\n print(tools)\n```\nOutputs:\n```\nmeta=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'})]\n```\n\nwhile running the server without jupyter notebook:\n\n```bash\n❯ uv run --quiet src/echo.py\nstarting echo server\n```\n\nstderr is correctly displayed.\n\nThis 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.\n\n**Describe the solution you'd like**\nI found the culprit being the use of:\n\n```python \nprocess = await anyio.open_process(\n [server.command, *server.args],\n env=server.env if server.env is not None else get_default_environment(),\n stderr=sys.stderr,\n )\n```\nIn particular sys.stderr here is not working in the jupyter / ipython context. Instead I would suggest a working change as follow:\n\n1. remove the stderr params from the process and handle process.stderr in an async function as stdout / stdin is handled.\n2. to that effect, use a `stderr_reader` async function like the following:\n\n```python\n async def stderr_reader():\n assert process.stderr, \"Opened process is missing stderr\"\n try:\n async for line in process.stderr:\n if is_jupyter_notebook():\n print(f\"\\033[91m {line.decode().strip()}\")\n else:\n # redirect to stderr as before\n print(line.decode().strip(), file=sys.stderr)\n except anyio.ClosedResourceError:\n await anyio.lowlevel.checkpoint()\n```\n\nThis would result in the same behavior as before while allowing the stderr to be logged in the jupyter notebook context.\n\n**Additional context**\nJupyter 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.\nhttps://github.com/grll/mcpadapt \n","author":{"url":"https://github.com/grll","@type":"Person","name":"grll"},"datePublished":"2025-01-17T09:09:24.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/156/python-sdk/issues/156"}
| route-pattern | /:user_id/:repository/issues/:id(.:format) |
| route-controller | issues |
| route-action | show |
| fetch-nonce | v2:98e90535-99c6-7602-601c-e92e9bd2b272 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 868C:242DFD:8B64C8:C87206:6A58C728 |
| html-safe-nonce | 2a182316012bd40178e6e1ac8f25f7043baf51d13431aebcd2b1dcd3871aaba7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NjhDOjI0MkRGRDo4QjY0Qzg6Qzg3MjA2OjZBNThDNzI4IiwidmlzaXRvcl9pZCI6IjY4MDczMjYyNzE3NTg0NTI1MjAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 0d7a2b4b9aad0e172291eb7db14d946bc807b13584bbbf548ad6289e0fe0e645 |
| hovercard-subject-tag | repository:862584018 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/modelcontextprotocol/python-sdk/issues/156 |
| twitter:image | https://opengraph.githubassets.com/843d1c8b211f1178ef432e19b617ca5096e04778dbb0df08903a2f377b3f3d34/modelcontextprotocol/python-sdk |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/843d1c8b211f1178ef432e19b617ca5096e04778dbb0df08903a2f377b3f3d34/modelcontextprotocol/python-sdk |
| og:image:alt | The official Python SDK for Model Context Protocol servers and clients - Issue · modelcontextprotocol/python-sdk |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| turbo-cache-control | no-cache |
| go-import | github.com/modelcontextprotocol/python-sdk git https://github.com/modelcontextprotocol/python-sdk.git |
| octolytics-dimension-user_id | 182288589 |
| octolytics-dimension-user_login | modelcontextprotocol |
| octolytics-dimension-repository_id | 862584018 |
| octolytics-dimension-repository_nwo | modelcontextprotocol/python-sdk |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 862584018 |
| octolytics-dimension-repository_network_root_nwo | modelcontextprotocol/python-sdk |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width