mirror of
https://github.com/dnlbauer/cordra-mcp.git
synced 2026-09-10 21:55:30 +00:00
Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e6aa73f50 | ||
|
|
a4d12d526b | ||
|
|
ad6dce005b | ||
|
|
d42bb1a581 | ||
|
|
c3b202c998 | ||
|
|
0bee7f2775 | ||
|
|
adc24dd4ad | ||
|
|
99fdaadea6 | ||
|
|
8b0694dff1 | ||
|
|
ea9cf92b2c | ||
|
|
3c8367f804 | ||
|
|
0a9ba538ef | ||
|
|
eaa300b8c2 | ||
|
|
7b77186ced | ||
|
|
5451883739 | ||
|
|
7d74d8d9af | ||
|
|
f6bfb14b29 | ||
|
|
9c9594367c |
@@ -1,5 +1,7 @@
|
||||
FROM python:3.13-alpine
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
# Install package manager
|
||||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/
|
||||
|
||||
@@ -18,4 +20,6 @@ COPY src src
|
||||
RUN uv sync \
|
||||
--locked
|
||||
|
||||
ENV CORDRA_RUN_MODE=http
|
||||
|
||||
CMD ["uv", "run", "cordra-mcp"]
|
||||
|
||||
48
README.md
48
README.md
@@ -18,32 +18,60 @@ ensuring safe exploration without risk of data modification or corruption.
|
||||
|
||||
## MCP Architecture
|
||||
|
||||
### Resources
|
||||
|
||||
- `cordra://objects/{prefix}/{suffix}` - Retrieve a specific object by its handle identifier
|
||||
- `cordra://schemas/{schema_name}` - Schema definition for a specific type.
|
||||
- `cordra://design` - Design document containing the overall structure and configuration of the Cordra repository.
|
||||
|
||||
### Tools
|
||||
|
||||
- `list_types` - List all available types in the Cordra repository.
|
||||
- Returns a JSON array of type names that are defined in the repository
|
||||
- Types are returned in sorted order
|
||||
|
||||
- `get_type_schema` - Retrieve the JSON schema definition for a specific type.
|
||||
- `type_name` - The name of the type (e.g., "Person", "Document", "Project")
|
||||
- Returns the full schema definition as JSON
|
||||
|
||||
- `get_object` - Retrieve a digital object by its complete ID/handle.
|
||||
- `object_id` - Complete object ID (e.g., "test/abc123")
|
||||
|
||||
- `search_objects` - Search for digital objects using a query string with pagination support.
|
||||
- `query` - Lucene/Solr compatible search query
|
||||
- `type` - Optional filter by object type
|
||||
- `limit` - Number of results per page (default: 1)
|
||||
- `type` - Optional filter by object type
|
||||
- `limit` - Number of results per page (default: 25)
|
||||
- `page_num` - Page number to retrieve, 0-based (default: 0)
|
||||
|
||||
- `count_objects` - Count the total number of objects matching a query.
|
||||
- `query` - Lucene/Solr compatible search query
|
||||
- `type` - Optional filter by object type
|
||||
- `type` - Optional filter by object type
|
||||
|
||||
- `get_design_object` - Retrieve the Cordra design object containing repository configuration.
|
||||
- Includes type definitions, workflow configurations, and system settings
|
||||
- Administrative privileges are typically required to access this object
|
||||
|
||||
#### Query Syntax
|
||||
|
||||
**CRITICAL**: JSON properties MUST be prefixed with `/`
|
||||
|
||||
✅ **Correct Examples:**
|
||||
- `/title:*report*` - Wildcard search in title field
|
||||
- `/author/name:Daniel` - Nested property access
|
||||
- `/status:active AND /priority:high` - Boolean operators
|
||||
- Use `type` parameter instead of including `type:` in query
|
||||
|
||||
❌ **Wrong (will fail):**
|
||||
- `name:John` - Missing `/` prefix
|
||||
- `author/name:Daniel` - Missing leading `/`
|
||||
- `type:Person` - Use the `type` parameter instead
|
||||
|
||||
**Operators:** `*` (wildcard), `?` (single char), `AND`, `OR`, `NOT`, `"phrases"`
|
||||
|
||||
## Configuration
|
||||
|
||||
The MCP server can be configured using environment variables with the `CORDRA_` prefix:
|
||||
The MCP server can be configured using environment variables:
|
||||
|
||||
- `CORDRA_BASE_URL` - Cordra server URL (default: `https://localhost:8443`)
|
||||
- `CORDRA_USERNAME` - Username for authentication (optional)
|
||||
- `CORDRA_PASSWORD` - Password for authentication (optional)
|
||||
- `CORDRA_VERIFY_SSL` - SSL certificate verification (default: `true`)
|
||||
- `CORDRA_TIMEOUT` - Request timeout in seconds (default: `30`)
|
||||
- `LOGLEVEL` - Logging level (default: `INFO`, options: `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[project]
|
||||
name = "cordra-mcp"
|
||||
version = "1.2.0"
|
||||
version = "1.4.0"
|
||||
description = "MCP server for Cordra digital object repository"
|
||||
authors = [
|
||||
{name = "Daniel Bauer", email = "github@dbauer.me"},
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
"""MCP server for Cordra digital object repository."""
|
||||
|
||||
__version__ = "1.2.0"
|
||||
__version__ = "1.4.0"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
"""Configuration settings for the MCP Cordra server."""
|
||||
|
||||
from pydantic import Field
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import Field, field_validator
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
@@ -16,6 +18,10 @@ class CordraConfig(BaseSettings):
|
||||
default="https://localhost:8443",
|
||||
description="Base URL of the Cordra repository",
|
||||
)
|
||||
host: str = Field(
|
||||
default="0.0.0.0",
|
||||
description="The host under which the MCP server runs when deployed as http run_mode",
|
||||
)
|
||||
username: str | None = Field(
|
||||
default=None, description="Username for Cordra authentication"
|
||||
)
|
||||
@@ -26,3 +32,24 @@ class CordraConfig(BaseSettings):
|
||||
default=True, description="Whether to verify SSL certificates"
|
||||
)
|
||||
timeout: int = Field(default=30, description="Request timeout in seconds")
|
||||
run_mode: Literal["stdio", "http"] | None = Field(
|
||||
default="stdio", description="Run mode for the MCP client"
|
||||
)
|
||||
log_level: str = Field(
|
||||
default="INFO",
|
||||
description="Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
|
||||
validation_alias="LOGLEVEL",
|
||||
)
|
||||
|
||||
@field_validator("log_level", mode="before")
|
||||
@classmethod
|
||||
def validate_log_level(cls, v: str) -> str:
|
||||
"""Validate that log_level is a standard logging level."""
|
||||
level_str = str(v).upper().strip()
|
||||
valid_levels = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}
|
||||
|
||||
if level_str not in valid_levels:
|
||||
raise ValueError(
|
||||
f"Invalid log level '{v}'. Must be one of: {', '.join(valid_levels)}"
|
||||
)
|
||||
return level_str
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
"""MCP server for Cordra digital object repository."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
from mcp.server.fastmcp.resources import FunctionResource
|
||||
|
||||
from . import __version__
|
||||
from .client import (
|
||||
CordraAuthenticationError,
|
||||
CordraClient,
|
||||
@@ -16,37 +15,39 @@ from .client import (
|
||||
from .config import CordraConfig
|
||||
|
||||
# Initialize the MCP server
|
||||
mcp = FastMCP("cordra-mcp")
|
||||
config = CordraConfig()
|
||||
mcp = FastMCP("cordra-mcp", host=config.host, port=8000)
|
||||
|
||||
# Initialize Cordra client at startup
|
||||
config = CordraConfig()
|
||||
cordra_client = CordraClient(config)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(config.log_level)
|
||||
|
||||
|
||||
@mcp.tool(
|
||||
name="search_objects",
|
||||
title="Search Cordra Objects",
|
||||
description="""Search for digital objects in the Cordra repository using Lucene/Solr query syntax.
|
||||
description="""Search for digital objects using Lucene/Solr query syntax.
|
||||
|
||||
Examples:
|
||||
- /title:report - Find objects with 'report' in title
|
||||
- /author:smith - Find objects by author Smith
|
||||
- /name:John AND type:Person - Complex queries
|
||||
CRITICAL SYNTAX RULES:
|
||||
1. Properties MUST start with '/' - Example: /title:report
|
||||
2. Nested properties: /parent/child:value
|
||||
3. Use 'type' parameter - NEVER 'type:' in query
|
||||
4. Operators: * ? AND OR NOT "phrases"
|
||||
|
||||
Pagination:
|
||||
- Results are paginated with 0-based page numbering
|
||||
- Use 'limit' to control page size (default: 25)
|
||||
- Use 'page_num' to specify which page to retrieve (default: 0)
|
||||
✅ CORRECT:
|
||||
- /title:*report* /author/name:Daniel
|
||||
- /status:active AND /priority:high
|
||||
- query="/title:report", type="Document"
|
||||
|
||||
Returns a JSON object containing:
|
||||
- object_ids: List of object IDs that match the search
|
||||
- total_count: Total number of objects matching the query
|
||||
- page_num: Current page number
|
||||
- page_size: Number of results per page
|
||||
❌ WRONG:
|
||||
- name:John (missing /)
|
||||
- author/name:Daniel (missing /)
|
||||
- type:Person (use type parameter)
|
||||
|
||||
Use the cordra://objects/{prefix}/{suffix} resources to retrieve full object details.""",
|
||||
Returns: {results: [ids], total_count, page_num, page_size}
|
||||
Pagination: limit (default 25), page_num (0-based)""",
|
||||
)
|
||||
async def search_objects(
|
||||
query: str,
|
||||
@@ -57,10 +58,9 @@ async def search_objects(
|
||||
"""Search for digital objects in the Cordra repository with pagination support.
|
||||
|
||||
Args:
|
||||
query: The search query string (Lucene/Solr compatible). Examples:
|
||||
- "/title:report" - Find objects with "report" in title
|
||||
- "/author:smith" - Find objects by author Smith
|
||||
- "/name:John AND type:Person" - Complex queries
|
||||
query: Search query (Lucene/Solr). Properties MUST start with '/'.
|
||||
✅ CORRECT: /title:*report*, /author/name:Daniel
|
||||
❌ WRONG: name:John, author/name:Daniel, type:Person
|
||||
type: Optional filter by object type (e.g., "Person", "Document", "Project")
|
||||
limit: Page size - number of results per page (default: 25)
|
||||
page_num: Page number to retrieve, 0-based (default: 0 for first page)
|
||||
@@ -94,7 +94,8 @@ async def search_objects(
|
||||
|
||||
Examples:
|
||||
- /title:report - Count objects with 'report' in title
|
||||
- /author:smith - Count objects by author Smith
|
||||
- type:Person - Find all Persons. Note that "type" is special and uses no slash "/"
|
||||
- /author/name:Daniel - Find objects with author Daniel as nested property.
|
||||
- /name:John AND type:Person - Complex queries
|
||||
|
||||
Returns the count of objects as integer.
|
||||
@@ -107,10 +108,9 @@ async def count_objects(
|
||||
"""Count digital objects in the Cordra repository matching a search query.
|
||||
|
||||
Args:
|
||||
query: The search query string (Lucene/Solr compatible). Examples:
|
||||
- "/title:report" - Count objects with "report" in title
|
||||
- "/author:smith" - Count objects by author Smith
|
||||
- "/name:John AND type:Person" - Complex queries
|
||||
query: Search query (Lucene/Solr). Properties MUST start with '/'.
|
||||
✅ CORRECT: /title:*report*, /author/name:Daniel
|
||||
❌ WRONG: name:John, author/name:Daniel, type:Person
|
||||
type: Optional filter by object type (e.g., "Person", "Document", "Project")
|
||||
|
||||
Returns:
|
||||
@@ -132,35 +132,33 @@ async def count_objects(
|
||||
raise RuntimeError(f"Count failed: {e}") from e
|
||||
|
||||
|
||||
@mcp.resource(
|
||||
"cordra://objects/{prefix}/{suffix}",
|
||||
name="cordra-object",
|
||||
title="Retrieve Cordra Digital Object",
|
||||
description="Retrieve a Digital Object and Metadata from Cordra by its ID/handle.",
|
||||
mime_type="application/json",
|
||||
@mcp.tool(
|
||||
name="get_object",
|
||||
title="Get Cordra Object by ID",
|
||||
description="""Retrieve a digital object by its complete ID/handle.
|
||||
|
||||
Returns: Full object with metadata as JSON
|
||||
Example: get_object("test/abc123")""",
|
||||
)
|
||||
async def get_cordra_object(prefix: str, suffix: str) -> str:
|
||||
"""Retrieve a Cordra digital object by its ID.
|
||||
async def get_object(object_id: str) -> str:
|
||||
"""Retrieve a Cordra digital object by its complete ID.
|
||||
|
||||
Args:
|
||||
prefix: The prefix part of the object ID (e.g., 'wildlive')
|
||||
suffix: The suffix part of the object ID (e.g., '7a4b7b65f8bb155ad36d')
|
||||
object_id: The complete object ID/handle (e.g., "test/abc123" or "wildlive/7a4b7b65f8bb155ad36d")
|
||||
|
||||
Returns:
|
||||
JSON representation of the digital object
|
||||
JSON string containing the complete digital object with all metadata
|
||||
|
||||
Raises:
|
||||
RuntimeError: If the object is not found or there's an API error
|
||||
"""
|
||||
|
||||
object_id = f"{prefix}/{suffix}"
|
||||
try:
|
||||
digital_object = await cordra_client.get_object(object_id)
|
||||
object_dict = digital_object.model_dump()
|
||||
return json.dumps(object_dict, indent=2)
|
||||
|
||||
except ValueError as e:
|
||||
raise RuntimeError(f"Invalid parameters: {e}") from e
|
||||
raise RuntimeError(f"Invalid object ID: {e}") from e
|
||||
except CordraNotFoundError as e:
|
||||
raise RuntimeError(f"Object not found: {object_id}") from e
|
||||
except CordraAuthenticationError as e:
|
||||
@@ -169,14 +167,18 @@ async def get_cordra_object(prefix: str, suffix: str) -> str:
|
||||
raise RuntimeError(f"Failed to retrieve object {object_id}: {e}") from e
|
||||
|
||||
|
||||
@mcp.resource(
|
||||
"cordra://design",
|
||||
name="cordra-design",
|
||||
title="Retrieve Cordra Design Object",
|
||||
description="Retrieve the Cordra design object containing repository configuration. Administrative privileges are typically required to access this object.",
|
||||
mime_type="application/json",
|
||||
@mcp.tool(
|
||||
name="get_design_object",
|
||||
title="Get Cordra Design Object",
|
||||
description="""
|
||||
The design object is the central location where Cordra stores its configuration,
|
||||
including type definitions, workflow configurations, and system settings.
|
||||
Administrative privileges are typically required to access this object.
|
||||
|
||||
Returns: The design object as JSON
|
||||
""",
|
||||
)
|
||||
async def get_cordra_design() -> str:
|
||||
async def get_cordra_design_object() -> str:
|
||||
"""Retrieve the Cordra design object containing repository configuration.
|
||||
|
||||
The design object is the central location where Cordra stores its configuration,
|
||||
@@ -202,25 +204,25 @@ async def get_cordra_design() -> str:
|
||||
raise RuntimeError(f"Failed to retrieve design object: {e}") from e
|
||||
|
||||
|
||||
async def create_schema_resource(schema_name: str) -> str:
|
||||
"""Create content for a specific schema resource."""
|
||||
try:
|
||||
schema_object = await cordra_client.get_schema(schema_name)
|
||||
schema_dict = schema_object.model_dump()
|
||||
return json.dumps(schema_dict, indent=2)
|
||||
except CordraNotFoundError as e:
|
||||
raise RuntimeError(f"Schema not found: {schema_name}") from e
|
||||
except CordraAuthenticationError as e:
|
||||
raise RuntimeError(f"Authentication failed: {e}") from e
|
||||
except CordraClientError as e:
|
||||
raise RuntimeError(f"Failed to retrieve schema {schema_name}: {e}") from e
|
||||
@mcp.tool(
|
||||
name="list_types",
|
||||
title="List Available Types",
|
||||
description="""List all available object types in the Cordra repository.
|
||||
|
||||
Returns a list of type names that are defined in the repository as json array.""",
|
||||
)
|
||||
async def list_types() -> str:
|
||||
"""List all available types in the Cordra repository.
|
||||
|
||||
async def register_schema_resources() -> None:
|
||||
"""Register individual schema resources dynamically."""
|
||||
Returns:
|
||||
JSON string containing a list of type names
|
||||
|
||||
Raises:
|
||||
RuntimeError: If there's an API error or authentication failure
|
||||
"""
|
||||
try:
|
||||
# Get all available schemas using pagination
|
||||
all_schemas = []
|
||||
# Get all available types using pagination
|
||||
all_types = []
|
||||
page_num = 0
|
||||
page_size = 20
|
||||
|
||||
@@ -229,7 +231,11 @@ async def register_schema_resources() -> None:
|
||||
"type:Schema", page_size=page_size, page_num=page_num
|
||||
)
|
||||
schemas = search_result["results"]
|
||||
all_schemas.extend(schemas)
|
||||
|
||||
for schema in schemas:
|
||||
type_name = schema.get("content", {}).get("name")
|
||||
if type_name:
|
||||
all_types.append(type_name)
|
||||
|
||||
# Check if we've retrieved all schemas
|
||||
if len(schemas) < page_size:
|
||||
@@ -237,45 +243,58 @@ async def register_schema_resources() -> None:
|
||||
|
||||
page_num += 1
|
||||
|
||||
for schema in all_schemas:
|
||||
schema_name = schema.get("content", {}).get("name")
|
||||
if not schema_name:
|
||||
logger.warning("Schema without a name found, skipping.")
|
||||
continue
|
||||
all_types.sort()
|
||||
return json.dumps(all_types, indent=2)
|
||||
|
||||
logger.info(f"Registering schema resource for cordra type {schema_name}")
|
||||
|
||||
async def schema_fn(name: str = schema_name) -> str:
|
||||
return await create_schema_resource(name)
|
||||
|
||||
mcp.add_resource(
|
||||
FunctionResource.from_function(
|
||||
uri=f"cordra://schemas/{schema_name}",
|
||||
fn=schema_fn,
|
||||
name=f"cordra-type-schema-{schema_name}",
|
||||
title=f"Cordra Type Schema: {schema_name}",
|
||||
description=f"Retrieve the JSON schema for the Cordra Type {schema_name}",
|
||||
mime_type="application/json",
|
||||
)
|
||||
)
|
||||
|
||||
logger.info(f"Registered {len(all_schemas)} schema resources")
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to register schema resources: {e}")
|
||||
except CordraAuthenticationError as e:
|
||||
raise RuntimeError(f"Authentication failed: {e}") from e
|
||||
except CordraClientError as e:
|
||||
raise RuntimeError(f"Failed to list types: {e}") from e
|
||||
|
||||
|
||||
async def initialize_server() -> None:
|
||||
"""Initialize server resources before starting."""
|
||||
logger.info("Initializing Cordra MCP server...")
|
||||
await register_schema_resources()
|
||||
logger.info("Server initialization complete")
|
||||
@mcp.tool(
|
||||
name="get_type_schema",
|
||||
title="Get Type Schema",
|
||||
description="""Retrieve the JSON schema definition for a specific type.
|
||||
|
||||
Args:
|
||||
type_name: The name of the type (e.g., "Person", "Document", "Project")
|
||||
|
||||
Returns: The full schema definition as JSON""",
|
||||
)
|
||||
async def get_type_schema(type_name: str) -> str:
|
||||
"""Retrieve the JSON schema definition for a specific object type.
|
||||
|
||||
Args:
|
||||
type_name: The name of the type to retrieve the schema for
|
||||
|
||||
Returns:
|
||||
JSON string containing the schema definition
|
||||
|
||||
Raises:
|
||||
RuntimeError: If the type is not found, authentication fails, or there's an API error
|
||||
"""
|
||||
try:
|
||||
schema_object = await cordra_client.get_schema(type_name)
|
||||
schema_dict = schema_object.model_dump()
|
||||
return json.dumps(schema_dict, indent=2)
|
||||
except CordraNotFoundError as e:
|
||||
raise RuntimeError(f"Type '{type_name}' not found") from e
|
||||
except CordraAuthenticationError as e:
|
||||
raise RuntimeError(f"Authentication failed: {e}") from e
|
||||
except CordraClientError as e:
|
||||
raise RuntimeError(
|
||||
f"Failed to retrieve schema for type '{type_name}': {e}"
|
||||
) from e
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Main entry point for the MCP server."""
|
||||
asyncio.run(initialize_server())
|
||||
mcp.run()
|
||||
logger.info(f"Starting Cordra MCP server v{__version__}...")
|
||||
if config.run_mode == "stdio":
|
||||
mcp.run()
|
||||
else:
|
||||
mcp.run(transport="streamable-http")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -14,8 +14,10 @@ from cordra_mcp.client import (
|
||||
)
|
||||
from cordra_mcp.server import (
|
||||
count_objects,
|
||||
get_cordra_design,
|
||||
get_cordra_object,
|
||||
get_cordra_design_object,
|
||||
get_object,
|
||||
get_type_schema,
|
||||
list_types,
|
||||
search_objects,
|
||||
)
|
||||
|
||||
@@ -44,27 +46,23 @@ def sample_digital_object() -> DigitalObject:
|
||||
)
|
||||
|
||||
|
||||
class TestGetCordraObject:
|
||||
"""Test the get_cordra_object resource handler."""
|
||||
class TestGetObject:
|
||||
"""Test the get_object tool."""
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_get_object_success(
|
||||
self, mock_client: Any, sample_digital_object: DigitalObject
|
||||
) -> None:
|
||||
"""Test successful object retrieval."""
|
||||
"""Test successful object retrieval with complete ID."""
|
||||
mock_client.get_object = AsyncMock(return_value=sample_digital_object)
|
||||
|
||||
result = await get_cordra_object("people", "john-doe-123")
|
||||
result = await get_object("people/john-doe-123")
|
||||
|
||||
# Verify the result is valid JSON
|
||||
parsed_result = json.loads(result)
|
||||
assert parsed_result["id"] == "people/john-doe-123"
|
||||
assert parsed_result["type"] == "Person"
|
||||
assert parsed_result["content"]["name"] == "John Doe"
|
||||
assert parsed_result["content"]["birthday"] == "1990-05-15"
|
||||
assert parsed_result["metadata"]["created"] == "2023-01-01"
|
||||
assert len(parsed_result["payloads"]) == 1
|
||||
assert parsed_result["payloads"][0]["name"] == "profile_photo"
|
||||
|
||||
# Verify the client was called with the correct object ID
|
||||
mock_client.get_object.assert_called_once_with("people/john-doe-123")
|
||||
@@ -73,14 +71,14 @@ class TestGetCordraObject:
|
||||
async def test_get_object_not_found(self, mock_client: Any) -> None:
|
||||
"""Test object not found exception."""
|
||||
mock_client.get_object = AsyncMock(
|
||||
side_effect=CordraNotFoundError("Object not found: people/nonexistent")
|
||||
side_effect=CordraNotFoundError("Object not found: test/nonexistent")
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_cordra_object("people", "nonexistent")
|
||||
await get_object("test/nonexistent")
|
||||
|
||||
assert "Object not found: people/nonexistent" in str(exc_info.value)
|
||||
mock_client.get_object.assert_called_once_with("people/nonexistent")
|
||||
assert "Object not found: test/nonexistent" in str(exc_info.value)
|
||||
mock_client.get_object.assert_called_once_with("test/nonexistent")
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_get_object_client_error(self, mock_client: Any) -> None:
|
||||
@@ -90,84 +88,156 @@ class TestGetCordraObject:
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_cordra_object("people", "john-doe-123")
|
||||
await get_object("test/obj123")
|
||||
|
||||
assert "Failed to retrieve object people/john-doe-123" in str(exc_info.value)
|
||||
assert "Connection failed" in str(exc_info.value)
|
||||
mock_client.get_object.assert_called_once_with("people/john-doe-123")
|
||||
assert "Failed to retrieve object test/obj123" in str(exc_info.value)
|
||||
mock_client.get_object.assert_called_once_with("test/obj123")
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_object_id_construction(
|
||||
self, mock_client: Any, sample_digital_object: DigitalObject
|
||||
) -> None:
|
||||
"""Test that object ID is correctly constructed from prefix and suffix."""
|
||||
mock_client.get_object = AsyncMock(return_value=sample_digital_object)
|
||||
|
||||
# Test various prefix/suffix combinations
|
||||
test_cases = [
|
||||
("people", "john-doe-123", "people/john-doe-123"),
|
||||
("documents", "report-2023", "documents/report-2023"),
|
||||
("items", "item_with_underscores", "items/item_with_underscores"),
|
||||
]
|
||||
|
||||
for prefix, suffix, expected_id in test_cases:
|
||||
await get_cordra_object(prefix, suffix)
|
||||
mock_client.get_object.assert_called_with(expected_id)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_json_formatting(
|
||||
self, mock_client: Any, sample_digital_object: DigitalObject
|
||||
) -> None:
|
||||
"""Test that the returned JSON is properly formatted."""
|
||||
mock_client.get_object = AsyncMock(return_value=sample_digital_object)
|
||||
|
||||
result = await get_cordra_object("people", "john-doe-123")
|
||||
|
||||
# Verify it's valid JSON with proper indentation
|
||||
parsed_result = json.loads(result)
|
||||
assert isinstance(parsed_result, dict)
|
||||
|
||||
# Check that the result contains indentation (pretty-printed)
|
||||
assert " " in result # Should have 2-space indentation
|
||||
|
||||
# Verify all expected fields are present
|
||||
assert "id" in parsed_result
|
||||
assert "type" in parsed_result
|
||||
assert "content" in parsed_result
|
||||
assert "metadata" in parsed_result
|
||||
assert "acl" in parsed_result
|
||||
assert "payloads" in parsed_result
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_minimal_object(self, mock_client: Any) -> None:
|
||||
"""Test handling of object with minimal data."""
|
||||
minimal_object = DigitalObject(
|
||||
id="test/minimal",
|
||||
type="",
|
||||
content={"id": "test/minimal"},
|
||||
metadata=None,
|
||||
acl=None,
|
||||
payloads=None,
|
||||
async def test_get_object_authentication_error(self, mock_client: Any) -> None:
|
||||
"""Test authentication error handling."""
|
||||
mock_client.get_object = AsyncMock(
|
||||
side_effect=CordraAuthenticationError("Authentication failed")
|
||||
)
|
||||
mock_client.get_object = AsyncMock(return_value=minimal_object)
|
||||
|
||||
result = await get_cordra_object("test", "minimal")
|
||||
parsed_result = json.loads(result)
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_object("test/obj123")
|
||||
|
||||
assert parsed_result["id"] == "test/minimal"
|
||||
assert parsed_result["type"] == ""
|
||||
assert parsed_result["content"]["id"] == "test/minimal"
|
||||
assert parsed_result["metadata"] is None
|
||||
assert parsed_result["acl"] is None
|
||||
assert parsed_result["payloads"] is None
|
||||
assert "Authentication failed" in str(exc_info.value)
|
||||
mock_client.get_object.assert_called_once_with("test/obj123")
|
||||
|
||||
|
||||
class TestSchemaResourceFunctions:
|
||||
"""Test the schema resource functions."""
|
||||
class TestListTypes:
|
||||
"""Test the list_types tool."""
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_create_schema_resource_success(self, mock_client: Any) -> None:
|
||||
"""Test successful schema resource creation."""
|
||||
async def test_list_types_success(self, mock_client: Any) -> None:
|
||||
"""Test successful listing of available types."""
|
||||
mock_search_result = {
|
||||
"results": [
|
||||
{"content": {"name": "User"}, "id": "test/user-schema"},
|
||||
{"content": {"name": "Project"}, "id": "test/project-schema"},
|
||||
{"content": {"name": "Document"}, "id": "test/doc-schema"},
|
||||
],
|
||||
"total_size": 3,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
mock_client.find = AsyncMock(return_value=mock_search_result)
|
||||
|
||||
result = await list_types()
|
||||
|
||||
# Verify the result is valid JSON
|
||||
parsed_result = json.loads(result)
|
||||
assert parsed_result == ["Document", "Project", "User"] # Should be sorted
|
||||
|
||||
# Verify the client was called with correct query
|
||||
mock_client.find.assert_called_once_with(
|
||||
"type:Schema", page_size=20, page_num=0
|
||||
)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_list_types_with_pagination(self, mock_client: Any) -> None:
|
||||
"""Test listing types with pagination."""
|
||||
first_page = {
|
||||
"results": [
|
||||
{"content": {"name": f"Type{i}"}, "id": f"test/schema{i}"}
|
||||
for i in range(20)
|
||||
],
|
||||
"total_size": 25,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
|
||||
second_page = {
|
||||
"results": [
|
||||
{"content": {"name": "ZType"}, "id": "test/zschema"},
|
||||
],
|
||||
"total_size": 25,
|
||||
"page_num": 1,
|
||||
"page_size": 20,
|
||||
}
|
||||
|
||||
mock_client.find = AsyncMock(side_effect=[first_page, second_page])
|
||||
|
||||
result = await list_types()
|
||||
|
||||
parsed_result = json.loads(result)
|
||||
# Should contain all 21 types and be sorted
|
||||
assert len(parsed_result) == 21
|
||||
assert parsed_result == sorted(parsed_result)
|
||||
assert "Type0" in parsed_result
|
||||
assert "ZType" in parsed_result
|
||||
|
||||
# Verify pagination calls
|
||||
assert mock_client.find.call_count == 2
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_list_types_missing_name(self, mock_client: Any) -> None:
|
||||
"""Test listing types when some schemas have missing name field."""
|
||||
mock_search_result = {
|
||||
"results": [
|
||||
{"content": {"name": "User"}, "id": "test/user-schema"},
|
||||
{"content": {}, "id": "test/no-name-schema"}, # Missing name
|
||||
{"content": {"name": "Project"}, "id": "test/project-schema"},
|
||||
],
|
||||
"total_size": 3,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
mock_client.find = AsyncMock(return_value=mock_search_result)
|
||||
|
||||
result = await list_types()
|
||||
|
||||
# Only 2 types should be returned (those with name)
|
||||
parsed_result = json.loads(result)
|
||||
assert parsed_result == ["Project", "User"]
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_list_types_client_error(self, mock_client: Any) -> None:
|
||||
"""Test listing types with client error."""
|
||||
mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed"))
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await list_types()
|
||||
|
||||
assert "Failed to list types:" in str(exc_info.value)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_list_types_authentication_error(self, mock_client: Any) -> None:
|
||||
"""Test listing types with authentication error."""
|
||||
mock_client.find = AsyncMock(
|
||||
side_effect=CordraAuthenticationError("Authentication failed")
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await list_types()
|
||||
|
||||
assert "Authentication failed:" in str(exc_info.value)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_list_types_empty(self, mock_client: Any) -> None:
|
||||
"""Test listing types when no types are available."""
|
||||
mock_search_result = {
|
||||
"results": [],
|
||||
"total_size": 0,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
mock_client.find = AsyncMock(return_value=mock_search_result)
|
||||
|
||||
result = await list_types()
|
||||
|
||||
parsed_result = json.loads(result)
|
||||
assert parsed_result == []
|
||||
|
||||
|
||||
class TestGetTypeSchema:
|
||||
"""Test the get_type_schema tool."""
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_get_type_schema_success(self, mock_client: Any) -> None:
|
||||
"""Test successful schema retrieval."""
|
||||
mock_schema = DigitalObject(
|
||||
id="test/user-schema",
|
||||
type="Schema",
|
||||
@@ -175,9 +245,7 @@ class TestSchemaResourceFunctions:
|
||||
)
|
||||
mock_client.get_schema = AsyncMock(return_value=mock_schema)
|
||||
|
||||
from cordra_mcp.server import create_schema_resource
|
||||
|
||||
result = await create_schema_resource("User")
|
||||
result = await get_type_schema("User")
|
||||
|
||||
# Verify the result is valid JSON
|
||||
parsed_result = json.loads(result)
|
||||
@@ -189,131 +257,58 @@ class TestSchemaResourceFunctions:
|
||||
mock_client.get_schema.assert_called_once_with("User")
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_create_schema_resource_not_found(self, mock_client: Any) -> None:
|
||||
"""Test schema resource creation with schema not found."""
|
||||
async def test_get_type_schema_not_found(self, mock_client: Any) -> None:
|
||||
"""Test schema retrieval with type not found."""
|
||||
mock_client.get_schema = AsyncMock(
|
||||
side_effect=CordraNotFoundError("Schema not found")
|
||||
)
|
||||
|
||||
from cordra_mcp.server import create_schema_resource
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await create_schema_resource("NonExistent")
|
||||
await get_type_schema("NonExistent")
|
||||
|
||||
assert "Schema not found: NonExistent" in str(exc_info.value)
|
||||
assert "Type 'NonExistent' not found" in str(exc_info.value)
|
||||
mock_client.get_schema.assert_called_once_with("NonExistent")
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_register_schema_resources_success(self, mock_client: Any) -> None:
|
||||
"""Test successful schema resource registration."""
|
||||
mock_search_result = {
|
||||
"results": [
|
||||
{"content": {"name": "User"}, "id": "test/user-schema"},
|
||||
{"content": {"name": "Project"}, "id": "test/project-schema"},
|
||||
{"content": {"name": "Document"}, "id": "test/doc-schema"},
|
||||
],
|
||||
"total_size": 3,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
mock_client.find = AsyncMock(return_value=mock_search_result)
|
||||
|
||||
# Mock the mcp.add_resource method
|
||||
with patch("cordra_mcp.server.mcp") as mock_mcp:
|
||||
from cordra_mcp.server import register_schema_resources
|
||||
|
||||
await register_schema_resources()
|
||||
|
||||
# Verify the client was called with correct query
|
||||
mock_client.find.assert_called_once_with(
|
||||
"type:Schema", page_size=20, page_num=0
|
||||
async def test_get_type_schema_authentication_error(self, mock_client: Any) -> None:
|
||||
"""Test schema retrieval with authentication error."""
|
||||
mock_client.get_schema = AsyncMock(
|
||||
side_effect=CordraAuthenticationError("Authentication failed")
|
||||
)
|
||||
|
||||
# Verify add_resource was called for each schema
|
||||
assert mock_mcp.add_resource.call_count == 3
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_type_schema("User")
|
||||
|
||||
assert "Authentication failed:" in str(exc_info.value)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_register_schema_resources_missing_name(
|
||||
self, mock_client: Any
|
||||
) -> None:
|
||||
"""Test schema resource registration with objects missing name field."""
|
||||
mock_search_result = {
|
||||
"results": [
|
||||
{"content": {"name": "User"}, "id": "test/user-schema"},
|
||||
{"content": {}, "id": "test/no-name-schema"}, # Missing name field
|
||||
{"content": {"name": "Project"}, "id": "test/project-schema"},
|
||||
],
|
||||
"total_size": 3,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
mock_client.find = AsyncMock(return_value=mock_search_result)
|
||||
|
||||
with patch("cordra_mcp.server.mcp") as mock_mcp:
|
||||
from cordra_mcp.server import register_schema_resources
|
||||
|
||||
await register_schema_resources()
|
||||
|
||||
# Only 2 schemas should be registered (those with name field)
|
||||
assert mock_mcp.add_resource.call_count == 2
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_register_schema_resources_client_error(
|
||||
self, mock_client: Any
|
||||
) -> None:
|
||||
"""Test schema resource registration with client error."""
|
||||
mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed"))
|
||||
|
||||
# Should not raise an exception, just log a warning
|
||||
from cordra_mcp.server import register_schema_resources
|
||||
|
||||
await register_schema_resources() # Should complete without raising
|
||||
|
||||
mock_client.find.assert_called_once_with(
|
||||
"type:Schema", page_size=20, page_num=0
|
||||
async def test_get_type_schema_client_error(self, mock_client: Any) -> None:
|
||||
"""Test schema retrieval with client error."""
|
||||
mock_client.get_schema = AsyncMock(
|
||||
side_effect=CordraClientError("Connection failed")
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_type_schema("User")
|
||||
|
||||
assert "Failed to retrieve schema for type 'User':" in str(exc_info.value)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_register_schema_resources_pagination(self, mock_client: Any) -> None:
|
||||
"""Test schema resource registration with pagination."""
|
||||
# Mock multiple pages of results
|
||||
# First page with full 20 results (simulating more schemas)
|
||||
first_page_schemas = [
|
||||
{"content": {"name": f"Schema{i}"}, "id": f"test/schema{i}"}
|
||||
for i in range(20)
|
||||
]
|
||||
first_page = {
|
||||
"results": first_page_schemas,
|
||||
"total_size": 25,
|
||||
"page_num": 0,
|
||||
"page_size": 20,
|
||||
}
|
||||
async def test_get_type_schema_json_formatting(self, mock_client: Any) -> None:
|
||||
"""Test that schema is properly formatted as JSON."""
|
||||
mock_schema = DigitalObject(
|
||||
id="test/schema",
|
||||
type="Schema",
|
||||
content={"name": "Test", "properties": {"field": "value"}},
|
||||
)
|
||||
mock_client.get_schema = AsyncMock(return_value=mock_schema)
|
||||
|
||||
# Second page with fewer results (indicating last page)
|
||||
second_page = {
|
||||
"results": [
|
||||
{"content": {"name": "Document"}, "id": "test/doc-schema"},
|
||||
],
|
||||
"total_size": 25,
|
||||
"page_num": 1,
|
||||
"page_size": 20,
|
||||
}
|
||||
result = await get_type_schema("Test")
|
||||
|
||||
# Return first page, then second page (with fewer results indicating last page)
|
||||
mock_client.find = AsyncMock(side_effect=[first_page, second_page])
|
||||
|
||||
with patch("cordra_mcp.server.mcp") as mock_mcp:
|
||||
from cordra_mcp.server import register_schema_resources
|
||||
|
||||
await register_schema_resources()
|
||||
|
||||
# Verify pagination calls
|
||||
assert mock_client.find.call_count == 2
|
||||
mock_client.find.assert_any_call("type:Schema", page_size=20, page_num=0)
|
||||
mock_client.find.assert_any_call("type:Schema", page_size=20, page_num=1)
|
||||
|
||||
# Verify all 21 schemas were registered (20 from first page + 1 from second page)
|
||||
assert mock_mcp.add_resource.call_count == 21
|
||||
# Verify it's valid JSON with proper indentation
|
||||
parsed_result = json.loads(result)
|
||||
assert isinstance(parsed_result, dict)
|
||||
assert " " in result # Should have 2-space indentation
|
||||
|
||||
|
||||
class TestSearchObjects:
|
||||
@@ -464,6 +459,39 @@ class TestSearchObjects:
|
||||
"nonexistent:data", object_type=None, page_size=25, page_num=0
|
||||
)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_search_objects_with_slash_prefixed_properties(
|
||||
self, mock_client: Any
|
||||
) -> None:
|
||||
"""Test object search with correct slash-prefixed property syntax."""
|
||||
mock_search_result = {
|
||||
"results": [
|
||||
{
|
||||
"id": "reports/2024-annual",
|
||||
"type": "Document",
|
||||
"content": {"title": "Annual Report 2024"},
|
||||
},
|
||||
],
|
||||
"total_size": 1,
|
||||
"page_num": 0,
|
||||
"page_size": 25,
|
||||
}
|
||||
mock_client.find = AsyncMock(return_value=mock_search_result)
|
||||
|
||||
# Test with slash-prefixed property and nested property
|
||||
result = await search_objects("/title:*report* AND /author/name:Daniel")
|
||||
|
||||
parsed_result = json.loads(result)
|
||||
assert parsed_result["results"] == ["reports/2024-annual"]
|
||||
assert parsed_result["total_count"] == 1
|
||||
|
||||
mock_client.find.assert_called_once_with(
|
||||
"/title:*report* AND /author/name:Daniel",
|
||||
object_type=None,
|
||||
page_size=25,
|
||||
page_num=0,
|
||||
)
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_search_objects_client_error(self, mock_client: Any) -> None:
|
||||
"""Test object search with client error."""
|
||||
@@ -568,7 +596,7 @@ class TestSearchObjects:
|
||||
|
||||
|
||||
class TestGetCordraDesign:
|
||||
"""Test the get_cordra_design resource handler."""
|
||||
"""Test the get_cordra_design_object tool."""
|
||||
|
||||
@patch("cordra_mcp.server.cordra_client")
|
||||
async def test_get_design_success(self, mock_client: Any) -> None:
|
||||
@@ -585,7 +613,7 @@ class TestGetCordraDesign:
|
||||
)
|
||||
mock_client.get_design = AsyncMock(return_value=mock_design)
|
||||
|
||||
result = await get_cordra_design()
|
||||
result = await get_cordra_design_object()
|
||||
|
||||
# Verify the result is valid JSON
|
||||
parsed_result = json.loads(result)
|
||||
@@ -606,7 +634,7 @@ class TestGetCordraDesign:
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_cordra_design()
|
||||
await get_cordra_design_object()
|
||||
|
||||
assert "Design object not found" in str(exc_info.value)
|
||||
mock_client.get_design.assert_called_once()
|
||||
@@ -619,7 +647,7 @@ class TestGetCordraDesign:
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_cordra_design()
|
||||
await get_cordra_design_object()
|
||||
|
||||
assert "Authentication failed" in str(exc_info.value)
|
||||
mock_client.get_design.assert_called_once()
|
||||
@@ -632,7 +660,7 @@ class TestGetCordraDesign:
|
||||
)
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
await get_cordra_design()
|
||||
await get_cordra_design_object()
|
||||
|
||||
assert "Failed to retrieve design object" in str(exc_info.value)
|
||||
assert "Connection failed" in str(exc_info.value)
|
||||
@@ -649,7 +677,7 @@ class TestGetCordraDesign:
|
||||
)
|
||||
mock_client.get_design = AsyncMock(return_value=mock_design)
|
||||
|
||||
result = await get_cordra_design()
|
||||
result = await get_cordra_design_object()
|
||||
|
||||
# Verify it's valid JSON with proper indentation
|
||||
parsed_result = json.loads(result)
|
||||
|
||||
Reference in New Issue
Block a user