HttpResource silently decodes PNG response bytes into lossy TextResourceContents
Maintainer antworten meist innerhalb von 1 Tag
@vanshaj1999 arbeitet bereits daran.
Seit 09.11.2026.
- #3910 von @EnesKartal — ohne Merge geschlossen
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 72/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- python
- Bereich
- backend-api-design
Rechercherichtung
Beginne mit HttpResource.read() in src/mcp/server/mcpserver/resources/types.py, wo laut Issue nach raise_for_status() response.text zurückgegeben wird. Verfolge, wie dieser Wert zu MCP-Text oder Blob-Inhalt wird, und vergleiche das anschließend mit BinaryResource und dem Reproducer im Issue. Fertig ist die Arbeit, wenn die HTTP-PNG-Bytes als Blob-Inhalt erhalten bleiben oder nicht unterstützte Binärantworten mit einer klaren Fehlermeldung fehlschlagen; prüfe, dass UTF-8-JSON-Ressourcen weiterhin funktionieren.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Initial Checks
- I confirm that I'm using the newest release of my line (the latest 2.x, or the latest 1.x if I'm still on v1)
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Release line
2.x (current stable)
Description
While checking HTTP-hosted figures for MCP resource delivery, I found that HttpResource silently converts a binary HTTP response into lossy text, even when both the resource and the HTTP response declare image/png.
A valid 69-byte PNG served with HTTP 200 reaches Client(server).read_resource() as TextResourceContents containing U+FFFD replacement characters. Re-encoding that string as UTF-8 produces 95 bytes, so the original image cannot be recovered. The MIME label remains image/png and the read reports success.
The same bytes served through BinaryResource arrive as BlobResourceContents and decode back to the original 69 bytes. A separate HTTP JSON resource containing UTF-8 Japanese text is also preserved correctly.
Expected: preserve the binary response bytes and return blob content. If binary HTTP resources are intentionally unsupported, reject them clearly rather than returning success after lossy text conversion.
The apparent cause is HttpResource.read() returning response.text unconditionally after raise_for_status() in src/mcp/server/mcpserver/resources/types.py. This happens before MCP's text/blob conversion. It is separate from the old base64-encoding issue #342.
The reproducer below uses a real loopback HTTP server and the public MCP client API, without mocks or SDK patches. It requires only mcp and its dependencies. The embedded image is a valid 1x1 RGB PNG; its chunk CRCs and decompressed pixel data were independently checked.
Observed output, omitting HTTP request logging:
image://control BlobResourceContents True
image://remote TextResourceContents False
AI assistance was used to investigate this behavior, create and execute the reproducer, and draft this report.
Example Code
import base64
import anyio
from anyio.abc import SocketAttribute, SocketStream
from mcp_types import BlobResourceContents, TextResourceContents
from mcp import Client
from mcp.server.mcpserver import MCPServer
from mcp.server.mcpserver.resources import BinaryResource, HttpResource
PNG = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC")
async def http_peer(stream: SocketStream) -> None:
request = b""
while b"\r\n\r\n" not in request:
request += await stream.receive(4096)
assert request.startswith(b"GET /figure.png ")
headers = f"HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {len(PNG)}\r\nConnection: close\r\n\r\n"
await stream.send(headers.encode("ascii") + PNG)
await stream.aclose()
async def main() -> None:
listener = await anyio.create_tcp_listener(local_host="127.0.0.1", local_port=0)
port = listener.extra(SocketAttribute.local_address)[1]
server = MCPServer()
server.add_resource(BinaryResource(uri="image://control", data=PNG, mime_type="image/png"))
server.add_resource(
HttpResource(uri="image://remote", url=f"http://127.0.0.1:{port}/figure.png", mime_type="image/png")
)
async with listener, anyio.create_task_group() as group:
group.start_soon(listener.serve, http_peer)
with anyio.fail_after(5):
async with Client(server) as client:
for uri in ("image://control", "image://remote"):
result = await client.read_resource(uri)
content = result.contents[0]
if isinstance(content, BlobResourceContents):
received = base64.b64decode(content.blob)
else:
assert isinstance(content, TextResourceContents)
received = content.text.encode("utf-8")
print(uri, type(content).__name__, received == PNG)
group.cancel_scope.cancel()
anyio.run(main)
Python & MCP Python SDK
CPython 3.12.14; Linux x86_64, glibc 2.39
Released wheel:
mcp 2.3.0
mcp-types 2.3.0
httpx2 2.16.0
pydantic 2.14.3
anyio 4.16.0
Also reproduced on main:
77001c96f9507be0f4d009b83210a384a5e08bda
Repository frozen dependencies:
httpx2 2.12.0
pydantic 2.12.5
anyio 4.14.2
- Vorherrschende Sprache
- Python
- Sterne
- 25k
- Forks
- 4.2k
- Ø Merge
- 10 Min.
- Gemergte PRs (30 T.)
- 1
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
-
v2
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 86/100
modelcontextprotocol/python-sdk#3978 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Client serves cached tools after its context exits with an exceptionEvtl. vergeben @jonxsong hat das vor 1 Tag übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
modelcontextprotocol/python-sdk#3968 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 85/100
modelcontextprotocol/python-sdk#3957 ·
Maintainer antworten meist innerhalb von 1 Tag
-
v2
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 91/100
modelcontextprotocol/python-sdk#3934 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug v1
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
modelcontextprotocol/python-sdk#3929 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in modelcontextprotocol/python-sdk
Ähnliche Issues
-
auto-reported bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
Maintainer antworten meist innerhalb von 1 Tag
-
stix-atlas.json: the same relationship id is used for several different mitigates relationshipsOffen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
backend bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
nolte/kamerplanter#3193 ·
Maintainer antworten meist innerhalb von 1 Tag
-
soup prune-prompt exits 1 after writing its output on a file over 100,000 rows, and reports a capped row count otherwiseEvtl. vergeben @abdulwaarith0 hat das heute übernommen. Offenneeds-triage
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
MakazhanAlpamys/Soup#3690 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag