fastmcp/tests/server/providers/openapi/test_raw_request_bodies.py
Kunpeng Xie 119339e90f
Preserve OpenAPI raw body content types (#5093)
* Preserve OpenAPI raw body content types

Generated with OpenAI Codex

* Encode raw OpenAPI bodies with their declared charset

Generated with OpenAI Codex.
2026-09-14 14:38:02 -05:00

25 lines
932 B
Python

"""Non-JSON request bodies retain the media type required by the endpoint."""
import httpx2
from fastapi import Body, FastAPI, Request
from fastmcp import Client, FastMCP
async def test_plain_text_body_reaches_http_endpoint() -> None:
app = FastAPI()
@app.post("/echo", operation_id="echo")
def echo(
request: Request, body: str = Body(media_type="text/plain")
) -> dict[str, str | None]:
return {"text": body, "content_type": request.headers.get("content-type")}
async with httpx2.AsyncClient(
base_url="http://test", transport=httpx2.ASGITransport(app=app)
) as http_client:
server = FastMCP.from_openapi(openapi_spec=app.openapi(), client=http_client)
async with Client(server) as client:
result = await client.call_tool("echo", {"body": "hello from a tool"})
assert result.data == {"text": "hello from a tool", "content_type": "text/plain"}