Skip to main content
Glama

disconnect

Safely close the active MCP server connection and clear all connection state and statistics. This tool handles disconnection even when no connection exists, returning details about the operation.

Instructions

Close the current MCP server connection.

Safely disconnects from the active MCP server and clears all connection state and statistics. This method is safe to call even if no connection exists.

Returns: Dictionary with disconnection details including: - success: Always True - message: Human-readable status message - was_connected: Whether a connection existed before disconnect - metadata: Request timing information and previous connection info

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The main handler function for the 'disconnect' MCP tool. It is decorated with @mcp.tool, which registers it and generates its schema from the signature and docstring. The function safely disconnects the current MCP server connection using ConnectionManager.disconnect(), handles errors gracefully, and returns a status dictionary.
    @mcp.tool async def disconnect(ctx: Context) -> dict[str, Any]: """Close the current MCP server connection. Safely disconnects from the active MCP server and clears all connection state and statistics. This method is safe to call even if no connection exists. Returns: Dictionary with disconnection details including: - success: Always True - message: Human-readable status message - was_connected: Whether a connection existed before disconnect - metadata: Request timing information and previous connection info """ start_time = time.perf_counter() # Get current state before disconnecting previous_state = ConnectionManager.get_status() was_connected = previous_state is not None try: # User-facing progress update await ctx.info("Disconnecting from MCP server") # Detailed technical log logger.info("Disconnecting from MCP server") await ConnectionManager.disconnect() elapsed_ms = (time.perf_counter() - start_time) * 1000 message = ( "Successfully disconnected from MCP server" if was_connected else "No active connection to disconnect" ) metadata: dict[str, Any] = { "request_time_ms": round(elapsed_ms, 2), "was_connected": was_connected, } # Include previous connection info if it existed if previous_state: metadata["previous_connection"] = { "server_url": previous_state.server_url, "transport": previous_state.transport, "duration_seconds": round( (time.perf_counter() - previous_state.connected_at.timestamp()) if previous_state.connected_at else 0, 2, ), "statistics": previous_state.statistics, } # User-facing completion update await ctx.info(message) # Detailed technical log logger.info(message, extra=metadata) return { "success": True, "message": message, "was_connected": was_connected, "metadata": metadata, } except Exception as e: # Disconnect should never fail, but handle gracefully elapsed_ms = (time.perf_counter() - start_time) * 1000 # User-facing error update await ctx.error(f"Unexpected error during disconnect: {str(e)}") # Detailed technical log logger.exception("Unexpected error during disconnect") return { "success": True, # Still return success since state is cleared "message": f"Disconnected with cleanup warning: {str(e)}", "was_connected": was_connected, "metadata": { "request_time_ms": round(elapsed_ms, 2), "cleanup_warning": str(e), }, }

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/rdwj/mcp-test-mcp'

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