Skip to main content
Glama

apply_layout

Apply a layout algorithm to organize graph visualizations for clearer network analysis and data interpretation.

Instructions

Apply a layout algorithm to a graph.

Args:
    graph_id: ID of the graph to apply layout to
    layout: Layout algorithm to apply (force_directed, radial, circle, grid)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
graph_idYes
layoutYes

Implementation Reference

  • The main handler function for the 'apply_layout' tool. It is registered via the @mcp.tool() decorator. Modifies a cached Graphistry graph object by applying one of several layout algorithms (force_directed, radial, circle, grid) using url_params settings and returns the updated visualization URL.
    @mcp.tool()
    async def apply_layout(graph_id: str, layout: str) -> Dict[str, Any]:
        """Apply a layout algorithm to a graph.
        
        Args:
            graph_id: ID of the graph to apply layout to
            layout: Layout algorithm to apply (force_directed, radial, circle, grid)
        """
        try:
            if graph_id not in graph_cache:
                raise ValueError(f"Graph not found: {graph_id}")
    
            graph_data = graph_cache[graph_id]
            g = graph_data["graph"]
    
            # Apply layout using Graphistry's url_params settings
            if layout == "force_directed":
                g = g.settings(url_params={'play': 5000, 'strongGravity': True})
            elif layout == "radial":
                g = g.settings(url_params={'play': 0, 'layout': 'radial'})
            elif layout == "circle":
                g = g.settings(url_params={'play': 0, 'layout': 'circle'})
            elif layout == "grid":
                g = g.settings(url_params={'play': 0, 'layout': 'grid'})
            else:
                raise ValueError(f"Unsupported layout: {layout}")
        
            graph_cache[graph_id]["graph"] = g
        
            return {
                "graph_id": graph_id,
                "url": g.plot(render=False)
            }
        except Exception as e:
            logger.error(f"Error in apply_layout: {e}")
            raise

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/graphistry/graphistry-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server