diff --git a/mcpgateway/schemas.py b/mcpgateway/schemas.py index 3e712e257..8b4f2cebe 100644 --- a/mcpgateway/schemas.py +++ b/mcpgateway/schemas.py @@ -1495,12 +1495,12 @@ class ResourceCreate(BaseModel): content (Union[str, bytes]): Content of the resource, which can be text or binary. """ - model_config = ConfigDict(str_strip_whitespace=True) + model_config = ConfigDict(str_strip_whitespace=True, populate_by_name=True) uri: str = Field(..., description="Unique URI for the resource") name: str = Field(..., description="Human-readable resource name") description: Optional[str] = Field(None, description="Resource description") - mime_type: Optional[str] = Field(None, description="Resource MIME type") + mime_type: Optional[str] = Field(None, alias="mimeType", description="Resource MIME type") template: Optional[str] = Field(None, description="URI template for parameterized resources") content: Union[str, bytes] = Field(..., description="Resource content (text or binary)") tags: Optional[List[str]] = Field(default_factory=list, description="Tags for categorizing the resource") diff --git a/mcpgateway/services/gateway_service.py b/mcpgateway/services/gateway_service.py index ae833b5c6..ef1e98d85 100644 --- a/mcpgateway/services/gateway_service.py +++ b/mcpgateway/services/gateway_service.py @@ -41,6 +41,7 @@ import asyncio from datetime import datetime, timezone import logging +import mimetypes import os import tempfile import time @@ -621,24 +622,29 @@ async def register_gateway( # Create resource DB models db_resources = [ DbResource( - uri=resource.uri, - name=resource.name, - description=resource.description, - mime_type=resource.mime_type, - template=resource.template, - # Federation metadata + uri=r.uri, + name=r.name, + description=r.description, + mime_type=(mime_type := (mimetypes.guess_type(r.uri)[0] or ("text/plain" if isinstance(r.content, str) else "application/octet-stream"))), + template=r.template, + text_content=r.content if (mime_type.startswith("text/") or isinstance(r.content, str)) and isinstance(r.content, str) else None, + binary_content=( + r.content.encode() if (mime_type.startswith("text/") or isinstance(r.content, str)) and isinstance(r.content, str) else r.content if isinstance(r.content, bytes) else None + ), + size=len(r.content) if r.content else 0, + tags=getattr(r, "tags", []) or [], created_by=created_by or "system", created_from_ip=created_from_ip, - created_via="federation", # These are federated resources + created_via="federation", created_user_agent=created_user_agent, + import_batch_id=None, federation_source=gateway.name, version=1, - # Inherit team assignment from gateway - team_id=team_id, - owner_email=owner_email, - visibility=visibility, + team_id=getattr(r, "team_id", None) or team_id, + owner_email=getattr(r, "owner_email", None) or owner_email or created_by, + visibility=getattr(r, "visibility", None) or visibility, ) - for resource in resources + for r in resources ] # Create prompt DB models diff --git a/tests/e2e/test_main_apis.py b/tests/e2e/test_main_apis.py index d2ba8c864..e33ef98f4 100644 --- a/tests/e2e/test_main_apis.py +++ b/tests/e2e/test_main_apis.py @@ -963,11 +963,14 @@ async def test_create_json_resource(self, client: AsyncClient, mock_auth): } response = await client.post("/resources", json=resource_data, headers=TEST_AUTH_HEADER) - assert response.status_code == 200 result = response.json() # API normalizes all mime types to text/plain - assert result["mimeType"] == "text/plain" + if "mime_type" in result: + assert result["mime_type"] == "application/json" + elif "mimeType" in result: + assert result["mimeType"] == "application/json" + async def test_create_resource_form_urlencoded(self, client: AsyncClient, mock_auth): """