disconnect
Safely close the active MCP server connection and clear all connection state. This tool handles disconnection whether a connection exists or not, returning detailed status information.
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
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- Handler function for the 'disconnect' MCP tool. Uses @mcp.tool decorator for registration and calls ConnectionManager.disconnect() to perform the disconnection logic.@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), }, }
- Creation of the shared FastMCP server instance 'mcp' used for registering all tools via decorators, including 'disconnect'.# Create the shared FastMCP server instance mcp = FastMCP(name="mcp-test-mcp")
- ConnectionManager.disconnect() method called by the tool handler to close the MCP client connection and clear state.async def disconnect(cls) -> None: """Disconnect from the current MCP server. Closes the active client connection and clears all connection state. This method is safe to call even if no connection exists. """ async with _connection.lock: await cls._disconnect_internal()