mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-24 06:24:18 +02:00
Fix issues with simple str response
This commit is contained in:
parent
63d8b1b723
commit
57cb110338
2 changed files with 7 additions and 49 deletions
|
|
@ -301,7 +301,8 @@ class OpenAPIResource(Resource):
|
|||
content_type = response.headers.get("content-type", "").lower()
|
||||
|
||||
if "application/json" in content_type:
|
||||
return str(response.json())
|
||||
result = response.json()
|
||||
return json.dumps(result)
|
||||
elif any(ct in content_type for ct in ["text/", "application/xml"]):
|
||||
return response.text
|
||||
else:
|
||||
|
|
@ -343,56 +344,13 @@ class OpenAPIResourceTemplate(ResourceTemplate):
|
|||
uri_template=uri_template,
|
||||
name=name,
|
||||
description=description,
|
||||
fn=self._create_resource_fn,
|
||||
fn=lambda **kwargs: None,
|
||||
parameters=parameters,
|
||||
tags=tags,
|
||||
)
|
||||
self._client = client
|
||||
self._route = route
|
||||
|
||||
async def _create_resource_fn(self, **kwargs):
|
||||
"""Create a resource with parameters."""
|
||||
# Prepare the path with parameters
|
||||
path = self._route.path
|
||||
for param_name, param_value in kwargs.items():
|
||||
path = path.replace(f"{{{param_name}}}", str(param_value))
|
||||
|
||||
try:
|
||||
response = await self._client.request(
|
||||
method=self._route.method,
|
||||
url=path,
|
||||
timeout=30.0, # Default timeout
|
||||
)
|
||||
|
||||
# Raise for 4xx/5xx responses
|
||||
response.raise_for_status()
|
||||
|
||||
# Determine content type and return appropriate format
|
||||
content_type = response.headers.get("content-type", "").lower()
|
||||
|
||||
if "application/json" in content_type:
|
||||
return str(response.json())
|
||||
elif any(ct in content_type for ct in ["text/", "application/xml"]):
|
||||
return response.text
|
||||
else:
|
||||
return response.content
|
||||
|
||||
except httpx.HTTPStatusError as e:
|
||||
error_message = (
|
||||
f"HTTP error {e.response.status_code}: {e.response.reason_phrase}"
|
||||
)
|
||||
try:
|
||||
error_data = e.response.json()
|
||||
error_message += f" - {error_data}"
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
if e.response.text:
|
||||
error_message += f" - {e.response.text}"
|
||||
|
||||
raise ValueError(error_message)
|
||||
|
||||
except httpx.RequestError as e:
|
||||
raise ValueError(f"Request error: {str(e)}")
|
||||
|
||||
async def create_resource(self, uri: str, params: dict[str, Any]) -> Resource:
|
||||
"""Create a resource with the given parameters."""
|
||||
# Generate a URI for this resource instance
|
||||
|
|
@ -406,9 +364,8 @@ class OpenAPIResourceTemplate(ResourceTemplate):
|
|||
route=self._route,
|
||||
uri=uri,
|
||||
name=f"{self.name}-{'-'.join(uri_parts)}",
|
||||
description=self.description
|
||||
or f"Resource for {self._route.path}", # Provide default if None
|
||||
mime_type="application/json", # Default, will be updated when read
|
||||
description=self.description or f"Resource for {self._route.path}",
|
||||
mime_type="application/json",
|
||||
tags=set(self._route.tags or []),
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import httpx
|
|||
import pytest
|
||||
from dirty_equals import IsStr
|
||||
from fastapi import FastAPI, HTTPException, Response
|
||||
from fastapi.responses import PlainTextResponse
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from mcp.types import BlobResourceContents, TextContent, TextResourceContents
|
||||
from pydantic import BaseModel, TypeAdapter
|
||||
|
|
@ -67,7 +68,7 @@ def fastapi_app(users_db: dict[int, User]) -> FastAPI:
|
|||
user.name = name
|
||||
return user
|
||||
|
||||
@app.get("/ping")
|
||||
@app.get("/ping", response_class=PlainTextResponse)
|
||||
async def ping() -> str:
|
||||
"""Ping the server."""
|
||||
return "pong"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue