Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/late/mcp/chatgpt_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from late.client.base import _retry_after_seconds
from late.mcp.auth import build_auth_provider, forget_rejected_token
from late.mcp.media_items import media_item
from late.mcp.middleware import WellFormedOutputMiddleware, WellFormedOutputTransform
from late.mcp.middleware.rate_limit import wait_hint
from late.mcp.user_agent import mcp_user_agent
Expand All @@ -47,7 +48,6 @@
"Saturday",
"Sunday",
]
_VIDEO_EXT = (".mp4", ".mov", ".avi", ".webm", ".m4v")
# Everything this surface can do fits these four; ChatGPT asks the user to
# grant every scope the discovery document advertises.
CHATGPT_SCOPES = ["posts:read", "posts:write", "accounts:read", "analytics:read"]
Expand Down Expand Up @@ -449,19 +449,7 @@ async def _usernames(posts: list[dict[str, Any]]) -> dict[str, str]:


def _media_items(media_urls: list[str] | None) -> list[dict[str, str]]:
items = []
for url in media_urls or []:
lower = url.lower().split("?")[0]
if lower.endswith(_VIDEO_EXT):
media_type = "video"
elif lower.endswith(".gif"):
media_type = "gif"
elif lower.endswith(".pdf"):
media_type = "document"
else:
media_type = "image"
items.append({"type": media_type, "url": url})
return items
return [media_item(url) for url in media_urls or []]


def _schedule_fields(scheduled_for: str, tz: str | None) -> dict[str, str]:
Expand Down
18 changes: 18 additions & 0 deletions src/late/mcp/media_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

_VIDEO_EXT = (".mp4", ".mov", ".avi", ".webm", ".m4v")
_IMAGE_EXT = (".jpg", ".jpeg", ".png", ".webp", ".heic", ".heif", ".bmp", ".tiff")


def media_item(url: str) -> dict[str, str]:
path = url.lower().split("?")[0]
if path.endswith(_VIDEO_EXT):
return {"type": "video", "url": url}
if path.endswith(".gif"):
return {"type": "gif", "url": url}
if path.endswith(".pdf"):
return {"type": "document", "url": url}
if path.endswith(_IMAGE_EXT):
return {"type": "image", "url": url}
# Unknown extension (e.g. a Drive link): omit type so the API infers it (Drive links are rehosted first).
return {"url": url}
25 changes: 4 additions & 21 deletions src/late/mcp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
from fastmcp.server.transforms.search.base import serialize_tools_for_output_markdown
from mcp.types import INVALID_PARAMS, ToolAnnotations

from late import Late, MediaType, PostStatus
from late import Late, PostStatus
from late.client.exceptions import LateAPIError, LateError, LateRateLimitError

from .auth import build_auth_provider, forget_rejected_token
from .idempotency import same_arguments_key
from .media_items import media_item
from .middleware import (
FEEDBACK_TOOL,
FeedbackSourceMiddleware,
Expand Down Expand Up @@ -931,16 +932,7 @@ def posts_create(
# Add media items if provided
if media_urls:
urls = [u.strip() for u in media_urls.split(",") if u.strip()]
media_items = []
for url in urls:
media_type: MediaType | str = MediaType.IMAGE
if any(
ext in url.lower() for ext in [".mp4", ".mov", ".avi", ".webm", ".m4v"]
):
media_type = MediaType.VIDEO
elif any(ext in url.lower() for ext in [".gif"]):
media_type = MediaType.GIF
media_items.append({"type": media_type, "url": url})
media_items = [media_item(url) for url in urls]
params["media_items"] = media_items

if is_draft:
Expand Down Expand Up @@ -1042,16 +1034,7 @@ def posts_cross_post(

if media_urls:
urls = [u.strip() for u in media_urls.split(",") if u.strip()]
media_items = []
for url in urls:
media_type: MediaType | str = MediaType.IMAGE
if any(
ext in url.lower() for ext in [".mp4", ".mov", ".avi", ".webm", ".m4v"]
):
media_type = MediaType.VIDEO
elif any(ext in url.lower() for ext in [".gif"]):
media_type = MediaType.GIF
media_items.append({"type": media_type, "url": url})
media_items = [media_item(url) for url in urls]
params["media_items"] = media_items

if is_draft:
Expand Down
21 changes: 20 additions & 1 deletion tests/test_chatgpt_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from fastmcp import Client

from late.mcp import auth, chatgpt_server
from late.mcp.chatgpt_server import chatgpt_mcp
from late.mcp.chatgpt_server import _media_items, chatgpt_mcp

API = chatgpt_server.API_BASE

Expand Down Expand Up @@ -341,3 +341,22 @@ async def test_an_api_401_evicts_the_cached_token_so_chatgpt_gets_a_401_and_refr

assert result.is_error
assert auth.token_cache_key("sk_test") not in auth._VERIFIED_AT


@pytest.mark.parametrize(
("url", "expected"),
[
("https://x.com/a.mp4", {"type": "video", "url": "https://x.com/a.mp4"}),
(
"https://x.com/a.jpg?w=1",
{"type": "image", "url": "https://x.com/a.jpg?w=1"},
),
("https://x.com/a.pdf", {"type": "document", "url": "https://x.com/a.pdf"}),
(
"https://drive.google.com/file/d/abc/view?usp=sharing",
{"url": "https://drive.google.com/file/d/abc/view?usp=sharing"},
),
],
)
def test_media_items_only_type_what_the_extension_proves(url, expected):
assert _media_items([url]) == [expected]
59 changes: 59 additions & 0 deletions tests/test_mcp_media_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from __future__ import annotations

import json

import httpx
import pytest
import respx
from fastmcp import Client

from late.mcp.server import mcp

API = "https://api.test.com"
DRIVE_URL = "https://drive.google.com/file/d/abc/view?usp=sharing"
POST_RESPONSE = {
"post": {"_id": "post_1", "content": "hi", "status": "draft", "platforms": []},
"message": "ok",
}


@pytest.fixture(autouse=True)
def _api_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("ZERNIO_API_KEY", "test_api_key")
monkeypatch.setenv("ZERNIO_BASE_URL", API)


@respx.mock
@pytest.mark.parametrize(
("tool", "arguments"),
[
(
"posts_create",
{"platform": "twitter", "account_id": "acc_1"},
),
(
"posts_cross_post",
{"platforms": "twitter", "account_ids": "acc_1"},
),
],
)
async def test_media_urls_are_typed_only_when_the_extension_proves_it(
tool: str, arguments: dict
) -> None:
create = respx.post(f"{API}/v1/posts").mock(
return_value=httpx.Response(200, json=POST_RESPONSE)
)
media_urls = f"https://x.com/a.mp4,https://x.com/a.jpg,{DRIVE_URL}"

async with Client(mcp) as client:
await client.call_tool(
tool,
{"content": "hi", "is_draft": True, "media_urls": media_urls, **arguments},
)

items = json.loads(create.calls.last.request.content)["mediaItems"]
assert items == [
{"type": "video", "url": "https://x.com/a.mp4"},
{"type": "image", "url": "https://x.com/a.jpg"},
{"url": DRIVE_URL},
]
Loading