refactor: return str directly from resources to comply with FastMCP design and fix mimeType mismatch
This commit is contained in:
@@ -115,8 +115,7 @@ async def generate():
|
||||
|
||||
print("Generating mock map HTML from resource...")
|
||||
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
||||
resource_result = await resource_handler(99999)
|
||||
html_text = resource_result.contents[0].text
|
||||
html_text = await resource_handler(99999)
|
||||
|
||||
if html_text:
|
||||
output_path = os.path.join(os.path.dirname(__file__), "..", "test.html")
|
||||
@@ -176,8 +175,7 @@ async def generate():
|
||||
|
||||
print("Generating map HTML from resource...")
|
||||
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
||||
resource_result = await resource_handler(activity_id)
|
||||
html_text = resource_result.contents[0].text
|
||||
html_text = await resource_handler(activity_id)
|
||||
|
||||
if html_text:
|
||||
output_path = os.path.join(os.path.dirname(__file__), "..", "test.html")
|
||||
|
||||
@@ -6,7 +6,6 @@ from mcp.types import (
|
||||
EmbeddedResource,
|
||||
TextResourceContents,
|
||||
CallToolResult,
|
||||
ReadResourceResult,
|
||||
)
|
||||
from strava_mcp_server.strava_client import StravaClient
|
||||
from strava_mcp_server.utils import (
|
||||
@@ -902,52 +901,18 @@ def register(mcp: FastMCP, strava: StravaClient) -> None:
|
||||
)
|
||||
|
||||
@mcp.resource("strava://activity/{activity_id}/map", mime_type="text/html")
|
||||
async def get_activity_map_resource(activity_id: int) -> ReadResourceResult:
|
||||
async def get_activity_map_resource(activity_id: int) -> str:
|
||||
"""HTML map resource for a specific Strava activity."""
|
||||
html_content = await _render_activity_map_html(activity_id)
|
||||
return ReadResourceResult(
|
||||
contents=[
|
||||
TextResourceContents(
|
||||
uri=f"strava://activity/{activity_id}/map",
|
||||
mimeType="text/html",
|
||||
text=html_content,
|
||||
)
|
||||
]
|
||||
)
|
||||
return await _render_activity_map_html(activity_id)
|
||||
|
||||
@mcp.resource("strava://activity/last/map", mime_type="text/html")
|
||||
async def get_last_activity_map_resource() -> ReadResourceResult:
|
||||
async def get_last_activity_map_resource() -> str:
|
||||
"""HTML map resource for the most recent Strava activity."""
|
||||
try:
|
||||
activities = await strava.list_activities(limit=1)
|
||||
if not activities:
|
||||
return ReadResourceResult(
|
||||
contents=[
|
||||
TextResourceContents(
|
||||
uri="strava://activity/last/map",
|
||||
mimeType="text/html",
|
||||
text="<html><body><p>Keine Strava-Aktivitäten gefunden.</p></body></html>",
|
||||
)
|
||||
]
|
||||
)
|
||||
return "<html><body><p>Keine Strava-Aktivitäten gefunden.</p></body></html>"
|
||||
last_id = activities[0]["id"]
|
||||
html_content = await _render_activity_map_html(last_id)
|
||||
return ReadResourceResult(
|
||||
contents=[
|
||||
TextResourceContents(
|
||||
uri="strava://activity/last/map",
|
||||
mimeType="text/html",
|
||||
text=html_content,
|
||||
)
|
||||
]
|
||||
)
|
||||
return await _render_activity_map_html(last_id)
|
||||
except Exception as e:
|
||||
return ReadResourceResult(
|
||||
contents=[
|
||||
TextResourceContents(
|
||||
uri="strava://activity/last/map",
|
||||
mimeType="text/html",
|
||||
text=f"<html><body><p>Error loading last activity: {str(e)}</p></body></html>",
|
||||
)
|
||||
]
|
||||
)
|
||||
return f"<html><body><p>Error loading last activity: {str(e)}</p></body></html>"
|
||||
|
||||
@@ -67,14 +67,9 @@ async def test_get_activity_map_success():
|
||||
# Now fetch and test the actual HTML from the registered resource handler
|
||||
assert "strava://activity/{activity_id}/map" in mcp.resources
|
||||
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
||||
resource_result = await resource_handler(12345)
|
||||
html_text = await resource_handler(12345)
|
||||
|
||||
assert len(resource_result.contents) == 1
|
||||
resource_content = resource_result.contents[0]
|
||||
assert str(resource_content.uri) == "strava://activity/12345/map"
|
||||
assert resource_content.mimeType == "text/html"
|
||||
|
||||
html_text = resource_content.text
|
||||
assert isinstance(html_text, str)
|
||||
assert "<!DOCTYPE html>" in html_text
|
||||
assert "unpkg.com/leaflet" in html_text
|
||||
assert "latlngData" in html_text
|
||||
|
||||
Reference in New Issue
Block a user