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...")
|
print("Generating mock map HTML from resource...")
|
||||||
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
||||||
resource_result = await resource_handler(99999)
|
html_text = await resource_handler(99999)
|
||||||
html_text = resource_result.contents[0].text
|
|
||||||
|
|
||||||
if html_text:
|
if html_text:
|
||||||
output_path = os.path.join(os.path.dirname(__file__), "..", "test.html")
|
output_path = os.path.join(os.path.dirname(__file__), "..", "test.html")
|
||||||
@@ -176,8 +175,7 @@ async def generate():
|
|||||||
|
|
||||||
print("Generating map HTML from resource...")
|
print("Generating map HTML from resource...")
|
||||||
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
||||||
resource_result = await resource_handler(activity_id)
|
html_text = await resource_handler(activity_id)
|
||||||
html_text = resource_result.contents[0].text
|
|
||||||
|
|
||||||
if html_text:
|
if html_text:
|
||||||
output_path = os.path.join(os.path.dirname(__file__), "..", "test.html")
|
output_path = os.path.join(os.path.dirname(__file__), "..", "test.html")
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ from mcp.types import (
|
|||||||
EmbeddedResource,
|
EmbeddedResource,
|
||||||
TextResourceContents,
|
TextResourceContents,
|
||||||
CallToolResult,
|
CallToolResult,
|
||||||
ReadResourceResult,
|
|
||||||
)
|
)
|
||||||
from strava_mcp_server.strava_client import StravaClient
|
from strava_mcp_server.strava_client import StravaClient
|
||||||
from strava_mcp_server.utils import (
|
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")
|
@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 map resource for a specific Strava activity."""
|
||||||
html_content = await _render_activity_map_html(activity_id)
|
return await _render_activity_map_html(activity_id)
|
||||||
return ReadResourceResult(
|
|
||||||
contents=[
|
|
||||||
TextResourceContents(
|
|
||||||
uri=f"strava://activity/{activity_id}/map",
|
|
||||||
mimeType="text/html",
|
|
||||||
text=html_content,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
@mcp.resource("strava://activity/last/map", mime_type="text/html")
|
@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."""
|
"""HTML map resource for the most recent Strava activity."""
|
||||||
try:
|
try:
|
||||||
activities = await strava.list_activities(limit=1)
|
activities = await strava.list_activities(limit=1)
|
||||||
if not activities:
|
if not activities:
|
||||||
return ReadResourceResult(
|
return "<html><body><p>Keine Strava-Aktivitäten gefunden.</p></body></html>"
|
||||||
contents=[
|
|
||||||
TextResourceContents(
|
|
||||||
uri="strava://activity/last/map",
|
|
||||||
mimeType="text/html",
|
|
||||||
text="<html><body><p>Keine Strava-Aktivitäten gefunden.</p></body></html>",
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
last_id = activities[0]["id"]
|
last_id = activities[0]["id"]
|
||||||
html_content = await _render_activity_map_html(last_id)
|
return await _render_activity_map_html(last_id)
|
||||||
return ReadResourceResult(
|
|
||||||
contents=[
|
|
||||||
TextResourceContents(
|
|
||||||
uri="strava://activity/last/map",
|
|
||||||
mimeType="text/html",
|
|
||||||
text=html_content,
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return ReadResourceResult(
|
return f"<html><body><p>Error loading last activity: {str(e)}</p></body></html>"
|
||||||
contents=[
|
|
||||||
TextResourceContents(
|
|
||||||
uri="strava://activity/last/map",
|
|
||||||
mimeType="text/html",
|
|
||||||
text=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
|
# Now fetch and test the actual HTML from the registered resource handler
|
||||||
assert "strava://activity/{activity_id}/map" in mcp.resources
|
assert "strava://activity/{activity_id}/map" in mcp.resources
|
||||||
resource_handler = mcp.resources["strava://activity/{activity_id}/map"]
|
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
|
assert isinstance(html_text, str)
|
||||||
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 "<!DOCTYPE html>" in html_text
|
assert "<!DOCTYPE html>" in html_text
|
||||||
assert "unpkg.com/leaflet" in html_text
|
assert "unpkg.com/leaflet" in html_text
|
||||||
assert "latlngData" in html_text
|
assert "latlngData" in html_text
|
||||||
|
|||||||
Reference in New Issue
Block a user