stdio_server: text a tool prints still reaches the client, because sys.stdout is flushed after fd 1 is restored
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 76/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- python
- Bereich
- backend-api-design
Rechercherichtung
Beginne damit, die Implementierung von stdio_server() zu lesen und nachzuverfolgen, wo fd 1 umgeleitet und wiederhergestellt wird; das Issue nennt weder eine Datei noch einen Test. Reproduziere den gemeldeten Fall mit einem Tool, das print() aufruft, während stdout weitergeleitet wird, und prüfe dann, dass ausstehender Text auf stderr ausgegeben wird, bevor fd 1 wiederhergestellt wird, und nicht als JSON-RPC-Nachricht erscheint.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
Since #3117, stdio_server() points fd 1 at stderr while it serves, so that stray output from handlers misses the wire. Text written with print() still reaches the client. It goes into sys.stdout's own buffer, which is block-buffered when stdout is a pipe. That buffer is flushed when the interpreter exits, after stdio_server() has given fd 1 back to the client's pipe. The client then reads the text as a JSON-RPC message.
Reproduction
mcp 2.3.0, Python 3.13.16, macOS 26.3.
import asyncio
import sys
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
from mcp.server.mcpserver import MCPServer
if sys.argv[1:] == ["server"]:
server = MCPServer("repro")
@server.tool()
def noisy() -> str:
print("stray output from a tool") # e.g. a library that prints
return "done"
server.run("stdio")
else:
async def main():
params = StdioServerParameters(command=sys.executable, args=[__file__, "server"])
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
result = await session.call_tool("noisy", {})
print("result:", result.content[0].text)
asyncio.run(main())
Running python repro.py, the call succeeds, and the client also logs:
pydantic_core._pydantic_core.ValidationError: 1 validation error for union[JSONRPCRequest,JSONRPCNotification,JSONRPCResponse,JSONRPCError]
Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='stray output from a tool', input_type=str]
Cause
While fd 1 is diverted, the printed text never reaches it: it sits in sys.stdout's buffer. The buffer is written out at interpreter exit, when fd 1 is the protocol pipe again. Adding sys.stdout.flush() right after the print() in the tool removes the error, and the text appears on stderr, which confirms this.
Suggestion
Flush sys.stdout before restoring fd 1, so that pending text goes to the diversion. Making sys.stdout line-buffered while serving (sys.stdout.reconfigure(line_buffering=True)) would also send printed text to stderr when it is printed, rather than at exit.
As a workaround we call sys.stdout.reconfigure(line_buffering=True) before server.run("stdio") and flush after each tool.
- Vorherrschende Sprache
- Python
- Sterne
- 24.7k
- Forks
- 4.1k
- Ø Merge
- 9 Std. 45 Min.
- Gemergte PRs (30 T.)
- 16
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus modelcontextprotocol/python-sdk
-
bug v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
modelcontextprotocol/python-sdk#3811 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
modelcontextprotocol/python-sdk#3810 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
modelcontextprotocol/python-sdk#3781 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug]: Inconsistent structured output wrapping and opt-out for typing.Dict and typing sequence generics in func_metadataEvtl. wieder frei Ein Pull Request dazu wurde ohne Merge geschlossen. Offenbug v1 v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
modelcontextprotocol/python-sdk#3771 · 3 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug v2
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
modelcontextprotocol/python-sdk#3769 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in modelcontextprotocol/python-sdk
Ähnliche Issues
-
follow-up needs-triage S3: minor
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
manaflow-ai/cmux#36856 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
documentation good first issue help wanted
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 90/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 73/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
Shell framework build fails at "copy the shader libraries": the iOS shader libraries are now one file b-<chain>.imgEvtl. vergeben @xiaofeng-han hat das heute übernommen. Offenbuild ios
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag