apply_layout
Apply a chosen layout algorithm (force_directed, radial, circle, grid) to a graph for visualization and analysis within Graphistry MCP, optimizing structure and clarity for complex network data.
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
| Name | Required | Description | Default |
|---|---|---|---|
| graph_id | Yes | ||
| layout | Yes |
Implementation Reference
- The handler function for the 'apply_layout' tool. It applies one of several layout algorithms (force_directed, radial, circle, grid) to the specified graph using Graphistry settings and updates the cached graph object with the new 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
- src/graphistry_mcp_server/server.py:207-207 (registration)The @mcp.tool() decorator registers the apply_layout function as an MCP tool.@mcp.tool()