π The fast, Pythonic way to build MCP servers and clients.
Read +json OpenAPI resource responses as JSON text (#5310)
Asem ahmedfbae4169ecb4
diff --git a/fastmcp_slim/fastmcp/server/providers/openapi/components.py b/fastmcp_slim/fastmcp/server/providers/openapi/components.pyindex 5f0771a37..bd25d5bc7 100644--- a/fastmcp_slim/fastmcp/server/providers/openapi/components.py+++ b/fastmcp_slim/fastmcp/server/providers/openapi/components.py@@ -327,13 +327,15 @@ class OpenAPIResource(Resource):_raise_for_status(response)content_type = response.headers.get("content-type", "").lower()+ media_type = content_type.split(";")[0].strip()- if "application/json" in content_type:+ # RFC 6839: a "+json" suffix marks a JSON media type+ if media_type == "application/json" or media_type.endswith("+json"):result = response.json()return ResourceResult(contents=[ResourceContent(- content=json.dumps(result), mime_type="application/json"+ content=json.dumps(result), mime_type=media_type)])diff --git a/tests/server/providers/openapi/test_openapi_features.py b/tests/server/providers/openapi/test_openapi_features.pyindex 35ddc4aa0..9ec9920c7 100644--- a/tests/server/providers/openapi/test_openapi_features.py+++ b/tests/server/providers/openapi/test_openapi_features.py@@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, Mockimport httpx2import pytestfrom httpx2 import Response+from mcp_types import TextResourceContentsfrom fastmcp import FastMCPfrom fastmcp.client import Client@@ -1091,6 +1092,55 @@ class TestResourceMimeType:assert len(resources) == 1assert resources[0].mime_type == "text/plain"+ async def test_resource_plus_json_response_is_text(self):+ """A +json response is read as JSON text, not a binary blob."""+ spec = {+ "openapi": "3.0.0",+ "info": {"title": "Status API", "version": "1.0.0"},+ "servers": [{"url": "https://api.example.com"}],+ "paths": {+ "/status": {+ "get": {+ "operationId": "get_status",+ "responses": {+ "200": {+ "description": "Status",+ "content": {+ "application/hal+json": {+ "schema": {"type": "object"}+ }+ },+ }+ },+ }+ }+ },+ }++ def handler(request: httpx2.Request) -> httpx2.Response:+ return httpx2.Response(+ 200,+ content=b'{"ok": true}',+ headers={"content-type": "application/hal+json; charset=utf-8"},+ )++ route_maps = [RouteMap(methods=["GET"], mcp_type=MCPType.RESOURCE)]+ async with httpx2.AsyncClient(+ base_url="https://api.example.com",+ transport=httpx2.MockTransport(handler),+ ) as client:+ provider = OpenAPIProvider(+ openapi_spec=spec, client=client, route_maps=route_maps+ )+ mcp = FastMCP("Test")+ mcp.add_provider(provider)+ async with Client(mcp) as mcp_client:+ [content] = await mcp_client.read_resource("resource://get_status")++ assert isinstance(content, TextResourceContents)+ assert json.loads(content.text) == {"ok": True}+ assert content.mime_type == "application/hal+json"+class TestValidateOutput:"""Tests for the validate_output option on OpenAPIProvider."""