fetch_example
Retrieves a static example diagram, including its content, metadata, learning objectives, and patterns for educational use. Specify the diagram filename to access detailed insights.
Instructions
Retrieves a static example diagram with its content and learning context.
Args:
example_name: The filename of the example to fetch (e.g., 'serverless-on-aws.ilograph').
Returns:
A dictionary containing the example's content, metadata, learning objectives, and patterns.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| example_name | Yes |
Implementation Reference
- The core handler function for the 'fetch_example' tool. It looks up the example metadata, checks if the file exists on disk, reads the content, and returns it along with metadata, handling various error cases gracefully.async def fetch_example_tool(example_name: str, ctx: Context) -> Dict[str, Any]: """ Retrieves a static example diagram with its content and learning context. Args: example_name: The filename of the example to fetch (e.g., 'serverless-on-aws.ilograph'). Returns: A dictionary containing the example's content, metadata, learning objectives, and patterns. """ await ctx.info(f"Attempting to fetch example: {example_name}") metadata = EXAMPLES_DATABASE.get(example_name) if not metadata: await ctx.error(f"Example '{example_name}' not found.") available = ", ".join(EXAMPLES_DATABASE.keys()) return { "error": "not_found", "message": f"Example '{example_name}' not found. Use the 'list_examples' tool to see available examples.", "available_examples": available.split(", "), } file_path = metadata.file_path if not file_path.is_file(): await ctx.error(f"Example file not found on disk: {file_path}") return { "error": "internal_error", "message": "The example file could not be found on the server, even though it is listed in the database.", } try: content = file_path.read_text(encoding="utf-8") await ctx.info(f"Successfully read example file: {example_name}") response = metadata.model_dump() response["content"] = content return response except Exception as e: await ctx.error(f"Failed to read example file '{example_name}': {e}") logger.exception(f"Error reading example file {file_path}") return { "error": "internal_error", "message": f"An unexpected error occurred while reading the example file: {e}", }
- src/ilograph_mcp/tools/register_example_tools.py:139-146 (registration)The @mcp.tool decorator that registers the fetch_example_tool function as the 'fetch_example' tool in the FastMCP server.@mcp.tool( name="fetch_example", annotations={ "title": "Get Example Diagram", "readOnlyHint": True, "description": "Retrieves a specific Ilograph example diagram with its content and rich metadata.", }, )
- src/ilograph_mcp/server.py:67-67 (registration)The call to register_example_tools(mcp) in the main server setup, which triggers the definition and registration of the fetch_example tool.register_example_tools(mcp)
- Pydantic BaseModel used to define and validate the metadata structure for examples, which is included in the tool's response.class ExampleMetadata(BaseModel): """Defines the metadata structure for an Ilograph example.""" name: str category: Literal["beginner", "intermediate", "advanced"] description: str learning_objectives: List[str] = Field(default_factory=list) patterns_demonstrated: List[str] = Field(default_factory=list) @property def file_path(self) -> Path: """Returns the full path to the example file.""" return EXAMPLES_DIR / self.name