list_graphs
Retrieve all accessible knowledge graphs from the Mnemosyne MCP server, with options to include statistics and metadata for comprehensive graph management.
Instructions
List all graphs accessible to the user via API server.
Args:
include_stats: Include graph statistics
include_metadata: Include graph metadata
Returns:
JSON string with graph list
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| include_stats | No | ||
| include_metadata | No |
Implementation Reference
- Primary handler function for the 'list_graphs' MCP tool. Decorated with @mcp_server.tool() for automatic registration. Handles authentication, caching via session, API calls, and response formatting using McpGraphListResponse.@mcp_server.tool() async def list_graphs( ctx: Context, include_stats: bool = True, include_metadata: bool = True ) -> str: """ List all graphs accessible to the user via API server. Args: include_stats: Include graph statistics include_metadata: Include graph metadata Returns: JSON string with graph list """ try: # Get session context (cached or fresh) auth_token, session_data = await get_session_context(ctx) # If we have valid session data with cached graphs, use it for basic list if session_data and not include_stats and not include_metadata: accessible_graphs = session_data.get("accessible_graphs", []) logger.info(f"Using cached graph list from session ({len(accessible_graphs)} graphs)") # Use cached response format mcp_response = McpGraphListResponse.from_cached_graphs(accessible_graphs) return mcp_response.to_json() # Need fresh data from API for stats/metadata or no session logger.info("Fetching fresh graph list via API") # Call API server api_response = await api_client.list_graphs(auth_token) # Transform to enhanced MCP format mcp_response = McpGraphListResponse.from_api_response(api_response, source="api_fresh") return mcp_response.to_json() except Exception as e: logger.error(f"List graphs failed: {e}") logger.error(f"Full traceback:\n{traceback.format_exc()}") error_response = McpErrorResponse.from_exception( e, error_type="GRAPH_LIST_FAILED", help_info={ "suggestions": [ "Check API connectivity", "Verify user authentication", "Try again in a moment" ] } ) return error_response.to_json()
- Dataclass definitions for the output schema of list_graphs tool, including McpGraphListResponse and supporting structures McpGraphStats, McpGraphInfo. Includes factory methods for transforming API responses and cached data into structured MCP responses.@dataclass class McpGraphStats: """Graph statistics""" triple_count: int size_mb: float last_updated: Optional[str] @dataclass class McpGraphInfo: """Enhanced graph information""" id: str name: str description: str stats: McpGraphStats sample_queries: List[str] @dataclass class McpGraphListResponse(McpResponseObject): """Enhanced graph list response""" graphs: List[McpGraphInfo] total_count: int source: str # "session_cache" | "api_fresh" success: bool = True @classmethod def from_api_response(cls, api_response: Dict[str, Any], source: str = "api_fresh") -> 'McpGraphListResponse': """Transform API graph list response to enhanced MCP format""" graphs_data = api_response.get("data", []) graphs = [] for graph_data in graphs_data: graph_id = graph_data.get("graph_id", graph_data.get("id", "")) graphs.append(McpGraphInfo( id=graph_id, name=graph_data.get("name", "Unnamed Graph"), description=graph_data.get("description", "No description available"), stats=McpGraphStats( triple_count=graph_data.get("triple_count", 0), size_mb=graph_data.get("size_mb", 0.0), last_updated=graph_data.get("updated_at") or graph_data.get("last_modified") ), sample_queries=_generate_sample_queries_for_graph(graph_data, graph_id) )) return cls( graphs=graphs, total_count=len(graphs), source=source ) @classmethod def from_cached_graphs(cls, graph_ids: List[str]) -> 'McpGraphListResponse': """Create simplified response from cached session data""" graphs = [] for graph_id in graph_ids: graphs.append(McpGraphInfo( id=graph_id, name=f"Graph: {graph_id}", description="Cached graph info - use include_stats=true for full details", stats=McpGraphStats( triple_count=0, size_mb=0.0, last_updated=None ), sample_queries=["# Use include_stats=true to get sample queries"] )) return cls( graphs=graphs, total_count=len(graphs), source="session_cache" )
- Helper method in MCPAPIClient class that performs the actual HTTP GET request to the /graphs API endpoint, called by the main handler.async def list_graphs(self, auth_token: str) -> Dict[str, Any]: """Call API /graphs endpoint.""" headers = {"Authorization": f"Bearer {auth_token}"} return await self._make_request("GET", "/graphs", headers)