diff --git a/src/mcp_cordra/client.py b/src/mcp_cordra/client.py index 2f9789a..fd580b9 100644 --- a/src/mcp_cordra/client.py +++ b/src/mcp_cordra/client.py @@ -148,4 +148,38 @@ class CordraClient: return [] except requests.RequestException as e: - raise CordraClientError(f"Failed to search with query '{query}': {e}") from e \ No newline at end of file + raise CordraClientError(f"Failed to search with query '{query}': {e}") from e + + async def get_schema(self, schema_name: str) -> DigitalObject: + """Retrieve a schema definition by its name. + + Args: + schema_name: The name of the schema to retrieve + + Returns: + The schema object containing the type definition + + Raises: + CordraNotFoundError: If the schema is not found + CordraAuthenticationError: If authentication fails + CordraClientError: For other API errors + """ + # Search for the specific schema by name using correct query format + query = f"type:Schema AND /name:{schema_name}" + + try: + schemas = await self.find(query) + + if not schemas: + raise CordraNotFoundError(f"Schema '{schema_name}' not found") + + # Get the first matching schema (should be unique by name) + schema_data = schemas[0] + + # Get the full schema object using its ID + return await self.get_object(schema_data['id']) + + except (CordraNotFoundError, CordraAuthenticationError): + raise + except Exception as e: + raise CordraClientError(f"Failed to retrieve schema '{schema_name}': {e}") from e \ No newline at end of file diff --git a/src/mcp_cordra/server.py b/src/mcp_cordra/server.py index bcb92c6..3f9067b 100644 --- a/src/mcp_cordra/server.py +++ b/src/mcp_cordra/server.py @@ -83,6 +83,32 @@ async def list_cordra_schemas() -> str: raise RuntimeError(f"Failed to list schemas: {e}") from e +@mcp.resource("cordra://schemas/{schema_name}", name="cordra-schema", description="Retrieve a specific Cordra schema definition by name") +async def get_cordra_schema(schema_name: str) -> str: + """Retrieve a Cordra schema definition by its name. + + Args: + schema_name: The name of the schema to retrieve (e.g., 'Person', 'Document') + + Returns: + JSON representation of the schema definition + + Raises: + RuntimeError: If the schema is not found or there's an API error + """ + 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: + raise RuntimeError(f"Schema not found: {schema_name}") + 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() async def ping() -> str: """Simple ping tool to test server connectivity."""